I've been trying to get a code working for a project I am working on involving the control of 2 servo's and a relay. I have been working with a bluetooth module and an app built with the MIT app inventor 2 to control these devices from an app on my android. I actually had this code working before but i lost the code and now I can't for the life of me figure out what's wrong with the rewrite!
The app sends a 2 byte number to the bluetooth module. In order to differentiate the 2 servo's, I've made one servo be controlled by the numbers 1000-1180 and the other from 2000-2180.
The problem is that the void loop doesn't seem to be running properly. Every time the code runs, it runs from the beginning. Also, it doesn't seem to be accepting and processing the number that the bluetooth is receiving properly. When the number is sent from the app, say 1090, the code should go to the void loop and execute the if loop for the 1000-1180 servo and turn that servo to 90 degrees.
When i tried some testing myself, i put in initial conditions for the servo positions and tried to get the relay working along with it, but every time i tried to command a single servo, multiple things would happen. the relay might turn on, the servo would turn to the wrong angle, etc.
So if you can see anything apparently wrong with the code or would like any additional info on the issue, I would love some comments and can provide more if need be.
I got the idea for the code from another location - i understood that servopos would read the first byte and servopos1 would read the second byte, that is why the calculation takes place. How would I go about it if this way is not appropriate?
Okay but i'm only sending 2 byte numbers so that wouldn't matter. if it is >0 and I'm only sending 2 byte numbers it will work the same. I changed it anyways, but changing this and the names won't solve my problem. Do you have any feedback as to how to make the program actually work or are you here just to comment on the aesthetics of the code?
It's true that you cannot assume two bytes are ready. If there is only one ready you get all out of sync. How about only reading one byte at a time and acting when you have read two bytes?