I'm communicating Processing (deepvisiod facial recognition) to an Arduino servo. The position of your face determines the position of the servo, which works. But every now and then the servo keeps glitching and jumps to 0 degrees, then back to the position it was given (position of your face). I printed the exact data that processing sends to Arduino but there's no 0 in it when it glitches. Does anybody know how I can fix this?
This is the code I use in Arduino to control the servo:
head = Serial.read();
myservo.write(head);
When I start the camera in Processing, I can't open the serial monitor in Arduino because port is busy. That's why I can't check if the data received on Arduino contains a 0.
Would love to hear from you if you have any ideas how to fix this.
The problem is that I can't open the serial monitor from Arduino because it its busy when I start Processing. I have to start processing otherwise it doesn't send any data to the serial which is received by Arduino
Its just 1 servo powered by the 5v from the Arduino Uno, connected to my laptop via usb. Haven't had this issue before, it used to work just fine. I have already tried other pins.
Thanks, I tried it with a LED. It doesn't blink so it isn't receiving a 0. Must be the power that messes with the servo then right?
Just changed if(val == 0){} to if(val <1){}
this time the LED does blink... So it receives a 0 I guess? I need to add some code to not make it able to go to 0.
A Uno is not able to supply power to a servo for reliable operations. Your setup is operating normally for using the Uno as a power supply for the servo.
This seems to be a problem for me.
At first I don't see why you should use a while loop - loop() is already loop - as the name advices
On the other hand: Serial.available will get true at the first received character ( assume the ';' here ). Than you again call Serial.read() without checking wether the next character has arrived already. Serial communication is very slow compared to the processor speed. It's very likely that the next character has not been received yet, und you will get -1 as the result.
And it seems that you mix characters and binary values at the serial line. How do you distinguish between each other?
By the way, this is the correct usage. You should not upload a sketch as file but always directly insert in your post - well done with code tags
You are checking for (Serial.available() != 0) meaning one or more characters are available, and then you read two characters. Change the line to: while (Serial.available() > 1) {
That way there will always be at least two characters in the buffer when you read the first character.