Servo keeps jumping to 0 degrees every now and then?

Hi everyone,

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.

What data type is the head variable and what exactly are you sending from Processing ?

The head is an int which ranges from 0-240 and is mapped in Processing to 0-180 (servo). This is send to the serial port like:

myPort.write(headMapped);

and received in Arduino like:

int head = Serial.read();

and then written to the servo like:

myservo.write(head);

All of this works but the servo keeps glitching and I'm not sure where it goes wrong.

It is time for you to post your full sketch, using code tags when you do

Have you tried printing what you receive so that you can determine whether the problem is caused by Processing or your Arduino sketch ?

And the issue could be power supply. Post a schematic. Post a few images of the project.

Powering the servos through a breadboard will cause issues.

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.

Ok, so blink an LED if a zero is received or better still, don't use the hardware Serial interface on pins 0 and 1 to communicate with Processing

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.

It probably tries to read when no characters are available. That returns -1. Did you check Serial.available() > 0 before trying to read?

NOTE: This question would have been answered already if you had posted your Arduino sketch.

An accident just waiting to happen.

Thank you for answering. Since I am new to this forum, I can't upload files, so I will just copy past my Arduino code for you. Maybe this helps.

#include <Servo.h>
#include <ServoEasing.hpp>

ServoEasing myservo; // head servo
Servo myservohand; // hand servo

char val;
int hoofd;
int hand = 0;
int talking = 0;
const int buzzer = 11;
int brightness;



void setup() {
  myservo.attach(6);
  myservohand.attach(10);
  Serial.begin(9600);
  pinMode(buzzer, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(8, OUTPUT);
}


void loop() {

  while (Serial.available()) {
    val = Serial.read();

    if (val == ';') {
      digitalWrite(2, HIGH);
      hoofd = Serial.read(); 
      myservo.easeTo(hoofd, 180);
      talking ++;
      delay(15);
    } else if (val == ':') {
      talking = 2;
      tone(buzzer, 800);
      delay(50);
      tone(buzzer, 1000);
      delay(50);
      noTone(buzzer);
      hand = Serial.read(); 
      myservohand.write(hand);
      brightness = map(hand, 0, 200, 0, 255);
      analogWrite(5, brightness);
      delay(15);
    } else if (val == '~') {
      digitalWrite(2, LOW);
    }

    if (talking == 1) {
      tone(buzzer, 466);
      delay(120);
      tone(buzzer, 466);
      delay(100);
      tone(buzzer, 523);
      delay(80);
      tone(buzzer, 587);
      delay(80);
      tone(buzzer, 622);
      delay(80);
      tone(buzzer, 698);
      delay(80);
      noTone(buzzer);
      talking ++;
    }

  }

  delay(15);

}

Hi could you maybe post your full sketch? Like also the Processing sketch? maybe there is something missing.

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 :wink:
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 :sunglasses:

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.

Thank you!! I had to change the while loop to:

while (Serial.available() > 1) // so it still has characters in the buffer when reading the next character. (shout-out to @johnwasser !)

This is such a relieve, the project is due tonight and finally works.
I really appreciate all the help :slight_smile:

Thank you, John!! This fixed the problem I had with the servo glitching!! I can't thank you enough, I really appreciate all the help :kissing_heart:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.