Notifications
Clear all

Arduino with Stepper and LiquidCrystal Issue

Page 16 / 21

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

@voltage

Sorry, I wasn't clear. In that section I only want you to comment out the lines with a call to rampSpeed, not every line.

Ok, I will try that and be right back.

 

edit: Still the same but I rechecked my handy work and realized I missed one instance of rampspeed. But when I rem it out I get an error and am unsure what to do. Here is the line I rem out and get an error: expected unqualified-id before '{' token

//void rampSpeed( float fromSpeed, float toSpeed )

Should I remark out all of the void code as it has to do with rampSpeed?

Thanks,
Voltage


ReplyQuote
Will
 Will
(@will)
Noble Member
Joined: 11 months ago
Posts: 1764
 

@voltage

That last one i the definition of the function, it has to stay in.

When you turn on the power, do you press any buttons or just adjust the pot ?

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


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

@voltage

That last one i the definition of the function, it has to stay in.

When you turn on the power, do you press any buttons or just adjust the pot ?

I tried both ways. Tell me what you want me to do and I will go do it and report back. If I push the Left button for example I hear the stepper tic,tic, tic etc. The Stop button stops it. The Right button does the same. The pot and LCD display work great.

 

Thanks,
Voltage


ReplyQuote
Will
 Will
(@will)
Noble Member
Joined: 11 months ago
Posts: 1764
 

@voltage

I just wondered because it now powers up into "stop" mode, so you need to set the speed with the pot and then push the left or right button to make it move.

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


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

@voltage

I just wondered because it now powers up into "stop" mode, so you need to set the speed with the pot and then push the left or right button to make it move.

I tried everything. It seems like it gets a start signal as the stepper is going tic tic, tic tic, tic tic, and I can feel it and hear it if I put the stepper up to my ear. I can feel it in hand too.

 

Thanks,
Voltage


ReplyQuote
Will
 Will
(@will)
Noble Member
Joined: 11 months ago
Posts: 1764
 

@voltage

OK, can you please post the code you're testing. I'm afraid my version is out of date now and I need to see the code you're using.

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


ReplyQuote
Voltage
(@voltage)
Estimable Member
Joined: 10 months ago
Posts: 169
Topic starter  
//Voltage Version sketch_aug06e
//Test of accelstepper with LCD and pot

//****************************************
//     sketch_aug06e-Mod-7
//      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, delta_dia = 5;  // Last pot reading
bool go = false;        //Is it running
float targetSpeed=0;    // Speed we want
float RPM;              // RPM for this diameter 
float dirn = 1;         // Direction +1 right, -1 left;
//
//  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
//
//      The preliminaries
//
void setup()
{  
    // Set mode for pins
    pinMode(LEFT_PIN, INPUT_PULLUP);
    pinMode(STOP_PIN, INPUT_PULLUP);
    pinMode(RIGHT_PIN, INPUT_PULLUP);

    // Set stepper attributes
    stepper.setMinPulseWidth(2.7);

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

//
//      The main event(s)
//
void loop()
{
    //
    //      Check buttons
    //
    if (switchPushed(STOP_PIN) && go) {
//        rampSpeed(stepper.speed(),0);                       // Ramp speed down to zero
        go = false;                                         // No longer running
        stepper.stop();                                     // Stop stepper.
    } else if (switchPushed(LEFT_PIN) && !go && dirn!=-1) {
        dirn = -1;                                          // Set direction
        go = true;                                          // We're running again
        calcSpeed();                                        // Recalculate speed
//        rampSpeed(0,targetSpeed);                           // Ramp up to speed
        old_dia = 0;                                        // Force speed recalc
    } else if (switchPushed(RIGHT_PIN) && !go && dirn !=1) {
        dirn = 1;                                           // Set direction
        go = true;                                          // We're running again
        calcSpeed();                                        // Recalculate speed
//        rampSpeed(0,targetSpeed);                           // Ramp up to speed
        old_dia = 0;                                        // Force speed recalc
    }
 
    //
    //      Reset speed if required
    //
    dia =  map((analogRead(spd)),0,1023,50,1200);           // Get diameter (x 100)
    if (abs(dia-old_dia)>delta_dia) {                       // If diameter changed enough
        calcSpeed();                                        // Calculate new speed
        stepper.setSpeed(targetSpeed);
        stepper.runSpeed();
        lcd.setCursor(5,0);                                 // Modify LCD to match
        lcd.print("      ");
        stepper.runSpeed();
        lcd.setCursor(5,0);
        lcd.print(dia/100.0);
        stepper.runSpeed();
        //
        lcd.setCursor(5,1);
        lcd.print("       ");
        stepper.runSpeed();
        lcd.setCursor(5,1);
        lcd.print(RPM);
        stepper.runSpeed();
        //
        old_dia = dia;                                      // Start over
    }
    //
    //      Business as usual
    //
    if (go) {
        stepper.runSpeed();
    } else {
        stepper.stop();
    }
}
//
//      Test if switch on pin is pressed
//
bool switchPushed( int pinNo )
{
    return (digitalRead(pinNo)==LOW);                       // Is pin LOW (i.e. grounded)
}
//
//      Calculate speed from dia and direction
//
void calcSpeed()
{
    dia =  map((analogRead(spd)),0,1023,50,1200);           // Get diameter (x 100)
    targetSpeed = dirn*finalSPE/dia;                        // Calc steps/sec for dia
    RPM = 60*targetSpeed/finalSPR;                          // Calc RPM    
}
//
//      Ramp speed up or down between specified speeds
//
void rampSpeed( float fromSpeed, float toSpeed )
{
    long            hold = 100;
    unsigned long   oldMillis = millis();
    float           delta = (toSpeed-fromSpeed)/10;
    float           newSpeed = fromSpeed;
    //
    //      Change speed in 10 steps of 100 ms each (i.e. over 1 second)
    //
    for (int i=0;i<10;i++) {
        newSpeed = newSpeed+delta;                          // Calc next speed
        stepper.setSpeed(newSpeed);                         // Set speed
        while ( (millis()-oldMillis)<hold )
            stepper.runSpeed();                             // Run out 100 ms
        oldMillis = millis();                               // Reset time
    }
}

Thanks,
Voltage


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

@voltage

OK, can you please post the code you're testing. I'm afraid my version is out of date now and I need to see the code you're using.

This is it and has the 3 instances of rampSpeed remmed out. And the line you had me add (last line in this code):

// Reset speed if required
//
dia = map((analogRead(spd)),0,1023,50,1200); // Get diameter (x 100)
if (abs(dia-old_dia)>delta_dia) { // If diameter changed enough
calcSpeed(); // Calculate new speed
stepper.setSpeed(targetSpeed);

Thanks,
Voltage


ReplyQuote
Will
 Will
(@will)
Noble Member
Joined: 11 months ago
Posts: 1764
 

@voltage

Thanks, I've checked it over and everything looks OK, but I've made a few small adjustments. 

 

 

//Voltage Version sketch_aug06e
//Test of accelstepper with LCD and pot

//****************************************
//     sketch_aug06e-Mod-7
//      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 = -100, old_dia = 0, delta_dia = 5;  // Last pot reading
bool go = false;        //Is it running
float targetSpeed=0;    // Speed we want
float RPM;              // RPM for this diameter 
float dirn = 1;         // Direction +1 right, -1 left;
//
//  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
//
//      The preliminaries
//
void setup()
{  
    // Set mode for pins
    pinMode(LEFT_PIN, INPUT_PULLUP);
    pinMode(STOP_PIN, INPUT_PULLUP);
    pinMode(RIGHT_PIN, INPUT_PULLUP);

    // Set stepper attributes
    stepper.setMinPulseWidth(2.7);

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

//
//      The main event(s)
//
void loop()
{
    //
    //      Check buttons
    //
    if (switchPushed(STOP_PIN) && go) {
        rampSpeed(stepper.speed(),0);                       // Ramp speed down to zero
        go = false;                                         // No longer running
        stepper.stop();                                     // Stop stepper.
    }
    if (switchPushed(LEFT_PIN) && !go) {
        dirn = -1;                                          // Set direction
        go = true;                                          // We're running again
        old_dia = -100;                                     // Force speed recalc
    }
    if (switchPushed(RIGHT_PIN) && !go) {
        dirn = 1;                                           // Set direction
        go = true;                                          // We're running again
        old_dia = -100;                                     // Force speed recalc
    }
    //
    //      Reset speed if required
    //
    dia =  map((analogRead(spd)),0,1023,50,1200);           // Get diameter (x 100)
    if (abs(dia-old_dia)>delta_dia) {                       // If diameter changed enough
        calcSpeed();                                        // Calculate new speed
        stepper.setSpeed(targetSpeed);
        if (go) stepper.runSpeed();
        lcd.setCursor(5,0);                                 // Modify LCD to match
        lcd.print("      ");
        if (go) stepper.runSpeed();
        lcd.setCursor(5,0);
        lcd.print(dia/100.0);
        if (go) stepper.runSpeed();
        //
        lcd.setCursor(5,1);
        lcd.print("       ");
        if (go) stepper.runSpeed();
        lcd.setCursor(5,1);
        lcd.print(RPM);
        if (go) stepper.runSpeed();
        //
        old_dia = dia;                                      // Start over
    }
    //
    //      Business as usual
    //
    if (go) {
        stepper.runSpeed();
    } else {
        stepper.stop();
    }
}
//
//      Test if switch on pin is pressed
//
bool switchPushed( int pinNo )
{
    return (digitalRead(pinNo)==LOW);                       // Is pin LOW (i.e. grounded)
}
//
//      Calculate speed from dia and direction
//
void calcSpeed()
{
    dia =  map((analogRead(spd)),0,1023,50,1200);           // Get diameter (x 100)
    targetSpeed = dirn*finalSPE/dia;                        // Calc steps/sec for dia
    RPM = 60*targetSpeed/finalSPR;                          // Calc RPM    
}
//
//      Ramp speed up or down between specified speeds
//
void rampSpeed( float fromSpeed, float toSpeed )
{
    long            hold = 100;
    unsigned long   oldMillis = millis();
    float           delta = (toSpeed-fromSpeed)/10;
    float           newSpeed = fromSpeed;
    //
    //      Change speed in 10 steps of 100 ms each (i.e. over 1 second)
    //
    for (int i=0;i<10;i++) {
        newSpeed = newSpeed+delta;                          // Calc next speed
        stepper.setSpeed(newSpeed);                         // Set speed
        while ( (millis()-oldMillis)<hold )
            stepper.runSpeed();                             // Run out 100 ms
        oldMillis = millis();                               // Reset time
    }
}

 

 

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


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

@voltage

Thanks, I've checked it over and everything looks OK, but I've made a few small adjustments. 

 

 

//Voltage Version sketch_aug06e
//Test of accelstepper with LCD and pot

//****************************************
//     sketch_aug06e-Mod-7
//      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 = -100, old_dia = 0, delta_dia = 5;  // Last pot reading
bool go = false;        //Is it running
float targetSpeed=0;    // Speed we want
float RPM;              // RPM for this diameter 
float dirn = 1;         // Direction +1 right, -1 left;
//
//  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
//
//      The preliminaries
//
void setup()
{  
    // Set mode for pins
    pinMode(LEFT_PIN, INPUT_PULLUP);
    pinMode(STOP_PIN, INPUT_PULLUP);
    pinMode(RIGHT_PIN, INPUT_PULLUP);

    // Set stepper attributes
    stepper.setMinPulseWidth(2.7);

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

//
//      The main event(s)
//
void loop()
{
    //
    //      Check buttons
    //
    if (switchPushed(STOP_PIN) && go) {
        rampSpeed(stepper.speed(),0);                       // Ramp speed down to zero
        go = false;                                         // No longer running
        stepper.stop();                                     // Stop stepper.
    }
    if (switchPushed(LEFT_PIN) && !go) {
        dirn = -1;                                          // Set direction
        go = true;                                          // We're running again
        old_dia = -100;                                     // Force speed recalc
    }
    if (switchPushed(RIGHT_PIN) && !go) {
        dirn = 1;                                           // Set direction
        go = true;                                          // We're running again
        old_dia = -100;                                     // Force speed recalc
    }
    //
    //      Reset speed if required
    //
    dia =  map((analogRead(spd)),0,1023,50,1200);           // Get diameter (x 100)
    if (abs(dia-old_dia)>delta_dia) {                       // If diameter changed enough
        calcSpeed();                                        // Calculate new speed
        stepper.setSpeed(targetSpeed);
        if (go) stepper.runSpeed();
        lcd.setCursor(5,0);                                 // Modify LCD to match
        lcd.print("      ");
        if (go) stepper.runSpeed();
        lcd.setCursor(5,0);
        lcd.print(dia/100.0);
        if (go) stepper.runSpeed();
        //
        lcd.setCursor(5,1);
        lcd.print("       ");
        if (go) stepper.runSpeed();
        lcd.setCursor(5,1);
        lcd.print(RPM);
        if (go) stepper.runSpeed();
        //
        old_dia = dia;                                      // Start over
    }
    //
    //      Business as usual
    //
    if (go) {
        stepper.runSpeed();
    } else {
        stepper.stop();
    }
}
//
//      Test if switch on pin is pressed
//
bool switchPushed( int pinNo )
{
    return (digitalRead(pinNo)==LOW);                       // Is pin LOW (i.e. grounded)
}
//
//      Calculate speed from dia and direction
//
void calcSpeed()
{
    dia =  map((analogRead(spd)),0,1023,50,1200);           // Get diameter (x 100)
    targetSpeed = dirn*finalSPE/dia;                        // Calc steps/sec for dia
    RPM = 60*targetSpeed/finalSPR;                          // Calc RPM    
}
//
//      Ramp speed up or down between specified speeds
//
void rampSpeed( float fromSpeed, float toSpeed )
{
    long            hold = 100;
    unsigned long   oldMillis = millis();
    float           delta = (toSpeed-fromSpeed)/10;
    float           newSpeed = fromSpeed;
    //
    //      Change speed in 10 steps of 100 ms each (i.e. over 1 second)
    //
    for (int i=0;i<10;i++) {
        newSpeed = newSpeed+delta;                          // Calc next speed
        stepper.setSpeed(newSpeed);                         // Set speed
        while ( (millis()-oldMillis)<hold )
            stepper.runSpeed();                             // Run out 100 ms
        oldMillis = millis();                               // Reset time
    }
}

 

 

No go. It is basically the same. It does the two tics,a slight pause, then the 2 tics, and a pause etc. I left the room and came back a few minutes later and the stepper had rotated to the right about 1/2" with just the flag tape on the shaft.

 

Thanks,
Voltage


ReplyQuote
Will
 Will
(@will)
Noble Member
Joined: 11 months ago
Posts: 1764
 

@voltage

 

Damn !

OK, obviously I need to go back and modify the previous working version in more manageable steps. I'm out this afternoon.

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


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

@voltage

 

Damn !

OK, obviously I need to go back and modify the previous working version in more manageable steps. I'm out this afternoon.

Will, Ok, take a break. I'll check back but for today I am likely out as it's the wife's only day of the week that she is off. So I better hang out with her a little. 😉 Don't worry, tomorrow is another day. I will check in today to see if I can answer anything but I am going to have to move the project before supper. Tomorrow I have to walk the dog in the AM but will be available most of the day after that until late afternoon. Thanks for all the hard work.Talk to you later.

 

Thanks,
Voltage


ReplyQuote
MadMisha
(@madmisha)
Reputable Member
Joined: 2 years ago
Posts: 319
 

@voltage @will

Sorry I'm late to the party. Woke up to having to put out the proverbial fire. 

 

It sounds like you are setting the ramp up speed too low to start out with. That will damage the motor. Set the start speed at the minimum of what it need to start properly. If the rotation needs to be lower than that setting, it can be set down after that it is already moving(think about why fans start out on high as the first position of the switch). The motor needs a bit more oomph to get going sometimes.

 

I do worry that the stop button is not on an interrupt anymore. That is a call that you want immediate and not missable. The other buttons don't matter. I know you can't use millis there but ramping down the speed won't really do any thing. You'd just be adding more time the motor is used.

 

I have to go back to work. I just stopped by to tell you about something you actually caught on your own. It was the map going to 1200 but the max speed was still at 1000. It looks like it was cut but I will point out that it was included in the constant speed example. Reading the AccelStepper site, I would say that it should only affect when you call run() but that wasn't in the example so I really don't know. It does not say there is a default value either. I'm not sure if it should really be there or not.


ReplyQuote
Will
 Will
(@will)
Noble Member
Joined: 11 months ago
Posts: 1764
 

@madmisha @voltage

I have since eliminated the ramping, it can always be added in afterwards if needed.

Now that the STOP button doesn't use the ramp function, it eliminates the extra second it took to stop rotation. I have put the STOP button test at the top of the loop so that it's the first thing that's tested. The worst case delay if it is running should be one (full) loop if the diameter changes and the speed needs to be reset and only one step if diameter is unchanged. That delay shouldn't result in too much extra carnage before stopping.

I removed the limit because we're not accelerating, so we don't really need any upper bound any longer.

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


ReplyQuote
Will
 Will
(@will)
Noble Member
Joined: 11 months ago
Posts: 1764
 

@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;
    }
}

 

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


ReplyQuote
Page 16 / 21