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