Hi,
Newbie here.
I'm trying to build a linear motion module by using start stop button, stepper motor and sensors.
I have a problem whenever I call a void goBackHome function, take me back to the initial state of void loop where I can re-press the start button and the cycles started again.
Below is the code I have so far, (still under development and a lot of thing to be asked ).
Appreciate if somebody could help.
void loop() {
digitalWrite(laser_pin, HIGH);
int onState = digitalRead(start_pin);
if (onState == 1) { // Check if on button is pressed
if (isBusy == 0){ // Check if motor is busy (at work)
stepperX.setCurrentPosition(0); // Set the current position
stepperX.setMaxSpeed(1000.0); // Set Max Speed of Stepper (Faster for regular movements)
stepperX.setAcceleration(1000.0); // Set Acceleration of Stepper
isBusy = 1; // Set the motor to be busy (measuring is on going)
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Measuring... "); // Inform user that a measurement is on going)
// Loop to each measurement point
for (int mtrPos = 0; mtrPos < stpCount; mtrPos++) {
stepperX.moveTo(measPoint[mtrPos]);
while(stepperX.distanceToGo() != 0) { // While target position isn't reached up
stepperX.run(); // Move Stepper into position
}
digitalWrite(enable_pin, !digitalRead(enable_pin));
digitalWrite(laser_pin,!digitalRead(laser_pin));
delay(3000); // Conduct measurement at the position
digitalWrite(laser_pin,!digitalRead(laser_pin));
digitalWrite(enable_pin, !digitalRead(enable_pin));
} // Move to next measurement point/ measurement completed
lcd.setCursor(0,0);
lcd.print("Completed... ");
goBackHome(); // Call motor to go back home
//isBusy = 0; // Set motor as not busy
} // End if isBusy
}
int offState = digitalRead(stop_pin); // Check if on button is pressed
if (offState == 1) { // Check if on button is pressed
//isBusy = 0; // Set motor as not busy
lcd.setCursor(0,0);
lcd.print("Cancelled... ");
goBackHome();
}
}
void goBackHome(){
digitalWrite(laser_pin,!digitalRead(laser_pin));
stepperX.setMaxSpeed(1000.0); // Set Max Speed of Stepper (Faster for regular movements)
stepperX.setAcceleration(1000.0); // Set Acceleration of Stepper
stepperX.moveTo(-0); // Set target position to home position
TravelX = stepperX.distanceToGo();
while(TravelX != 0) { // While target position isn't reached up
if (TravelX >= -500 && TravelX < -50) { // Check if about to reach target position
stepperX.setMaxSpeed(500.0); // Slow down to set slower speed to get better accuracy
stepperX.setAcceleration(500.0);
}
if (TravelX >= -50) { // Check if about to reach target position
stepperX.setMaxSpeed(100.0); // Slow down to set slower speed to get better accuracy
stepperX.setAcceleration(100.0);
}
stepperX.run(); // Move Stepper into position
}
isBusy = 0;
loop();
//return true;
}