I hope someone can tell me what is wrong with this line of code;
if((Mode_X ++) > 2) Mode_X = 1;
This is the do loop code
do{
Busy_LED_On;
// while(digitalRead(CW_Home_Pin) == true){
debug_IF_Delay(Pulse_Delay_Val[x][Mode_X][1]);
Reset_Count;
do{
StepPinToggle;
Delay_Check(Pulse_Delay_Val[x][Mode_X][1]);
// }while(digitalRead(CW_Home_Pin) == true);
}while((INC_Count) <= (Angle_180_Count));
Busy_LED_Off;
if((x += digitalRead(DIR_Out_Pin)) > 9){
if((Mode_X ++) > 2) Mode_X = 1;
x=0;
}
digitalWrite(DIR_Out_Pin,!digitalRead(DIR_Out_Pin));
debugF("\tMode_X >> ");
debugln(Mode_X);
debugF("\tx >> ");
debugln(x);
debuglnF("*****************");
delay(2500);
// while(digitalRead(CW_Home_Pin) == false){
// while(INC_Count < false){
// StepPinToggle;
// Delay_Check(Delay);
// }
debug_SerialWait;
}while(true);
I know the if statement works because Mode_X count increases from 2 to 3 instead of setting Mode_X to 1!!!!
I'm guessing it's because of the post increment of Mode_X. Maybe you need a pre increment here? Just a guess....
LarryD
October 5, 2024, 5:48pm
3
Try:
Mode_X++;
if(Mode_X > 2)
{
Mode_X = 1;
}
If Mode_X can only have values 1 and 2, then either use a conditional, or a modulus. Sorry, 82yo autistic mind shooting from the hip, might be useful or not.
Obviously not. You are saying "Increment Mode_X" and then the rest is a syntax error.
What it should be is
if((Mode_X ++) > 2) {
Mode_X = 1;
}
No syntax error.
The brackets are optional if there is only one statement to execute, and it is not necessary to put the statement on a separate line from the if statement.
Their is no problem with the line of code, just the posters understanding of what it does.
if((Mode_X ++) > 2) Mode_X = 1;
First the conditions (Mode_X > 2 ) is evaluated, then Mode_X is incremented.
xfpd
October 5, 2024, 7:38pm
7
Mode_X increments after the comparison to 2.
Compile this.
int Mode_X = 1;
void setup() {
Serial.begin(115200);
if ((Mode_X ++) > 2) Mode_X = 1;
Serial.println(Mode_X);
}
void loop() {}
Check this out, run it and watch serial. I know it's 0,1 where you want 1,2 but that is left as an exerc ise for the reader.
int Mode_X = 1;
int BadMode = 1;
void setup() {
Serial.begin(115200);
}
void loop() {
Mode_X = (Mode_X + 1) % 2;
BadMode = (BadMode++) % 2;
Serial.print("Mode_X="); Serial.println(Mode_X);
Serial.print("BadMode=");Serial.println(BadMode);
delay(2000);
}
I can't believe it, just change it to ++Mode_X so the incremented value is tested.
Did you notice you have Mode_X ++ instead of Mode_X++. But in reality it needs to be ++Mode_X increment then test.
Ok problem is fixed with this line of code
if( ++Mode_X > 2) Mode_X = 1;
Before I let you all go. May I be so bold as to ask one more question. What is the difference between Mode_X++ and +++ Mode_X?
Thanks everyone for sharing your great wisdom with me.
sonofcy
October 6, 2024, 12:21am
12
Don't forget to mark my post as Solved.