I have a program where I am asking several stepper motors to run and perform various actions (like a CNC machine). I have three limit switches for three different motors. In theory, the program will never drive a motor into a switch, but since I am writing each program from scratch, if I mistype or measure a distance incorrectly, a motor will run to far and hit a switch, and probably damage some parts, and at that point, I want it to stop everything.
I already plan on using a open ended statement to get the program to pause and wat for user input:
while (Start==0){
Start=customKeypad.getKey();
}
//Program pauses and waits for user to press "1" on the keypad.
So my questions is: Can I wrap the entire program (basically almost everything that happens in the void loop) in a while loop where it will only operate as long as all three limit switches are returning a "HIGH"?
Here is the code:
int LimitP= 2;
int LimitX= 3;
int LimitY= 4;
int buzzer = 5;
char Start=0;
//I have not included all the motor pins, motros set up, lcd etc... in this code to make it cleaner.
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(LimitP, INPUT_PULLUP);
pinMode(LimitX,INPUT_PULLUP);
pinMode(LimitY,INPUT_PULLUP);
pinMode(buzzer,OUTPUT);
delay(1000); //Setuptime
}
void loop() {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("READY");
lcd.setCursor(0,10);
lcd.print("Press 1 to start");
Serial.println("Press 1 to start");
while (Start==0){
Start=customKeypad.getKey();
}
//Program pauses and waits for user to press "anykey" on the keypad to start the program.
//This keeps it from automaticaly running a soon as it has power.
int p=digitalRead(LimitP);
int x=digitalRead(LimitX);
int y=digitalRead(LimitY);
delay(5);
while(p==HIGH && x==HIGH && y==HIGH){
int p=digitalRead(LimitX);
int x=digitalRead(LimitX);
int y=digitalRead(LimitX);
//MY CODE FOR MOTORS GOES HERE>>>>>>>>>>
//>>>>>>>>>>
// Etc for the entire code>>>>>>>>>>>>>>
}
//If the while loop is broken, then a buzzer will soundtwice
//and it will await user input to prevent the code from looping again
tone(buzzer,1900,100);
delay(1000);
noTone(buzzer);
delay(1000);
tone(buzzer,1900,100);
delay(1000);
noTone(buzzer);
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("LIMIT SWITCH HIT");
Serial.println("Press 1 to start");
lcd.setCursor(0,1);
lcd.print("PROGRAM ABORT");
Serial.println("PROGRAM ABORT");
lcd.setCursor(0,2);
lcd.print("Press 1 to start");
Serial.println("Press 1 to start");
while (Start==0){
Start=customKeypad.getKey();
}
//Program pauses and waits for user to press "any key" on the keypad.
}