Notifications
Clear all

Arduino with Stepper and LiquidCrystal Issue

Page 17 / 21

Voltage
(@voltage)
Estimable Member
Joined: 11 months ago
Posts: 169
Topic starter  
Posted by: @will

@voltage @madmisha

Since it seems obvious that I've changed too much too fast, here's an Oldie but Goldie with some relevant changes. Hopefully I didn't overstep the limits again.

 

//Voltage Version sketch_aug06e-Will-Mod-5
//Test of accelstepper with LCD and pot

//****************************************
//      0808 01
//      08-08-2021
//      base sketch by MadMisha
//      mods by Will
//      tested/verified by Voltage
//****************************************

#include <AccelStepper.h>
#include <LiquidCrystal.h>

//Define pins
int LEFT_PIN = 11;
int STOP_PIN = 2;
int RIGHT_PIN = 3;
int driverPUL = 12;    // PUL- pin
int driverDIR = 13;    // DIR- pin
int spd = A1;          // Potentiometer
int dia, old_dia = 0;  // Last pot reading
bool go = false;       // Is it running
bool dirFor = true;    // Direction to go in
float sps;             // Sets per second for this diameter
float RPM;             // RPM for this diameter 
//
//  SPR = chain ratio times gear ratio times stepper steps/rev (adjusted for multistepping)
//  SPE = SPR / 8 / pi = steps per eighth inch for 1" dia, need to afjust for given dia
//
float finalSPR = 3 * 4.25 * 400;            // Steps per rev after gear and chain
float finalSPE = 100*finalSPR/8/3.14159265; // Steps per eighth per 1" diameter

// Instantiate objects
AccelStepper stepper(1,driverPUL,driverDIR); // This needs to change in any example(Mode, Pul, Dir)
LiquidCrystal lcd(8,9,4,5,6,7); //Setup LCD

//Interupt Handlers
void formotor (){
    if (!go) {                                              // Can't change direction if running
        dirFor = true;                                      // Set new direction
        go = true;                                          // Now we're running
    }
}

void bacmotor (){
    if (!go) {                                              // Can't change direction if running
        dirFor = false;                                     // Set new direction
        go = true;                                          // Now we're running
    }
}
//
//      The preliminaries
//
void setup()
{ 
    // Set pin modes
    pinMode(LEFT_PIN, INPUT_PULLUP);
    pinMode(STOP_PIN, INPUT_PULLUP);
    pinMode(RIGHT_PIN, INPUT_PULLUP);

    // Attach interrupts
    attachInterrupt(digitalPinToInterrupt(LEFT_PIN), formotor, FALLING);
    attachInterrupt(digitalPinToInterrupt(RIGHT_PIN), bacmotor, FALLING);
    //attachInterrupt(digitalPinToInterrupt(STOP_PIN), stpmotor, FALLING);

    // Setpper attributes
    //stepper.setPinsInverted(false, true, false);
    stepper.setMaxSpeed(1000);
    stepper.setMinPulseWidth(2.7);

    // LCD setup
    lcd.begin(16, 2);
    lcd.setCursor(1,0);
    lcd.print("Dia:");
    lcd.setCursor(1,1);
    lcd.print("RPM:");
}

void loop()
{
    //
    //      Handle the stop button
    //
    if (digitalRead(STOP_PIN)==LOW) {
        unsigned long now = millis();                       // Get current time
        while ( (millis()-now) < 50 ) {                     // Debouncing
            recalcSpeed();                                  // Update speed if req'd
        }
        go = false;
        stepper.stop();
        stepper.setSpeed(0);
    }
    //
    //      Recaculate speed if required
    //
    recalcSpeed();                                          // Recalculate speed
    //
    //      If we're running, then run else stop.
    //
    if (go){
        stepper.runSpeed();
    } else {
        stepper.stop();
    }
}
//
//      Check for speed change
//
void recalcSpeed()
{
    dia =  map((analogRead(spd)),0,1023,50,1200);           // Get diameter (x 100)
    if (dia!=old_dia) {                                     // If diameter changed
        sps = finalSPE/dia;                                 // Calc steps/sec
        RPM = 60*sps/finalSPR;                              // Calc RPM
        if (go) stepper.setSpeed( (dirFor) ? sps : -sps);   // Set new speed
        lcd.setCursor(5,0);                                 // Modify LCD to match
        lcd.print("      ");
        if (go) stepper.runSpeed();
        lcd.setCursor(5,0);
        lcd.print(dia/100.0);                               // Print new diameter
        if (go) stepper.runSpeed();
        //
        lcd.setCursor(5,1);
        lcd.print("      ");
        if (go) stepper.runSpeed();
        lcd.setCursor(5,1);
        lcd.print( (dirFor) ? RPM : -RPM);                  // Print new RPM
        if (go) stepper.runSpeed();
        //
        old_dia = dia;
    }
}

 

Uh,oh. No go. Nothing works. Except the display. I will go back and check the wiring but it should have changed unless you changed some pins... I may have to re-read this thread and find the last version that was close to working properly when I was making disks etc.

 

Thanks,
Voltage


ReplyQuote
Will
 Will
(@will)
Noble Member
Joined: 12 months ago
Posts: 1780
 
Posted by: @voltage

Uh,oh. No go. Nothing works. Except the display. I will go back and check the wiring but it should have changed unless you changed some pins... I may have to re-read this thread and find the last version that was close to working properly when I was making disks etc.

Yeah, I think it's come to that because I have no idea what I'm going wrong but I'd like to find out why and where.

First, please test the original version that came with the kit so we'll know that the electronics and stepper are still operational.

Then, if you can dig out the last working version and test it to make sure it still works, I'll try making just a single change at  time. Hopefully that will tell us what I'm screwing up.

I was kidnapped by mimes.
They did unspeakable things to me.


ReplyQuote
Voltage
(@voltage)
Estimable Member
Joined: 11 months ago
Posts: 169
Topic starter  
Posted by: @will
Posted by: @voltage

Uh,oh. No go. Nothing works. Except the display. I will go back and check the wiring but it should have changed unless you changed some pins... I may have to re-read this thread and find the last version that was close to working properly when I was making disks etc.

Yeah, I think it's come to that because I have no idea what I'm going wrong but I'd like to find out why and where.

First, please test the original version that came with the kit so we'll know that the electronics and stepper are still operational.

Then, if you can dig out the last working version and test it to make sure it still works, I'll try making just a single change at  time. Hopefully that will tell us what I'm screwing up.

I was just going through all the last sketches to find the one that worked the best with only a minor issue of only the one direction worked. I will post that sketch here but you can mod the headers with comments so we know this is a good starting point.

edit: The stepper changes direction with a double click of the stop button, but only after you change the position of the potentiometer. It is best if the Left Button is added and use if I remember correct.

 

//Voltage Version sketch_aug06e-Will-Mod-4
//last best working sketch
//Test of accelstepper with LCD and pot

#include <AccelStepper.h>
#include <LiquidCrystal.h>

//Define pins
int LEFT_PIN = 11;
int STOP_PIN = 2;
int RIGHT_PIN = 3;
int driverPUL = 12;    // PUL- pin
int driverDIR = 13;    // DIR- pin
int spd = A1;          // Potentiometer
int dia, old_dia = 0;  // Last pot reading
bool go = false;  //Is it running
bool dirFor = true; //Direction to go in
float sps;             // Sets per second for this diameter
float RPM;             // RPM for this diameter 

AccelStepper stepper(1,driverPUL,driverDIR); // This needs to change in any example(Mode, Pul, Dir)

LiquidCrystal lcd(8,9,4,5,6,7); //Setup LCD

//Interupt Handlers
void formotor (){

  dirFor = true;
  go = true;
  
}

void bacmotor (){

  lcd.setCursor(0,1);
  go = true;
  
}

void stpmotor (){

  if(go){
    stepper.stop();
    go = false;
  } else {
    dirFor = !dirFor;
  }  
}


void setup()
{  
   //pinMode(LEFT_PIN, INPUT_PULLUP);
   pinMode(STOP_PIN, INPUT_PULLUP);
   pinMode(RIGHT_PIN, INPUT_PULLUP);

   //attachInterrupt(digitalPinToInterrupt(LEFT_PIN), formotor, FALLING);
   attachInterrupt(digitalPinToInterrupt(RIGHT_PIN), bacmotor, FALLING);
   attachInterrupt(digitalPinToInterrupt(STOP_PIN), stpmotor, FALLING);
   
   //stepper.setPinsInverted(false, true, false);
   stepper.setMaxSpeed(1000);
   stepper.setMinPulseWidth(2.7);

   lcd.begin(16, 2);
   lcd.setCursor(1,0);
   lcd.print("Dia:");
   lcd.setCursor(1,1);
   lcd.print("RPM:");
}

void loop()
{
  if (go){
    dia =  map((analogRead(spd)),0,1023,50,1200);         // Get diameter (x 100)
    if (dia!=old_dia) {                                   // If diameter changed
        sps = 20290/dia;                                  // Calc steps/sec
        stepper.setSpeed( (dirFor) ? sps : -sps);         // Set new speed
        RPM = 60*sps/5100;                                // Calc RPM
        lcd.setCursor(5,0);                               // Modify LCD to match
        lcd.print("      ");
        lcd.setCursor(5,0);
        lcd.print(dia/100.0);
        stepper.runSpeed();
        //
        lcd.setCursor(5,1);
        lcd.print("      ");
        lcd.setCursor(5,1);
        lcd.print( (dirFor) ? RPM : -RPM);
        stepper.runSpeed();
        //
        old_dia = dia;
    }
    stepper.runSpeed();
  } else {
    stepper.stop();
  }
}

 

 

Thanks,
Voltage


ReplyQuote
Will
 Will
(@will)
Noble Member
Joined: 12 months ago
Posts: 1780
 

@voltage

The code in the last post is all in html. You may have forgotten to press the {;} in the icon bar before you pasted the code ?

I was kidnapped by mimes.
They did unspeakable things to me.


ReplyQuote
Voltage
(@voltage)
Estimable Member
Joined: 11 months ago
Posts: 169
Topic starter  
Posted by: @will

@voltage

The code in the last post is ll in html. You may have forgotten to press the {;} in the icon bar before you pasted the code ?

Yeah it took a few tries to figure that out this morning. It should be ok now. 😎 Also, read my edit above on the direction issue.

 

Thanks,
Voltage


ReplyQuote
Will
 Will
(@will)
Noble Member
Joined: 12 months ago
Posts: 1780
 

@voltage

Looks good now, thanks

I was kidnapped by mimes.
They did unspeakable things to me.


ReplyQuote
Will
 Will
(@will)
Noble Member
Joined: 12 months ago
Posts: 1780
 

@voltage

 

OK, I added some comments and a couple of float values to the top. I changed the speed and RPM calculations to use those values. I changed the version name to Restart 01

I'm thinking of addressing the buttons one at a time, starting with the left button (which you point out isn't working in this sketch). These will be changed, one at a time in the following test.

All that assumes that this test works 🙂

 

//last best working sketch
//Test of accelstepper with LCD and pot

//****************************************
//      Restart 01
//      08-08-2021
//      base sketch by MadMisha
//      mods by Will
//      tested/verified by Voltage
//****************************************

#include <AccelStepper.h>
#include <LiquidCrystal.h>

//Define pins
int LEFT_PIN = 11;
int STOP_PIN = 2;
int RIGHT_PIN = 3;
int driverPUL = 12;    // PUL- pin
int driverDIR = 13;    // DIR- pin
int spd = A1;          // Potentiometer
int dia, old_dia = 0;  // Last pot reading
bool go = false;  //Is it running
bool dirFor = true; //Direction to go in
float sps;             // Sets per second for this diameter
float RPM;             // RPM for this diameter 
//
//  SPR = chain ratio times gear ratio times stepper steps/rev (adjusted for multistepping)
//  SPE = SPR / 8 / pi = steps per eighth inch for 1" dia, need to afjust for given dia
//
float finalSPR = 3 * 4.25 * 400;            // Steps per rev after gear and chain
float finalSPE = 100*finalSPR/8/3.14159265; // Steps per eighth per 1" diameter

AccelStepper stepper(1,driverPUL,driverDIR); // This needs to change in any example(Mode, Pul, Dir)

LiquidCrystal lcd(8,9,4,5,6,7); //Setup LCD

//Interupt Handlers
void formotor (){

  dirFor = true;
  go = true;
  
}

void bacmotor (){

  dirFor = false;
  go = true;
  
}

void stpmotor (){

  if(go){
    stepper.stop();
    go = false;
  } else {
    dirFor = !dirFor;
  }  
}


void setup()
{  
   //pinMode(LEFT_PIN, INPUT_PULLUP);
   pinMode(STOP_PIN, INPUT_PULLUP);
   pinMode(RIGHT_PIN, INPUT_PULLUP);

   //attachInterrupt(digitalPinToInterrupt(LEFT_PIN), formotor, FALLING);
   attachInterrupt(digitalPinToInterrupt(RIGHT_PIN), bacmotor, FALLING);
   attachInterrupt(digitalPinToInterrupt(STOP_PIN), stpmotor, FALLING);
   
   //stepper.setPinsInverted(false, true, false);
   stepper.setMaxSpeed(1000);
   stepper.setMinPulseWidth(2.7);

   lcd.begin(16, 2);
   lcd.setCursor(1,0);
   lcd.print("Dia:");
   lcd.setCursor(1,1);
   lcd.print("RPM:");
}

void loop()
{
  if (go){
    dia =  map((analogRead(spd)),0,1023,50,1200);         // Get diameter (x 100)
    if (dia!=old_dia) {                                   // If diameter changed
        sps = finalSPE/dia;                               // Calc steps/sec
        stepper.setSpeed( (dirFor) ? sps : -sps);         // Set new speed
        RPM = 60*sps/finalSPR;                            // Calc RPM
        lcd.setCursor(5,0);                               // Modify LCD to match
        lcd.print("      ");
        lcd.setCursor(5,0);
        lcd.print(dia/100.0);
        stepper.runSpeed();
        //
        lcd.setCursor(5,1);
        lcd.print("      ");
        lcd.setCursor(5,1);
        lcd.print( (dirFor) ? RPM : -RPM);
        stepper.runSpeed();
        //
        old_dia = dia;
    }
    stepper.runSpeed();
  } else {
    stepper.stop();
  }
}

 

I was kidnapped by mimes.
They did unspeakable things to me.


ReplyQuote
Voltage
(@voltage)
Estimable Member
Joined: 11 months ago
Posts: 169
Topic starter  
Posted by: @will

@voltage

 

OK, I added some comments and a couple of float values to the top. I changed the speed and RPM calculations to use those values. I changed the version name to Restart 01

I'm thinking of addressing the buttons one at a time, starting with the left button (which you point out isn't working in this sketch). These will be changed, one at a time in the following test.

All that assumes that this test works 🙂

 

//last best working sketch
//Test of accelstepper with LCD and pot

//****************************************
//      Restart 01
//      08-08-2021
//      base sketch by MadMisha
//      mods by Will
//      tested/verified by Voltage
//****************************************

#include <AccelStepper.h>
#include <LiquidCrystal.h>

//Define pins
int LEFT_PIN = 11;
int STOP_PIN = 2;
int RIGHT_PIN = 3;
int driverPUL = 12;    // PUL- pin
int driverDIR = 13;    // DIR- pin
int spd = A1;          // Potentiometer
int dia, old_dia = 0;  // Last pot reading
bool go = false;  //Is it running
bool dirFor = true; //Direction to go in
float sps;             // Sets per second for this diameter
float RPM;             // RPM for this diameter 
//
//  SPR = chain ratio times gear ratio times stepper steps/rev (adjusted for multistepping)
//  SPE = SPR / 8 / pi = steps per eighth inch for 1" dia, need to afjust for given dia
//
float finalSPR = 3 * 4.25 * 400;            // Steps per rev after gear and chain
float finalSPE = 100*finalSPR/8/3.14159265; // Steps per eighth per 1" diameter

AccelStepper stepper(1,driverPUL,driverDIR); // This needs to change in any example(Mode, Pul, Dir)

LiquidCrystal lcd(8,9,4,5,6,7); //Setup LCD

//Interupt Handlers
void formotor (){

  dirFor = true;
  go = true;
  
}

void bacmotor (){

  dirFor = false;
  go = true;
  
}

void stpmotor (){

  if(go){
    stepper.stop();
    go = false;
  } else {
    dirFor = !dirFor;
  }  
}


void setup()
{  
   //pinMode(LEFT_PIN, INPUT_PULLUP);
   pinMode(STOP_PIN, INPUT_PULLUP);
   pinMode(RIGHT_PIN, INPUT_PULLUP);

   //attachInterrupt(digitalPinToInterrupt(LEFT_PIN), formotor, FALLING);
   attachInterrupt(digitalPinToInterrupt(RIGHT_PIN), bacmotor, FALLING);
   attachInterrupt(digitalPinToInterrupt(STOP_PIN), stpmotor, FALLING);
   
   //stepper.setPinsInverted(false, true, false);
   stepper.setMaxSpeed(1000);
   stepper.setMinPulseWidth(2.7);

   lcd.begin(16, 2);
   lcd.setCursor(1,0);
   lcd.print("Dia:");
   lcd.setCursor(1,1);
   lcd.print("RPM:");
}

void loop()
{
  if (go){
    dia =  map((analogRead(spd)),0,1023,50,1200);         // Get diameter (x 100)
    if (dia!=old_dia) {                                   // If diameter changed
        sps = finalSPE/dia;                               // Calc steps/sec
        stepper.setSpeed( (dirFor) ? sps : -sps);         // Set new speed
        RPM = 60*sps/finalSPR;                            // Calc RPM
        lcd.setCursor(5,0);                               // Modify LCD to match
        lcd.print("      ");
        lcd.setCursor(5,0);
        lcd.print(dia/100.0);
        stepper.runSpeed();
        //
        lcd.setCursor(5,1);
        lcd.print("      ");
        lcd.setCursor(5,1);
        lcd.print( (dirFor) ? RPM : -RPM);
        stepper.runSpeed();
        //
        old_dia = dia;
    }
    stepper.runSpeed();
  } else {
    stepper.stop();
  }
}

 

Ok. Works like before with only one direction but the numbers aren't right. I think you have to add a 1 or 100 somewhere as this looks familiar. Anyway:

DIA    RPM

12      -0.20

9        -0.27

6        -0.40

0.50   -4.77

Edit. Maybe I better try that 4.875" disk. It just looks really slow.

About 43 seconds for 1 revolution with 4 7/8" disk, I think that may be correct. 🙂

Thanks,
Voltage


ReplyQuote
Will
 Will
(@will)
Noble Member
Joined: 12 months ago
Posts: 1780
 

@voltage

 

OK, 12 inches means 12 x ∏ x8 eighths of an inch and 1 rev = 3 x 4.25 x 400 steps.

so 1 eight = 5100/301.6 = 16.9 steps/second

60 seconds x 16.9 steps/second = 1014 steps/minute

1014 steps/minute = 1014/5100 revs/minute

= 0.1988 RPM (which will be rounded to .20 for display on the LCD)

 

What am I missing ?

I was kidnapped by mimes.
They did unspeakable things to me.


ReplyQuote
Voltage
(@voltage)
Estimable Member
Joined: 11 months ago
Posts: 169
Topic starter  
Posted by: @will

@voltage

 

OK, 12 inches means 12 x ∏ x8 eighths of an inch and 1 rev = 3 x 4.25 x 400 steps.

so 1 eight = 5100/301.6 = 16.9 steps/second

60 seconds x 16.9 steps/second = 1014 steps/minute

1014 steps/minute = 1014/5100 revs/minute

= 0.1988 RPM (which will be rounded to .20 for display on the LCD)

 

What am I missing ?

It looks correct. Without the disk on the output it just looks really slow. That is why I put the disk on and double checked. We went through the same thing on Saturday where you had 0.123 instead of 0.93 as far as thousandths of and inch per second when you divided something backwards. Sorry about that.

 

Thanks,
Voltage


ReplyQuote
Will
 Will
(@will)
Noble Member
Joined: 12 months ago
Posts: 1780
 

@voltage

 

Excellent, it always pays to double-check.

Here's the next instalment, I've added some comments at the top to describe what changes have been made in each iteration to help distinguish between versions.

//Voltage Version sketch_aug06e-Will-Mod-4
//last best working sketch
//Test of accelstepper with LCD and pot

//****************************************
//      Restart 02
//      08-08-2021
//      base sketch by MadMisha
//      mods by Will
//      tested/verified by Voltage
//****************************************

//  Rev 1
//      - added some comments
//      - added new float values to calculate reev/sec of entire system
//      - relaced literals wit variable above
//
//  Rev 2
//      - added recalcSpeed sub to check pot change and update display
//      - if the diameter has changed, then the LCD is udated
//      - if speed changed and we're running (g0==true) update stepper speed
//      - dia & RPM update even while stopped
//
//
#include <AccelStepper.h>
#include <LiquidCrystal.h>

//Define pins
int LEFT_PIN = 11;
int STOP_PIN = 2;
int RIGHT_PIN = 3;
int driverPUL = 12;    // PUL- pin
int driverDIR = 13;    // DIR- pin
int spd = A1;          // Potentiometer
int dia, old_dia = 0;  // Last pot reading
bool go = false;  //Is it running
bool dirFor = true; //Direction to go in
float sps;             // Sets per second for this diameter
float RPM;             // RPM for this diameter 
//
//  SPR = chain ratio times gear ratio times stepper steps/rev (adjusted for multistepping)
//  SPE = SPR / 8 / pi = steps per eighth inch for 1" dia, need to afjust for given dia
//
float finalSPR = 3 * 4.25 * 400;                // Steps per rev after gear and chain
float finalSPE = 100*finalSPR/8/3.14159265;     // Steps per eighth per 1" diameter

AccelStepper stepper(1,driverPUL,driverDIR);    // Create stepper

LiquidCrystal lcd(8,9,4,5,6,7);                 // Create LCD

//Interupt Handlers
void formotor (){

  dirFor = true;
  go = true;
  
}

void bacmotor (){

  dirFor = false;
  go = true;
  
}

void stpmotor (){

  if(go){
    stepper.stop();
    go = false;
  } else {
    dirFor = !dirFor;
  }  
}


void setup()
{  
   //pinMode(LEFT_PIN, INPUT_PULLUP);
   pinMode(STOP_PIN, INPUT_PULLUP);
   pinMode(RIGHT_PIN, INPUT_PULLUP);

   //attachInterrupt(digitalPinToInterrupt(LEFT_PIN), formotor, FALLING);
   attachInterrupt(digitalPinToInterrupt(RIGHT_PIN), bacmotor, FALLING);
   attachInterrupt(digitalPinToInterrupt(STOP_PIN), stpmotor, FALLING);
   
   //stepper.setPinsInverted(false, true, false);
   stepper.setMaxSpeed(1000);
   stepper.setMinPulseWidth(2.7);

   lcd.begin(16, 2);
   lcd.setCursor(1,0);
   lcd.print("Dia:");
   lcd.setCursor(1,1);
   lcd.print("RPM:");
}

void loop()
{
    recalcSpeed();                                          // Check for pot moved
    if (go) {
        stepper.runSpeed();
    } else {
        stepper.stop();
    }
}
//
//      Check for speed change
//
void recalcSpeed()
{
    dia =  map((analogRead(spd)),0,1023,50,1200);           // Get diameter (x 100)
    if (dia!=old_dia) {                                     // If diameter changed
        sps = finalSPE/dia;                                 // Calc steps/sec
        RPM = 60*sps/finalSPR;                              // Calc RPM
        if (go) stepper.setSpeed( (dirFor) ? sps : -sps);   // Set new speed
        lcd.setCursor(5,0);                                 // Modify LCD to match
        lcd.print("      ");
        if (go) stepper.runSpeed();
        lcd.setCursor(5,0);
        lcd.print(dia/100.0);                               // Print new diameter
        if (go) stepper.runSpeed();
        //
        lcd.setCursor(5,1);
        lcd.print("      ");
        if (go) stepper.runSpeed();
        lcd.setCursor(5,1);
        lcd.print( (dirFor) ? RPM : -RPM);                  // Print new RPM
        if (go) stepper.runSpeed();
        //
        old_dia = dia;
    }
}

 

 

I was kidnapped by mimes.
They did unspeakable things to me.


ReplyQuote
Voltage
(@voltage)
Estimable Member
Joined: 11 months ago
Posts: 169
Topic starter  
Posted by: @will

@voltage

 

Excellent, it always pays to double-check.

Here's the next instalment, I've added some comments at the top to describe what changes have been made in each iteration to help distinguish between versions.

//Voltage Version sketch_aug06e-Will-Mod-4
//last best working sketch
//Test of accelstepper with LCD and pot

//****************************************
//      Restart 02
//      08-08-2021
//      base sketch by MadMisha
//      mods by Will
//      tested/verified by Voltage
//****************************************

//  Rev 1
//      - added some comments
//      - added new float values to calculate reev/sec of entire system
//      - relaced literals wit variable above
//
//  Rev 2
//      - added recalcSpeed sub to check pot change and update display
//      - if the diameter has changed, then the LCD is udated
//      - if speed changed and we're running (g0==true) update stepper speed
//      - dia & RPM update even while stopped
//
//
#include <AccelStepper.h>
#include <LiquidCrystal.h>

//Define pins
int LEFT_PIN = 11;
int STOP_PIN = 2;
int RIGHT_PIN = 3;
int driverPUL = 12;    // PUL- pin
int driverDIR = 13;    // DIR- pin
int spd = A1;          // Potentiometer
int dia, old_dia = 0;  // Last pot reading
bool go = false;  //Is it running
bool dirFor = true; //Direction to go in
float sps;             // Sets per second for this diameter
float RPM;             // RPM for this diameter 
//
//  SPR = chain ratio times gear ratio times stepper steps/rev (adjusted for multistepping)
//  SPE = SPR / 8 / pi = steps per eighth inch for 1" dia, need to afjust for given dia
//
float finalSPR = 3 * 4.25 * 400;                // Steps per rev after gear and chain
float finalSPE = 100*finalSPR/8/3.14159265;     // Steps per eighth per 1" diameter

AccelStepper stepper(1,driverPUL,driverDIR);    // Create stepper

LiquidCrystal lcd(8,9,4,5,6,7);                 // Create LCD

//Interupt Handlers
void formotor (){

  dirFor = true;
  go = true;
  
}

void bacmotor (){

  dirFor = false;
  go = true;
  
}

void stpmotor (){

  if(go){
    stepper.stop();
    go = false;
  } else {
    dirFor = !dirFor;
  }  
}


void setup()
{  
   //pinMode(LEFT_PIN, INPUT_PULLUP);
   pinMode(STOP_PIN, INPUT_PULLUP);
   pinMode(RIGHT_PIN, INPUT_PULLUP);

   //attachInterrupt(digitalPinToInterrupt(LEFT_PIN), formotor, FALLING);
   attachInterrupt(digitalPinToInterrupt(RIGHT_PIN), bacmotor, FALLING);
   attachInterrupt(digitalPinToInterrupt(STOP_PIN), stpmotor, FALLING);
   
   //stepper.setPinsInverted(false, true, false);
   stepper.setMaxSpeed(1000);
   stepper.setMinPulseWidth(2.7);

   lcd.begin(16, 2);
   lcd.setCursor(1,0);
   lcd.print("Dia:");
   lcd.setCursor(1,1);
   lcd.print("RPM:");
}

void loop()
{
    recalcSpeed();                                          // Check for pot moved
    if (go) {
        stepper.runSpeed();
    } else {
        stepper.stop();
    }
}
//
//      Check for speed change
//
void recalcSpeed()
{
    dia =  map((analogRead(spd)),0,1023,50,1200);           // Get diameter (x 100)
    if (dia!=old_dia) {                                     // If diameter changed
        sps = finalSPE/dia;                                 // Calc steps/sec
        RPM = 60*sps/finalSPR;                              // Calc RPM
        if (go) stepper.setSpeed( (dirFor) ? sps : -sps);   // Set new speed
        lcd.setCursor(5,0);                                 // Modify LCD to match
        lcd.print("      ");
        if (go) stepper.runSpeed();
        lcd.setCursor(5,0);
        lcd.print(dia/100.0);                               // Print new diameter
        if (go) stepper.runSpeed();
        //
        lcd.setCursor(5,1);
        lcd.print("      ");
        if (go) stepper.runSpeed();
        lcd.setCursor(5,1);
        lcd.print( (dirFor) ? RPM : -RPM);                  // Print new RPM
        if (go) stepper.runSpeed();
        //
        old_dia = dia;
    }
}

 

 

Seems to work fine but with noticeably more flicker in display digits on the LCD. I changed the date of the code to todays date under Restart 2 and fixed comment typo reev to rev. 🙂 Is that supposed to be rev/step or rev/sec?

 

Thanks,
Voltage


ReplyQuote
Will
 Will
(@will)
Noble Member
Joined: 12 months ago
Posts: 1780
 

@voltage

 

The extra flicker's probably because it's checking more often. It may get worse for the moment as the buttons are added, so we'll address that if e get all of the buttons working.

Here's a version where the left button should work but ONLY if it's stopped running. This one has me worried.

 

 

//Voltage Version sketch_aug06e-Will-Mod-4
//last best working sketch
//Test of accelstepper with LCD and pot

//****************************************
//      Restart 03
//      08-08-2021
//      base sketch by MadMisha
//      mods by Will
//      tested/verified by Voltage
//****************************************

//  Rev 1
//      - added some comments
//      - added new float values to calculate reev/sec of entire system
//      - relaced literals wit variable above
//
//  Rev 2
//      - added recalcSpeed sub to check pot change and update display
//      - if the diameter has changed, then the LCD is udated
//      - if speed changed and we're running (g0==true) update stepper speed
//      - dia & RPM update even while stopped
//
//  Rev 3
//      - added sub to handle debounce
//      - added code for LEFT button
//      - only active when NOT running
//      - needs to accomodate bounce
//
//
#include <AccelStepper.h>
#include <LiquidCrystal.h>

//Define pins
int LEFT_PIN = 11;
int STOP_PIN = 2;
int RIGHT_PIN = 3;
int driverPUL = 12;    // PUL- pin
int driverDIR = 13;    // DIR- pin
int spd = A1;          // Potentiometer
int dia, old_dia = 0;  // Last pot reading
bool go = false;  //Is it running
bool dirFor = true; //Direction to go in
float sps;             // Sets per second for this diameter
float RPM;             // RPM for this diameter 
//
//  SPR = chain ratio times gear ratio times stepper steps/rev (adjusted for multistepping)
//  SPE = SPR / 8 / pi = steps per eighth inch for 1" dia, need to afjust for given dia
//
float finalSPR = 3 * 4.25 * 400;                // Steps per rev after gear and chain
float finalSPE = 100*finalSPR/8/3.14159265;     // Steps per eighth per 1" diameter

AccelStepper stepper(1,driverPUL,driverDIR);    // Create stepper

LiquidCrystal lcd(8,9,4,5,6,7);                 // Create LCD

//Interupt Handlers

void bacmotor (){

  dirFor = false;
  go = true;
  
}

void stpmotor (){

  if(go){
    stepper.stop();
    go = false;
  } else {
    dirFor = !dirFor;
  }  
}


void setup()
{  
   pinMode(LEFT_PIN, INPUT_PULLUP);
   pinMode(STOP_PIN, INPUT_PULLUP);
   pinMode(RIGHT_PIN, INPUT_PULLUP);

   attachInterrupt(digitalPinToInterrupt(RIGHT_PIN), bacmotor, FALLING);
   attachInterrupt(digitalPinToInterrupt(STOP_PIN), stpmotor, FALLING);
   
   //stepper.setPinsInverted(false, true, false);
   stepper.setMaxSpeed(1000);
   stepper.setMinPulseWidth(2.7);

   lcd.begin(16, 2);
   lcd.setCursor(1,0);
   lcd.print("Dia:");
   lcd.setCursor(1,1);
   lcd.print("RPM:");
}

void loop()
{
    //
    //      Handle buttons
    //
    if ( (digitalRead(LEFT_PIN)==LOW) && !go ) {
        dirFor = false;
        deBounce(50,true);                                  // Handle bounce
        go = true;
    }
    //
    //      Business as usual
    //
    recalcSpeed();                                          // Check for pot moved
    if (go) {
        stepper.runSpeed();
    } else {
        stepper.stop();
    }
}
//
//      Check for speed change
//
void recalcSpeed()
{
    dia =  map((analogRead(spd)),0,1023,50,1200);           // Get diameter (x 100)
    if (dia!=old_dia) {                                     // If diameter changed
        sps = finalSPE/dia;                                 // Calc steps/sec
        RPM = 60*sps/finalSPR;                              // Calc RPM
        if (go) stepper.setSpeed( (dirFor) ? sps : -sps);   // Set new speed
        lcd.setCursor(5,0);                                 // Modify LCD to match
        lcd.print("      ");
        if (go) stepper.runSpeed();
        lcd.setCursor(5,0);
        lcd.print(dia/100.0);                               // Print new diameter
        if (go) stepper.runSpeed();
        //
        lcd.setCursor(5,1);
        lcd.print("      ");
        if (go) stepper.runSpeed();
        lcd.setCursor(5,1);
        lcd.print( (dirFor) ? RPM : -RPM);                  // Print new RPM
        if (go) stepper.runSpeed();
        //
        old_dia = dia;
    }
}
//
//      Debounce
//
void deBounce( int milliSeconds, bool keepStepping ) {
    unsigned long   now;                                    // Current time
    while ( (millis()-now)<milliSeconds) {                  // While inside bounce window
        recalcSpeed();                                      // Check for speed change
        if (keepStepping)
            stepper.runSpeed();                             // Keep moving
    }
}

 

I was kidnapped by mimes.
They did unspeakable things to me.


ReplyQuote
Voltage
(@voltage)
Estimable Member
Joined: 11 months ago
Posts: 169
Topic starter  
Posted by: @will

@voltage

 

The extra flicker's probably because it's checking more often. It may get worse for the moment as the buttons are added, so we'll address that if e get all of the buttons working.

Here's a version where the left button should work but ONLY if it's stopped running. This one has me worried.

 

 

//Voltage Version sketch_aug06e-Will-Mod-4
//last best working sketch
//Test of accelstepper with LCD and pot

//****************************************
//      Restart 03
//      08-08-2021
//      base sketch by MadMisha
//      mods by Will
//      tested/verified by Voltage
//****************************************

//  Rev 1
//      - added some comments
//      - added new float values to calculate reev/sec of entire system
//      - relaced literals wit variable above
//
//  Rev 2
//      - added recalcSpeed sub to check pot change and update display
//      - if the diameter has changed, then the LCD is udated
//      - if speed changed and we're running (g0==true) update stepper speed
//      - dia & RPM update even while stopped
//
//  Rev 3
//      - added sub to handle debounce
//      - added code for LEFT button
//      - only active when NOT running
//      - needs to accomodate bounce
//
//
#include <AccelStepper.h>
#include <LiquidCrystal.h>

//Define pins
int LEFT_PIN = 11;
int STOP_PIN = 2;
int RIGHT_PIN = 3;
int driverPUL = 12;    // PUL- pin
int driverDIR = 13;    // DIR- pin
int spd = A1;          // Potentiometer
int dia, old_dia = 0;  // Last pot reading
bool go = false;  //Is it running
bool dirFor = true; //Direction to go in
float sps;             // Sets per second for this diameter
float RPM;             // RPM for this diameter 
//
//  SPR = chain ratio times gear ratio times stepper steps/rev (adjusted for multistepping)
//  SPE = SPR / 8 / pi = steps per eighth inch for 1" dia, need to afjust for given dia
//
float finalSPR = 3 * 4.25 * 400;                // Steps per rev after gear and chain
float finalSPE = 100*finalSPR/8/3.14159265;     // Steps per eighth per 1" diameter

AccelStepper stepper(1,driverPUL,driverDIR);    // Create stepper

LiquidCrystal lcd(8,9,4,5,6,7);                 // Create LCD

//Interupt Handlers

void bacmotor (){

  dirFor = false;
  go = true;
  
}

void stpmotor (){

  if(go){
    stepper.stop();
    go = false;
  } else {
    dirFor = !dirFor;
  }  
}


void setup()
{  
   pinMode(LEFT_PIN, INPUT_PULLUP);
   pinMode(STOP_PIN, INPUT_PULLUP);
   pinMode(RIGHT_PIN, INPUT_PULLUP);

   attachInterrupt(digitalPinToInterrupt(RIGHT_PIN), bacmotor, FALLING);
   attachInterrupt(digitalPinToInterrupt(STOP_PIN), stpmotor, FALLING);
   
   //stepper.setPinsInverted(false, true, false);
   stepper.setMaxSpeed(1000);
   stepper.setMinPulseWidth(2.7);

   lcd.begin(16, 2);
   lcd.setCursor(1,0);
   lcd.print("Dia:");
   lcd.setCursor(1,1);
   lcd.print("RPM:");
}

void loop()
{
    //
    //      Handle buttons
    //
    if ( (digitalRead(LEFT_PIN)==LOW) && !go ) {
        dirFor = false;
        deBounce(50,true);                                  // Handle bounce
        go = true;
    }
    //
    //      Business as usual
    //
    recalcSpeed();                                          // Check for pot moved
    if (go) {
        stepper.runSpeed();
    } else {
        stepper.stop();
    }
}
//
//      Check for speed change
//
void recalcSpeed()
{
    dia =  map((analogRead(spd)),0,1023,50,1200);           // Get diameter (x 100)
    if (dia!=old_dia) {                                     // If diameter changed
        sps = finalSPE/dia;                                 // Calc steps/sec
        RPM = 60*sps/finalSPR;                              // Calc RPM
        if (go) stepper.setSpeed( (dirFor) ? sps : -sps);   // Set new speed
        lcd.setCursor(5,0);                                 // Modify LCD to match
        lcd.print("      ");
        if (go) stepper.runSpeed();
        lcd.setCursor(5,0);
        lcd.print(dia/100.0);                               // Print new diameter
        if (go) stepper.runSpeed();
        //
        lcd.setCursor(5,1);
        lcd.print("      ");
        if (go) stepper.runSpeed();
        lcd.setCursor(5,1);
        lcd.print( (dirFor) ? RPM : -RPM);                  // Print new RPM
        if (go) stepper.runSpeed();
        //
        old_dia = dia;
    }
}
//
//      Debounce
//
void deBounce( int milliSeconds, bool keepStepping ) {
    unsigned long   now;                                    // Current time
    while ( (millis()-now)<milliSeconds) {                  // While inside bounce window
        recalcSpeed();                                      // Check for speed change
        if (keepStepping)
            stepper.runSpeed();                             // Keep moving
    }
}

 

Both the left and right buttons make the motor run in the same direction. I tried the left button first, then stop, then right. All the same direction. Then I tried moving the potentiometer in case that would reverse it like before with the double stop press, but no go.

 

Thanks,
Voltage


ReplyQuote
Will
 Will
(@will)
Noble Member
Joined: 12 months ago
Posts: 1780
 

@voltage

OK, i've changed the direction associated with LEFT and RIGHT buttons. Please check that the move in opposite directions (and also the correct direction for their labels).

This version eliminates the interrupt used for the STOP button.

 

 

//Voltage Version sketch_aug06e-Will-Mod-4
//last best working sketch
//Test of accelstepper with LCD and pot

//****************************************
//      Restart 03
//      08-08-2021
//      base sketch by MadMisha
//      mods by Will
//      tested/verified by Voltage
//****************************************

//  Rev 1
//      - added some comments
//      - added new float values to calculate reev/sec of entire system
//      - relaced literals wit variable above
//
//  Rev 2
//      - added recalcSpeed sub to check pot change and update display
//      - if the diameter has changed, then the LCD is udated
//      - if speed changed and we're running (g0==true) update stepper speed
//      - dia & RPM update even while stopped
//
//  Rev 3
//      - added sub to handle debounce
//      - added code for LEFT button
//      - only active when NOT running
//      - needs to accomodate bounce
//
//  Rev 4
//      - removed interrupt for STOP button
//      - added STOP button into loop()
//      - Needs to accomodate bounce
//      - more tidying and more comments
//
//
#include <AccelStepper.h>
#include <LiquidCrystal.h>

//Define pins
int LEFT_PIN = 11;
int STOP_PIN = 2;
int RIGHT_PIN = 3;
int driverPUL = 12;     // PUL- pin
int driverDIR = 13;     // DIR- pin
int spd = A1;           // Potentiometer
int dia, old_dia = 0;   // Last pot reading
bool go = false;        // Is it running
bool dirFor = true;     // Direction to go in
float sps;              // Sets per second for this diameter
float RPM;              // RPM for this diameter 
//
//  SPR = chain ratio times gear ratio times stepper steps/rev (adjusted for multistepping)
//  SPE = SPR / 8 / pi = steps per eighth inch for 1" dia, need to afjust for given dia
//
float finalSPR = 3 * 4.25 * 400;                // Steps per rev after gear and chain
float finalSPE = 100*finalSPR/8/3.14159265;     // Steps per eighth per 1" diameter

AccelStepper stepper(1,driverPUL,driverDIR);    // Create stepper

LiquidCrystal lcd(8,9,4,5,6,7);                 // Create LCD

//Interupt Handlers

void bacmotor (){

  dirFor = true;
  go = true;
  
}

void setup()
{  
   pinMode(LEFT_PIN, INPUT_PULLUP);
   pinMode(STOP_PIN, INPUT_PULLUP);
   pinMode(RIGHT_PIN, INPUT_PULLUP);

   attachInterrupt(digitalPinToInterrupt(RIGHT_PIN), bacmotor, FALLING);
   
   //stepper.setPinsInverted(false, true, false);
   stepper.setMaxSpeed(1000);
   stepper.setMinPulseWidth(2.7);

   lcd.begin(16, 2);
   lcd.setCursor(1,0);
   lcd.print("Dia:");
   lcd.setCursor(1,1);
   lcd.print("RPM:");
}

void loop()
{
    //
    //      Handle buttons
    //
    if ( (digitalRead(LEFT_PIN)==LOW) && !go ) {
        go = true;                                          // Now we're going
        dirFor = false;                                     // Set direction
        deBounce(50,true);                                  // Handle bounce
    }
    //
    if ( (digitalRead(STOP_PIN)==LOW) && go ) {
        go = false;                                         // Not going any more
        stepper.stop();                                     // Stop stepper
        deBounce(50,false);                                 // Handle bounce
     }
    //
    //      Business as usual
    //
    recalcSpeed();                                          // Check for pot moved
    //
    //      Take a step if appropriate
    //
    if (go) {
        stepper.runSpeed();
    } else {
        stepper.stop();
    }
}
//
//      Check for speed change
//
void recalcSpeed()
{
    dia =  map((analogRead(spd)),0,1023,50,1200);           // Get diameter (x 100)
    if (dia!=old_dia) {                                     // If diameter changed
        sps = finalSPE/dia;                                 // Calc steps/sec
        RPM = 60*sps/finalSPR;                              // Calc RPM
        if (go) stepper.setSpeed( (dirFor) ? sps : -sps);   // Set new speed
        lcd.setCursor(5,0);                                 // Modify LCD to match
        lcd.print("      ");
        if (go) stepper.runSpeed();
        lcd.setCursor(5,0);
        lcd.print(dia/100.0);                               // Print new diameter
        if (go) stepper.runSpeed();
        //
        lcd.setCursor(5,1);
        lcd.print("      ");
        if (go) stepper.runSpeed();
        lcd.setCursor(5,1);
        lcd.print( (dirFor) ? RPM : -RPM);                  // Print new RPM
        if (go) stepper.runSpeed();
        //
        old_dia = dia;
    }
}
//
//      Debounce
//
void deBounce( int milliSeconds, bool keepStepping ) {
    unsigned long   now;                                    // Current time
    while ( (millis()-now)<milliSeconds) {                  // While inside bounce window
        recalcSpeed();                                      // Check for speed change
        if (keepStepping)
            stepper.runSpeed();                             // Keep moving
    }
}

 

I was kidnapped by mimes.
They did unspeakable things to me.


ReplyQuote
Page 17 / 21