So I'm trying to control two stepper motors. Currently working on getting the first one to move X distance when pressed. I can get it to move continuously but I only want it moving so far.
So I placed it within an 'if' statement and now it only does one microstep. I thought if I set the thing to run within an if statement it would carry out X number of steps. Please help, this is for a big project due tomorrow.
You'll see the bit in satpos == 1
#include <LiquidCrystal.h>
#include <Stepper.h>
const int stepsPerRevolution1 = 200;
const int stepsPerRevolution2 = 200;
LiquidCrystal lcd(1, 2, 4, 5, 6, 7);
Stepper StepperElev(stepsPerRevolution1, 22, 24, 26, 28);
Stepper StepperBase(stepsPerRevolution2, 23, 25, 27, 29);
const int nextbutton = 41;
const int backbutton = 40;
int buttonstate = 0;
int buttonstate2 = 0;
int lastbuttonstate = 0;
int lastbuttonstate2 = 0;
int satpos = 0;
int satdirection = 0;
int hasbeendone = 1;
void setup()
{
lcd.begin(16, 2);
lcd.clear();
pinMode (nextbutton, INPUT);
pinMode (backbutton, INPUT);
}
void loop()
{
buttonstate = digitalRead(nextbutton);
if (buttonstate != lastbuttonstate) {
if (buttonstate == HIGH) {
satpos++;
satdirection = 0;
hasbeendone = 0;
}
delay(50);
}
lastbuttonstate = buttonstate;
buttonstate2 = digitalRead(backbutton);
if (buttonstate2 != lastbuttonstate2) {
if (buttonstate2 == HIGH) {
satpos--;
satdirection = 1;
hasbeendone = 0;
}
delay(50);
}
lastbuttonstate2 = buttonstate2;
if (satpos == 0) {
lcd.clear();
lcd.print ("SAT1: Astra 28:E");
lcd.setCursor(0, 1);
lcd.print ("Back Next");
delay(100);
}
if (satpos == 1) {
lcd.clear();
lcd.print ("SAT2: Astra 28:F");
lcd.setCursor(0, 1);
lcd.print ("Back Next");
delay(100);
if (hasbeendone == 0) {
StepperElev.step(stepsPerRevolution1);
hasbeendone = 1;
}
}
if (satpos == 2) {
lcd.clear();
lcd.print ("SAT3: Astra 28:G");
lcd.setCursor(0, 1);
lcd.print ("Back Next");
delay(100);
}
if (satpos == 3) {
lcd.clear();
lcd.print ("SAT4: Astra 1:N");
lcd.setCursor(0, 1);
lcd.print ("Back Next");
delay(100);
}
if (satpos == -1){
satpos = 3;
}
if (satpos == 4){
satpos = 0;
}
}