Go Down

Topic: Servo glitches caused by Processing (Read 2063 times) previous topic - next topic

urthlight

Dec 07, 2019, 11:29 pm Last Edit: Dec 07, 2019, 11:57 pm by urthlight
I do not know if this is the right area for this question,but here we go.  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?

Code: [Select]
// 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);

}

}

}

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


Code: [Select]
////////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://youtu.be/w9bJoZjlnuQ

TheMemberFormerlyKnownAsAWOL

Code: [Select]
Servo_1.attach(5); //analog pin 0 Which board has pin 5 as analogue pin zero?
Please don't PM technical questions - post them on the forum, then everyone benefits/suffers equally

urthlight


vinceherman

First, a comment on the formatting of your code.
It is difficult to read.
Use the auto format (CTRL-T) and get rid of the excessive white space.
Code: [Select]
#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);
    }
  }
}


I strongly believe that this is much easier to read.  Don't you agree?

I am curious.  You have 2 functions that read a single character form the serial buffer.  How do you know that the expected function is processing the data?

Go Up