So I am doing a project which is to get a servo motor moving by doing serial communication with python. However, the while/if (mode == 2) {...} statements do not work after the variable mode is changed to 2 for the second time (and so on). Note that the while/if (mode == 1) statements works fine.
I have tried everything I have think of, for example using an LED and a serial monitor to check the values of the variable.
These are the arduino and python codes (I omitted everything that works fine):
Arduino code:
void loop() {
// get input for mode
if (mode == 0) {
digitalWrite(LED_BUILTIN, LOW);
// Serial.println("mode?");
while (Serial.available() > 0) {
String sel = Serial.readString();
mode = sel.toInt();
lcd.clear();
lcd.setCursor(0,0);
lcd.print(mode == 2); // it showed "1" when I typed 2 in python
if (mode == 1 || mode == 2) {
break;
}
}
}
// Codes for manual control
// run once when mode = 1
if (mode == 1) {
...
}
// run as long as button isnt pressed
while (mode == 1) {
...
}
if (mode == 2) {
// this works the first time, but when mode is switched back to 0 then switched to 2 this does not work
digitalWrite(LED_BUILTIN, HIGH);
}
// run repeatedly when mode = 2
while (mode == 2) {
// get linear position from serial monitor
while (Serial.available() > 0) {
String selpos = Serial.readString();
pos = selpos.toInt();
if (pos == 0 || (pos >= 33 && pos <= 56)) {
break;
}
}
// exit auto mode if input is 0
if (pos == 0) {
myservo.write(0);
mode = 0;
}
// calculate angular position of the servo, and turn it into the specified linear position
if (pos >= 33 && pos <= 56.5) {
pos = (pos - 33) * 7.6433;
myservo.write(pos);
// delay(10000);
while (1) {
// Read the button state
int btnState = digitalRead(SW);
// If we detect LOW signal, button is pressed and exit manual control mode
if (btnState == LOW) {
// Remember last button press
lastButtonPress = millis();
delay(100);
// if 50ms have passed since last LOW pulse, it means that the
// button has been pressed, released and pressed again
if (millis() - lastButtonPress > 50) {
// Serial.println("Retracted");
Serial.println("8");
myservo.write(0);
pos = 1;
break;
}
}
}
}
}
}
And the python code is:
while 1:
# get user input for mode
while mode == "0":
print("Input mode. (1: Manual, 2: Auto)")
selection = input()
while selection != "1" and selection != "2":
print("Invalid input, please try again. (1: Manual, 2: Auto)")
selection = input()
mode = selection
ser.write(mode.encode())
break
# code for manual control
if mode == "1":
...
while mode == "1":
...
# code for automatic control
while mode == "2":
print("Input distance (33-56mm). Input 0 to exit auto mode.")
pos = input()
while (int(pos) < 33 or int(pos) > 56) and int(pos) != 0:
print("Invalid input, please try again (33-56mm). Input 0 to exit control mode")
pos = input()
# write selection to serial
ser.write(pos.encode())
if pos == "0":
mode = "0"
pos = "1"
break
if int(pos) >= 33 and int(pos) <= 56:
print("Extended to required distance. Press to retract.")
while int(pos) >= 33 and int(pos) <= 56:
exit_2 = int.from_bytes(ser.readline(), "little")
# print(exit_2)
if exit_2 == 658744:
pos = "1"
break
Any helps and questions are appreciated, thanks!