Stepper Motors, LCD, Pots, LED, Limit Switches, and Timeout function

I am currently making a new piece of equipment for my work. I'm making a gluing machine that uses a geared stepper motor to drive a lead screw. I have 4 potentiometers used to allow user interface to fine tune different variables (like the speed of the motor while gluing, adding additional steps in not enough glue is coming out, etc.). I then have the arduino displaying the potentiometer values on the lcd. These are all working, but if there is any advice on the coding for them I'd gladly listen.

The functions I want in the end product is to have a gluing button that moves down then backs up to relieve pressure off the tube of glue, a pulse button that allows the user to pulse small amounts of glue in needed areas, and an up and down button for obvious reasons. These I all have working for the most part. There are a few other functions that I want to implement and this is where I ran into a few problems. Again any additional advice is appreciated!

  1. Limit Switches:
    I want to put a limit switch at the top and bottom of the lead screw so that when the bottom limit switch is trigger the motor switches directions and moves up till it hits the top limit switch. I've seen a recent post but I don't quite understand how it pertains to my problem. I'm trying to achieve high tolerance for the amount of glue coming out, as it will be used for gluing parts for retail. As for my code now I only have the empty switch coded and I know its not right because it is an "if" function that the arduino checks once per loop. I'm using a 27:1 geared stepper motor so I'm trying to avoid crashing into the end because it will just destroy the 3d printed parts. so the limit switch must interrupt the code if it is pressed in the middle of the gluing process.

  2. Increasing RPM:
    with the 27:1 geared stepper 250 RPMS is rather slow moving with the lead screw, I know the stepper motor and driver is capable of higher rpms but I run into the problem of lack of knowledge of ramping up the rpms. I know you cant start a stepper at a high rpm because it will just vibrate, so I'm looking for a way to ramp up the rpms slowly when pressing the up or down button.

  3. There needs to be a "time out'" function, so if the glue/pulse/down button hasn't been pressed in say 1 min, a red led comes on and "locks" the code from gluing again, until a Purge button has been pressed to purge the semi hard glue out of the tube, then the red light turns off and the code goes back to the normal process.

I've seen a couple other posts that have talked about using unsigned long variables, updating the current time every loop cycle, using millis(), and updating the last button press time every time the button is pressed then at the bottom of the code it checks the (current - last button pressed) to see if its exceeded the "time out" limit. But there seems to be a problem with people disagreeing with how to go about this process. I know the millis() will roll over and go back to 0 when it reaches its limit, but this machine will be turned on and off frequently and only be used for a few hours at a time. I'm not sure if this is still relevant but i would like some help and input.

Things I'm using:
-Arduino Mega
-Kuman L298N motor Driver
Kuman L298N motor driver

-Nema 17 bipolar stepper motor 27:1 planetary gear
Nema 17 bipolar stepper motor 27:1 planetary gear

Limit switches are N.O.

Thank you,

Thomas

Code found bellow:

//Testing Buttons with LCD and New Motors
#include <Stepper.h>
#include <LiquidCrystal.h>

//setting up led
int greenLed = 52;
int redLed = 50;

// Setting up int for the control buttons
int glueButton = 53;
int pulseButton = 51;
int upButton = 49;
int downButton = 47;
int emptyLimitSwitch = 22;

// Setting up int for glueing motor
int in1Pin = 6;
int in2Pin = 5;
int in3Pin = 4;
int in4Pin = 3;
const int stepsPerRev = 200;
Stepper glueStepper (stepsPerRev, in1Pin, in2Pin, in3Pin, in4Pin);

// Setting up Constants for LCD
const int rs = 7, en = 8, d4 = 9, d5 = 10, d6 = 11, d7 = 12;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
 //Activating LCD letting the arduino know the LCD is 16 characters x 2 Rows
 lcd.begin(16, 2);

 //Activating stepper and setting pin mode
 pinMode(in1Pin, OUTPUT);
 pinMode(in2Pin, OUTPUT);
 pinMode(in3Pin, OUTPUT);
 pinMode(in4Pin, OUTPUT);

 pinMode(glueButton, INPUT_PULLUP);
 pinMode(pulseButton, INPUT_PULLUP);
 pinMode(upButton, INPUT_PULLUP);
 pinMode(downButton, INPUT_PULLUP);
 pinMode(emptyLimitSwitch, INPUT_PULLUP);

 pinMode(greenLed, OUTPUT);
 pinMode(redLed, OUTPUT);
 digitalWrite(greenLed, HIGH); //turning on green led
}

void loop() {
 //reading pot values and assinging value to a name, mapping the pots to chosen range
 int pulseValue = analogRead(A0);
 int pulseStep = map(pulseValue, 0, 1023, 0, 500);
 int glueStepValue = analogRead(A1);
 int glueStep = map(glueStepValue, 0, 1023, 0, 1000);
 int glueRpmValue = analogRead(A2);
 int glueRpm = map(glueRpmValue, 0, 1023, 0, 201);
 int rpmValue = analogRead(A3);
 int rpm = map(rpmValue, 0, 1023, 0, 5);

 //printing the pot values to the lcd screen and moving cursor and printing blanks to refresh the screen
 lcd.print("P=");
 lcd.setCursor(2, 0),
               lcd.print("   ");
 lcd.setCursor(2, 0);
 lcd.print(pulseStep);
 lcd.setCursor(6, 0);
 lcd.print("Gstp=");
 lcd.setCursor(11, 0),
               lcd.print("    ");
 lcd.setCursor(11, 0),
               lcd.print(glueStep);
 lcd.setCursor(0, 1);
 lcd.print("Grpm=");
 lcd.setCursor(5, 1),
               lcd.print("   ");
 lcd.setCursor(5, 1),
               lcd.print(glueRpm);
 lcd.setCursor(8, 1);
 lcd.print("RPM=");
 lcd.setCursor(12, 1),
               lcd.print("   ");
 lcd.setCursor(12, 1),
               lcd.print(rpm);
 lcd.setCursor(0, 0);

 //reading button values
 int glueButtonValue = digitalRead(glueButton);
 int pulseButtonValue = digitalRead(pulseButton);
 int upButtonValue = digitalRead(upButton);
 int downButtonValue = digitalRead(downButton);
 int emptyLimitSwitchValue = digitalRead(emptyLimitSwitch);

 //button code
 if (glueButtonValue == LOW)
 {
   glueStepper.setSpeed(glueRpm);
   glueStepper.step(-1500 - glueStep);

   glueStepper.step(800);
   delay(250);
 }

 if (pulseButtonValue == LOW)
 {
   glueStepper.setSpeed(glueRpm);
   glueStepper.step (-pulseStep);

 }

 if (upButtonValue == LOW)
 {
   glueStepper.setSpeed(275);
   glueStepper.step(500);
 }

 if (downButtonValue == LOW)
 {
   glueStepper.setSpeed(275);
   glueStepper.step(-500);
 }
 if (emptyLimitSwitchValue == LOW)
 {
   glueStepper.setSpeed(250);
   glueStepper.step(1000);
 }
}

Read the " how to use this forum-please read" sticky to see how to properly post code. You can edit your post to put the code in code tags.

The L298 motor driver is not suited for driving bipolar stepper efficiently. The L298 is ancient and power wasting technology. Consider the A4988 or DRV8825 drivers.

What does that code do? How is that different from what it should do?

Your post mentions many things. It may be better to concentrate on one thing at a time.

tdurand29:
I know you cant start a stepper at a high rpm because it will just vibrate, so I'm looking for a way to ramp up the rpms

There's a library specifically for this:
https://www.airspayce.com/mikem/arduino/AccelStepper/

tdurand29:
I've seen a couple other posts that have talked about using unsigned long variables, updating the current time every loop cycle, using millis(), and updating the last button press time every time the button is pressed then at the bottom of the code it checks the (current - last button pressed) to see if its exceeded the "time out" limit. But there seems to be a problem with people disagreeing with how to go about this process. I know the millis() will roll over and go back to 0 when it reaches its limit, but this machine will be turned on and off frequently and only be used for a few hours at a time. I'm not sure if this is still relevant but i would like some help and input.

If done as you just described, the roll over will cause no problems at all:

if(millis() - buttonPressTimestamp > timeOutDuration) {

Anyone saying anything different doesn't know what they're talking about and should be disregarded.

groundFungus:
Read the " how to use this forum-please read" sticky to see how to properly post code. You can edit your post to put the code in code tags.

The L298 motor driver is not suited for driving bipolar stepper efficiently. The L298 is ancien and power wasting. Consider the A4988 or DRV8825 drivers.

What does that code do? How is that different from what it should do?

Your post mentions many things. It may be better to concentrate on one thing at a time.

+1 to all those things - especially the last one.

It is much easier to develop a complex project if you break the program up into separate functions that can each be tested separately. Have a look at Planning and Implementing a Program

You may also find these links useful
Stepper Motor Basics
Simple Stepper Code

Several Things at a Time

...R

...R

tdurand29:

  1. Limit Switches:
    I want to put a limit switch at the top and bottom of the lead screw so that when the bottom limit switch is trigger the motor switches directions and moves up till it hits the top limit switch. I've seen a recent post but I don't quite understand how it pertains to my problem. I'm trying to achieve high tolerance for the amount of glue coming out, as it will be used for gluing parts for retail. As for my code now I only have the empty switch coded and I know its not right because it is an "if" function that the arduino checks once per loop. I'm using a 27:1 geared stepper motor so I'm trying to avoid crashing into the end because it will just destroy the 3d printed parts. so the limit switch must interrupt the code if it is pressed in the middle of the gluing process.

Yes it checks the limit switch once per loop but it checks the loop 1000's of times a second so it won't miss what you tell it to do unless you have blocking functions in your code i.e.. delay(), while statements etc.
Limit switches are just what they sound like, tell the motor to go to the limit switch and it will, it is usually done with an "if" statement and is very accurate. They are used in CNC machine's to calibrate them. A simple if statement saying if the limit switch is HIGH move to it and it will stop where ever you have the switch and then code to tell it what to do once it hits that switch. Something like this using Accelstepper library,

if (limitswitch == HIGH) {
gluestepper.move(50);
gluestepper.setSpeed(250);
}
if (limitswitch == LOW) {
gluestepper.setSpeed(0)
}

tdurand29:
2. Increasing RPM:
with the 27:1 geared stepper 250 RPMS is rather slow moving with the lead screw, I know the stepper motor and driver is capable of higher rpms but I run into the problem of lack of knowledge of ramping up the rpms. I know you cant start a stepper at a high rpm because it will just vibrate, so I'm looking for a way to ramp up the rpms slowly when pressing the up or down button.

You would be better off using the Accelstepper library for your stepper movements than the Stepper.h library. I f you are using limit switches you shouldn't have to worry about missing steps because the steppers will run until they hit the limit switches regardless of steps if you code it that way, which you should.

Is there a reason you chose that stepper motor, are you more concerned about the torque that it provides. I haven't used them before but in reading about them the do run slower than regular steppers and if you look at the spec sheet it has a very high inductance of 34mH which is going to be difficult to get much speed out of it.
The Accelstepper library has acceleration rates that you can play with to get more speed out of your motor and it is mostly trial and error with it but you can get more speed, the driver does play a big role in that speed increase as @groundFungus has stated the one you have could be improved upon.

Another thing that can help is I used a Mega for a project and traded it for a Due and the faster clock speed on the Due made for an increase in speed for the motors, the Due had some problems with having to solder a resistor onto it or else you had to hit the reset button every time you wanted to run your code but it was easily fixed. I would look into this if speed is just not what you want it to be and you want more.

Thank you for your help and guidance!

groundFungus and Robin2 sorry the improper post format I was trying to make a quick post to the forum to try and help me with my issue before I got off work. I believe I have made the proper changes to the post. Let me know if I have missed anything. Thanks for the driver advice as well!

pert and ribbonman, I didn't know there was an accelstepper library so I'll look into that to try and solve my problem with speed. It doesn't need to move very fast just a reasonable speed to unload and load different tubes of glue.

ribbonman, yes I chose 27:1 stepper for the torque, but I also like the the ability to move even slower than a normal stepper due to the need of high precision. I initial used a cheap nema 17 I bought off amazon and it wasn't supplying the torque or control I needed, but this could be due to that fact it was a cheap stepper.

Taking all your advice I'll focus on my first issue with the limit switches.

So the problem I have at hand is I don't know how to code the limit switches to stop all actions when pressed. The current plan is the user presses the glue button every time, to glue each individual part. When the glue button is pressed the stepper will turn moving the gluing plunger down a specific distance then back up a fraction of the distance to relieve pressure off the tube of glue, to prevent over extrusion. so the glue button will be pressed upwards to say 50 times, gluing 50 different parts, as it moves slowly down a 300mm lead screw. The problem I'm currently facing is when the user gets down to the end of the tube of glue, if there Isn't enough room for the plunger to move down one more full gluing cycle before it bottoms out it will crash into the bottom and destroy the 3D printed parts. There for I need a way to stop the stepper motor even if it is in the middle of the gluing cycle. Then return the plunger to the top limit switch.

Thank you all for your help!

In reference to this statement: "So the problem I have at hand is I don't know how to code the limit switches to stop all actions when pressed.".

I suspect you already have an idea about needing a universal "stop all" boolean variable. You must test it's state before doing every function.

Paul

Paul,

I don't quite know what you mean, I do need to figure out a way to do a stop all function. would that be similar to putting the whole code in a while (limitswitch != LOW) or is that just completely wrong and not how this works?

Thanks!