Trying a "State Machine" for Servo Control

ctmal

Supporting Member
I also took a look at your code again. I think there's something wrong in your header detection.

I don't think you want the for loops in your header detection clauses. Right now, It looks for the '~' and if it doesn't see it, it breaks the for loop. Which means its falling through. Same thing for the '!'.

So, I think the flow you want is:
Wait for data to be present (while (!SerialBT.available());
Test if it's a '~' and restart the while loop if its not because its not the start of a data packet. So, if (SerialBT != '~') continue; Continue restarts the while loop.
Otherwise, it was a ! so test for the '!' next the same way you did for the ~. If its not the ! its not the start of a packet so you need continue to restart the while loop.
If both tests pass, loop through the remaining data for your channelvalues. In the case of 1 servo, its only 1 iteration.

note my code might not be exactly correct, I did it in the browser, not an ide so don't just copy and paste.
 
Back
Top