Servo glitches caused by Processing

I have a little weirdness here. I do not know where this spasm is coming from. The servo moves are clean but the button pushes spasm almost every time. If I send data from the Serial monitor it is ok. Any ideas where this might be coming from?

// Arduino Code

#include <Servo.h>

Servo Servo_1;

Servo Servo_2;

void setup() {

////////////////////////////////////////////////////////////////////

pinMode(13, OUTPUT); //set pin as output , blue led

pinMode(11, OUTPUT); //set pin as output , red led

pinMode(12, OUTPUT); //set pin as output , yellow led

Serial.begin(9600); //start serial communication @9600 bps

////////////////////////////////////////////////////////////////////

pinMode(1,OUTPUT);

Servo_1.attach(5); //analog pin 0

Servo_2.attach(6); //analog pin 1

Serial.begin(115200);

Serial.println("Ready");

}

void loop() {

manualservomove();

buttons();

}

//////////////////////////////////////////////////////

void manualservomove()

{

static int v = 0;

if ( Serial.available()) {

char val = Serial.read();

switch(val) {

case '0'...'9':

v = v * 10 + val - '0';

break;

case 'a':

Servo_1.write(v);

Serial.println(v);

v = 0;

break;

case 'b':

Servo_2.write(v);

Serial.println(v);

v = 0;

break;

}

}

}

////////////////////////////////////////////////////////

void buttons(){

if(Serial.available()){ //id data is available to read

char val = Serial.read();

if(val == 'r'){ //if r received

digitalWrite(13, HIGH); //turn on red led

}

if(val == 'l'){ //if b received

digitalWrite(13, HIGH); //turn on blue led

}

if(val == 'y'){ //if y received

digitalWrite(13, HIGH); //turn on yellow led

}

if(val == 'f'){ //if f received

digitalWrite(11, LOW); //turn off all led

digitalWrite(12, LOW);

digitalWrite(13, LOW);

}

}

}

///////////////////////////////////////////////////////////


////////Processing code/////////////////////////////

import processing.serial.*;

import controlP5.*;

ControlP5 cp5;

int pan = 10;

int tilt = 10;

Serial port;

///////////////////////////////////////////////////////

//ControlP5 cp5; //create ControlP5 object

PFont font;

////////////////////////////////////////////////////////

void setup() {

size(900,900);

noStroke();

port = new Serial(this, Serial.list()[0], 115200);

cp5 = new ControlP5(this);

// add a horizontal slider for pan

cp5.addSlider("pan")

.setPosition(300,30)

.setRange(0,180)

.setValue(81)

;

// add a horizontal slider for tilt

cp5.addSlider("tilt")

.setPosition(300,60)

.setRange(0,180)

.setValue(146)

;

//////////////////////////////////////////////////////////////////

//lets add buton to empty window

cp5 = new ControlP5(this);

font = createFont("calibri light bold", 20); // custom fonts for buttons and title

cp5.addButton("full") //"red" is the name of button

.setPosition(55, 40) //x and y coordinates of upper left corner of button

.setSize(90, 28) //(width, height)

.setFont(font)

;

cp5.addButton("partial") //"yellow" is the name of button

.setPosition(55, 75) //x and y coordinates of upper left corner of button

.setSize(90, 28) //(width, height)

.setFont(font)

;

cp5.addButton("search") //"blue" is the name of button

.setPosition(55, 110) //x and y coordinates of upper left corner of button

.setSize(90, 28) //(width, height)

.setFont(font)

;

cp5.addButton("track") //"alloff" is the name of button

.setPosition(55, 145) //x and y coordinates of upper left corner of button

.setSize(90, 28) //(width, height)

.setFont(font)

;

//////////////////////////////////////////////////////////////////

}

void draw() {

// output the servo positions for servos

port.write("b"+pan);

port.write("a"+tilt);

///////////////////////////////////////////////

//lets give title to our window

fill(0, 0, 255); //text color (r, g, b)

textFont(font);

text("Azimuth", 60, 30); // ("text", x coordinate, y coordinat)

/////////////////////////////////////////

}

////////////////////////////////////////////////////////////////////////

void full(){

port.write('q');

}

void partial(){

port.write('w');

}

void search(){

port.write('e');

}

void track(){

port.write('r');

}
```here is a video of the problem

https://www.youtube.com/watch?v=w9bJoZjlnuQ&feature=youtu.be

Hey,
I have a couple of thoughts that I would like to share. First, your arduino code there is something I wouldn’t do. In your loop() function, you have two functions which are checking if there is serial data available. You have split your serial checking in two two functions, manualservomove and buttons. If you have incoming data that is meant for buttons, the data would still be digested by manualservomove and this could cause undesirable effects (I do not think this by itself explains the glitches you are observing). Nevertheless you should address this by either merging the functions into one or by reading the serial at the loop() level and then passing it to each function and the right function will process it correctly, like this:

void loop() {
  if ( Serial.available()) {
    char val = Serial.read();
    manualservomove(val);
    buttons(val);
  }
}

So you dont need to read the serial stream inside those functions.

Related to the java code, I don’t recommend you instantiate your cp5 twice: cp5 = new ControlP5(this); In addition, you are sending pan and tilt data in draw() actively and without control. This could be the cause of your glitch. You should only send data when the sliders are updated. For that, I recommend you check the ControlP5 docs and related examples. A possible workaround is to use noLoop() in setup() and trigger draw manually using redraw(). For reference:

I would much prefer the first solution, to send data only when the sliders’s value changes.
I hope this helps,

Kf

Fixing the arduino code did the trick. You are awesome. Thank you for the help. I was convinced the problem was in Processing. I also fixed ControlP5. I will study the redraw when I am fresh. The next hurdle is getting the SONAR data to processing. Wrapping my head around handshaking.