Hey all,
I am trying to automate a fixture at my job using an Arduino, sainsmart motor shield, and NEMA 17 stepper motor.
When I run the code, the motor turns continuously, no matter which button I press or which state the if statement is set to.
I've attached my code. Could it have something to do with the while statement I have inside the if clause?
const int start = A15; // Normally HIGH
const int safe = A12;
const int overTravel = A13;
const int testSwitch = A10;
//E-Stop is hooked into power directly
//all single pins set
int startState = 0;
int safeState = 0;
int overTravelState = 0;
int testSwitchState = 0;
//these variables are set to low. They are used as logic indicators for High/Low readings.
#include <AFMotor.h>
AF_Stepper testStepper(200, 2); // set 1.8 degree, 4 pin stepper motor.
// stepper set
#include <LiquidCrystal.h> //LCD set to library.
LiquidCrystal lcd(51, 53, 43, 45, 47, 49);
// LCD Display set.
void setup() {
lcd.begin(16,2);
lcd.print("Unit On");
//Greeting
pinMode(start, INPUT);
pinMode(safe, INPUT);
pinMode(overTravel, INPUT);
pinMode(testSwitch, INPUT);
//these influence button states.
testStepper.setSpeed(10); //motor swt to 10rpm
}
void loop() {
startState = digitalRead(start);
safeState = digitalRead(safe);
overTravelState = digitalRead(overTravel);
testSwitchState = digitalRead(testSwitch);
if(startState == LOW); // start test function
{ lcd.clear();
while(safeState == HIGH)
{testStepper.step(1, BACKWARD, SINGLE);
}
int timeA = micros(); //timeA equals time before motor movement
while(testSwitchState == HIGH)
{testStepper.step(1, FORWARD, SINGLE);
}
int timeB = micros();
int time1 = timeB-timeA;
//time1 done
//end test function
int time = (time1);
int transferHeight = ((time)*(.0000000167)*(60)*(.025)); // ((microseconds)*(min/m.s.)*(rpm)*(in/rev))=inches
if(.05<transferHeight<.1)
{lcd.setCursor(0,0);
lcd.print("Good Unit");
lcd.setCursor(0,1);
lcd.print(transferHeight);
}
else
{
lcd.setCursor(0,0);
lcd.print("Bad Unit");
lcd.setCursor(0,1);
lcd.print(transferHeight);
}
}
}
I appreciate any help you can provide. If I have been unclear about something feel free to ask and I'll try to be more explanatory.
Thanks,
Nicholas
