Hey folks me again.
I've made some good progress on my project, right now I'm trying to index the stepper motor axis
This runs through a few steps.
1: If the stepper is on the switch, continue rotating until it is no longer on the switch
2. rotate until the switch is pressed another time
3. back off of the switch until it is no longer pressed
4. set the table is indexed.
I need to figure out how to interrupt it with if the "STOP" switch is pressed too at some point, Right now I was trying to track this by "do step 1 until the state is good to go, then set a variable to "i'm done" and step 2 picks up if the "i'm done" variable is set to its value
Now this sort of kind of works, it appears my IF statements aren't good enough and aren't capturing from the previous loop since it will jump to the bottom instead of to the next step (or so it appears).
I'm almost 100% sure this is a terrible way of going about this, I dont need the code rewritten just show me how I should be doing this kinda flow
Thanks folks!
// index procedure function
int indexfunction() {
int indexresult = 0; //indexresult vars: --0=unindexed --1=indexed --2=first stage done --3=second stage done 4= third stage done
int indexstate;
int estopstate;
indexstate = digitalRead(indexswitch);
estopstate = digitalRead(estopbutton);
while (indexresult == 0) {
while (indexstate == HIGH && estopstate != HIGH) { //Get off the switch if it is currently pressed
digitalWrite(indexmotor, HIGH);
delay(500);
digitalWrite(indexmotor, LOW);
indexstate = digitalRead(indexswitch);
estopstate = digitalRead(estopbutton);
lcd.setCursor(0, 0);
lcd.print("Index Stage 1 "); //display indexing stage
delay (200);
}
lcd.setCursor(0, 0);
lcd.print("Index St 1 done"); //display indexing stage
indexresult = 2;
}
while (indexresult == 2) { //Step 2, keep rotating until switch is presed again
while (indexstate == LOW && estopstate != HIGH) { //double loop for the variable
digitalWrite(indexmotor, HIGH);
delay(50);
digitalWrite(indexmotor, LOW);
delay(50);
indexstate = digitalRead(indexswitch);
estopstate = digitalRead(estopbutton);
lcd.setCursor(0, 0);
lcd.print("Index Stage 2 "); //display indexing stage
}
indexresult = 3; //after loop finishes check and set index stage
lcd.setCursor(0, 0);
lcd.print("Index St 2 done"); //display indexing stage
delay (200);
}
if (indexresult == 3) { //if index switch pressed and not estop and not indexed
while (indexstate == HIGH && estopstate != HIGH) { //do this until index is no longer pressed
digitalWrite(indexdir, HIGH); //rev stepper direction
digitalWrite(indexmotor, HIGH);
delay(50);
digitalWrite(indexmotor, LOW);
digitalWrite(indexdir, LOW); //untoggle stepper direction
indexstate = digitalRead(indexswitch);
estopstate = digitalRead(estopbutton);
lcd.setCursor(0, 0);
lcd.print("Index Stage 3 ");
delay (200);
}
if (indexstate == LOW && estopstate != HIGH) {
indexresult = 1; //after loop finishes check and set index stage
lcd.setCursor(0, 0);
lcd.print("Index Complete "); //display indexing stage
lcd.setCursor(2, 1);
lcd.print("P");
}
}
indexed = 1;
lcd.setCursor(0, 0);
lcd.print("Index exit "); //display indexing stage
}
//index function end