I've built 2 motor drivers for a robotics project and need to set positions that I want the motors to move to, and read that using the arduino. The arduino then uses a rotary encoder to implement a closed loop PID controller and move the motor the set point. My code wasn't working so I started troubleshooting and found out that my setpoint was getting reset to 0 automatically!
The only thin that changes the setpoint was the serial read so I made a smaller sketch for troubleshooting which is this:
volatile long setpoint; void setup() { Serial.begin(9600); } void loop() { if (Serial.available()){ setpoint = Serial.parseInt(); Serial.print("Set to: "); Serial.println(setpoint); } } The result was quite odd, it sets the value correctly but resets back to 0 without me sending any new commands!
I tried updating the IDE to 1.8.12, tried old and new arduinos (with old and new bootloader) but all had the same result. Here is a demo video showing the weird reset to 0 problem:
I tried changing the buadrate higher or lower but that also didn't fix anything. Any ideas on what I'm doing wrong?