Notifications
Clear all

Arduino with Stepper and LiquidCrystal Issue

Page 15 / 21

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

@voltage @madmisha

I don't know what time I'll be available tomorrow, but as a consolation prize here's a sub to ramp speed up or down. It changes the speed from fromSpeed to toSpeed in 10 equal steps over 100 ms each for an overall time of 1 second.

I think this should minimize any possibility of damage from inertia and it can be used in the STOP  and both directional buttons. 

 

 

//
//      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: 11 months ago
Posts: 169
Topic starter  
Posted by: @will
Posted by: @voltage

Madmisha:

The buttons can be whatever as I am going to build a new control enclosure.

The LCD shield would have to be wired for the button pushes if used as that would not be available through the cover of the enclosure but I think you know that. 🙂

I will be reading the forum after 9:30 AM CST but will be doing backups and eating breakfast until 11:30 and be ready to rumble at 12:00 Noon CST.

Thanks Madmisha!

🙂

If we agree that LEFT and RIGHT will only work when the machine is stopped, then we won't need a start button. Stop will just stop the machine and the operator will use the LEFT or RIGHT button to start it running in the desired direction.

@madmisha, I'm wondering, if we have such crisp meanings for each button, will we really need to de-bounce the switches ? I mean, if we sense the STOP button pushed we're just going to stop, we don't have to worry about it bouncing on and off. Similarly if the machine is stopped and somebody pushes a direction button - we're just going to start moving that direction whether the switch bounces or not.

We could "slow to a stop" after the button is pushed by gradually decreasing the stepper speed. In a similar way we could "ramp up" to start by gradually increasing the stepper speed from 0 to the calculated rate. Both of these could take place over a second or two each and should take a good amount of stress off the stepper.

The slow down could be incorporated into the STOP button code and the ramp up could be built into the LEFT & RIGHT button code.

Does this sum everything up correctly ? Use Voltage's custom enclosure (to be designed), use shield buttons if possible wired to enclosure buttons, STOP just stops, LEFT and RIGHT only work when machine stopped and will start up in desired direction, Stop may involve gradual reduction in speed to zero, LEFT, RIGHT may involve gradual spin-up to pot indicated speed.

Just so we all start with the same vision and assumptions 🙂

I would agree to the Left Right and Stop. With my other unit I added 3 ports to connect foot pedals to remotely control it too. I would only use the Stop and one direction connected at a time.

 

Thanks,
Voltage


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

@voltage @madmisha

One other question Voltage while I think of it, would you prefer using a potentiometer to dial in diameter or would you prefer using buttons to do so ?

The potentiometer is fine.

 

Thanks,
Voltage


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

@voltage @madmisha

I don't know what time I'll be available tomorrow, but as a consolation prize here's a sub to ramp speed up or down. It changes the speed from fromSpeed to toSpeed in 10 equal steps over 100 ms each for an overall time of 1 second.

I think this should minimize any possibility of damage from inertia and it can be used in the STOP  and both directional buttons. 

 

 

//
//      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
    }
}

 

 

Looks good. Nice consolation prize. 🙂

Thanks,
Voltage


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

@voltage @voltage

Voltage, as I was trying to get to sleep last night I started wondering about the shield. To use the buttons you'd have to mount it in the control panel with the LCD and buttons exposed. This brought up 2 questions ...

1) I've never used a TIG welder but arc welders used to spit out hot balls of melted metal, smoke and little balls of slag when brushed clean. My gloves used to get filthy with the residual muck and metal filings from when I need to grind the surface. Would the exposed shield stay clean and dirt free in the workshop environment being touched by your work gloves all the time ?

2) I always used to use heavy leather gloves while welding and there's no way I could work those small buttons one at a time with gloves on. If you wear gloves, could you put them on and see if you can easily press each of the buttons (only one at a time).

 

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 @madmisha

Here's a new testable version which eliminates the interrupt for the buttons, ramps speed up and down, processes the buttons as we discussed and handles all of the buttons inline in the loop.

I've left it using standard buttons as I haven't heard from Voltage about whether the shield buttons are going to be suitable in use in the shop.

 

 

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

//****************************************
//     sketch_aug06e-Mod-7
//
//      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 PCD 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
        old_dia = 0;                                        // Force spped recalc
    } else if (switchPushed(RIGHT_PIN) && !go && dirn !=1) {
        dirn = 1;                                           // Set direction
        go = true;                                          // We're running again
        old_dia = 0;                                        // Force spped 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
        stepper.runSpeed();
        targetSpeed = dirn*finalSPE/dia;                    // Calc steps/sec for dia
        RPM = 60*targetSpeed/finalSPR;                      // Calc RPM
        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)
}
//
//      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
Will
 Will
(@will)
Noble Member
Joined: 12 months ago
Posts: 1780
 

@voltage @madmisha

 

Sorry, the sketch above is incomplete, here's the version to test

 

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

//****************************************
//     sketch_aug06e-Mod-7
//
//      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;                                          // 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;                                           // 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
        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: 11 months ago
Posts: 169
Topic starter  
Posted by: @will

@voltage @voltage

Voltage, as I was trying to get to sleep last night I started wondering about the shield. To use the buttons you'd have to mount it in the control panel with the LCD and buttons exposed. This brought up 2 questions ...

1) I've never used a TIG welder but arc welders used to spit out hot balls of melted metal, smoke and little balls of slag when brushed clean. My gloves used to get filthy with the residual muck and metal filings from when I need to grind the surface. Would the exposed shield stay clean and dirt free in the workshop environment being touched by your work gloves all the time ?

2) I always used to use heavy leather gloves while welding and there's no way I could work those small buttons one at a time with gloves on. If you wear gloves, could you put them on and see if you can easily press each of the buttons (only one at a time).

 

Thanks

Will, TIG welding is very neat and leaves no spatter or mess. MIG and Stick do. I can weld all 3 versions along with Brazing but I prefer TIG. That said, I mentioned in a previous post yesterday to Madmisha that using the buttons directly on the shield would not work unless wires could be soldered and then connected to switches. So definitely NO WAY on using the shields buttons for this. I will be ready to test the new code in about 30 minutes after my wife finishes the dishes and gets out of the way. 😋 

 

p.s. I like your new avatar. Even a caveman could do it! 🙂

 

Thanks,
Voltage


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

@voltage

 

Sorry about the premature version of the code, I was working from a page of notes and forgot that there were more changes on the next page ... oops 🙂

Since the speed ramps up for LEFT and RIGHT buttons over a period of 1 second, when you're testing you'll have to let the speed build up before starting to measure the rotation time.

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

 

Sorry about the premature version of the code, I was working from a page of notes and forgot that there were more changes on the next page ... oops 🙂

Since the speed ramps up for LEFT and RIGHT buttons over a period of 1 second, when you're testing you'll have to let the speed build up before starting to measure the rotation time.

Will, with the last version of the code, I tried to run it and I get zero movement of the stepper. I checked and all the wires are the same as you made no changes from the last working code. I swapped the left and right wire as that was the only thing I could find but that is irrelevant. I think I may hear a small click, click but that is it.

 

edit: I do hear a quiet noise and the stepper looks like it may be moving a thousandth's of an inch when it goes tic toc, tic toc, tic toc...etc.

 

Thanks,
Voltage


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

@voltage

I think that's because I recalculate the speed but never set it. In the loop where it tests for a difference in dia and old_dia, add the line stepper.setSpeed(targetSpeed); right after calcSpeed();

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

I think that's because I recalculate the speed but never set it. In the loop where it tests for a difference in dia and old_dia, add the line stepper.setSpeed(targetSpeed); right after calcSpeed();

There are 3 instances of calcSpeed(); so I tried the third one down and it doesn't fix it. Did I choose the wrong place to add this line?

// 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(" ");

 

Thanks,
Voltage


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

@voltage

I think that's in the right place, so it may be the rampSpeed sub isn't working (it's new code as well). Try putting // in front of all calls to rampSpeed to see if it works. they all occur in the section starting

//

//  Check buttons

//

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

I think that's in the right place, so it may be the rampSpeed sub isn't working (it's new code as well). Try putting // in front of all calls to rampSpeed to see if it works. they all occur in the section starting

//

//  Check buttons

//

I did this and tried again and now nothing, not even the tic toc noise.

 

//
// 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
//

 

Thanks,
Voltage


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

@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.

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


ReplyQuote
Page 15 / 21