Servos: Arduino not respecting 'if' command

Hello!

I am trying to create a multi-servo machine which I can control from my computer, I'll show you the code for the interface first. Now the problem isn't the interface, however, I've tried many times to fix my problem on my GEEKCRAFT (It's basically a 3rd party Arduino UNO, view here ).

The issue with the code is that the 'if' statements are ignored, the servos immediately go to 70 and then to 90 without me sending any short of command, I had to put a delay so that the servos didn't overhead while they are having a mini seizure.

Arduino:

#include <SoftwareSerial.h>

#include <Servo.h>

int data = 0;
Servo myservoa;
Servo myservob;
Servo myservoc;
Servo myservod;
Servo myservoe;
Servo myservof;
void setup() {
  Serial.begin(9600);
  myservoa.attach(3);  // attaches the servo on pin 2 to the servo object
  myservob.attach(4);
  myservoc.attach(5);
  myservod.attach(6);
  myservoe.attach(7);
  myservof.attach(8);
}
void serialEvent() {
  data = Serial.read();
}

void loop() {
  if (data == '1'); {
    myservoa.write(70);              // tell servo to go to position 70
  }
  if (data == '2'); {
    myservob.write(70);
  }
  if (data == '3'); {
    myservoc.write(70);
  }
  if (data == '4'); {
    myservod.write(70);
  }
  if (data == '5'); {
    myservoe.write(70);
  }
  if (data == '6'); {
    myservof.write(70);
  }
  delay(20);
  if (data == '7'); {
    myservoa.write(90);
    myservob.write(90);
    myservoc.write(90);
    myservod.write(90);
    myservoe.write(90);
    myservof.write(90);
  }
}

Computer Interface:

import processing.serial.*;
Serial myPort;
String ledStatus="Command: Not received";
void setup(){
  size(450, 500);
  myPort = new Serial(this, "COM6", 9600); // Starts the serial communication
  myPort.bufferUntil('\n'); // Defines up to which character the data from the serial port will be read. The character '\n' or 'New Line'
}
void serialEvent (Serial myPort){ // Checks for available data in the Serial Port
  ledStatus = myPort.readStringUntil('\n'); //Reads the data sent from the Arduino (the String "LED: OFF/ON) and it puts into the "ledStatus" variable
}
void draw(){
  background(237, 240, 241);
  fill(20, 160, 133); // Green Color
  stroke(33);
  strokeWeight(1);
  rect(50, 125, 150, 50, 10);  // Turn Feet Up Button
  rect(250, 125, 185, 50, 10); // Turn Feet Down Button
  rect(50, 65, 150, 50, 10);  // Turn Bed Up Button
  rect(250, 65, 185, 50, 10); // Turn Bed Down Button
  rect(50, 5, 150, 50, 10);  // Turn Head Up Button
  rect(250, 5, 185, 50, 10); // Turn Head Down Button
  rect(200, 5, 50, 170, 10); // Turn STOP Button
  fill(255);
  
  textSize(32);
  text("Head Up",60, 40);
  text("Head Down", 255, 40);
  text("Bed Up",60, 100);
  text("Bed Down", 255, 100);
  text("Feet Up",60, 160);
  text("Feet Down", 255, 160);
  text("S",217, 40);
  text("T",216, 80);
  text("O",214, 120);
  text("P",217, 160);
  textSize(24);
  fill(33);
  text("Status:", 180, 200);
  textSize(30);
  textSize(16);
  text("Welcome to your personal interface Marian!", 80, 320);
  text(ledStatus, 155, 240); // Prints the string comming from the Arduino
  
   // If the button "STOP" is pressed
  if(mousePressed && mouseX>200 && mouseX<250 && mouseY>5 && mouseY<175){
    myPort.write('7'); // Sends the character '0' and that will turn on the LED
    stroke(255,0,0);
    strokeWeight(2);
    noFill();
    rect(200, 5, 50, 170, 10);
  }
  
  // If the button "Feet Up" is pressed
  if(mousePressed && mouseX>50 && mouseX<200 && mouseY>125 && mouseY<175){
    myPort.write("1"); // Sends the character '0' and that will turn on the LED
    stroke(255,0,0);
    strokeWeight(2);
    noFill();
    rect(50, 125, 150, 50, 10);
  }
    // If the button "Feet Down" is pressed
  if(mousePressed && mouseX>250 && mouseX<435 && mouseY>125 && mouseY<175){
    myPort.write("2"); // Sends the character '1' and that will turn on the LED
    stroke(255,0,0);
    strokeWeight(2);
    noFill();
    rect(250, 125, 185, 50, 10);
  }
    // If the button "Bed Up" is pressed
  if(mousePressed && mouseX>50 && mouseX<200 && mouseY>65 && mouseY<115){
    myPort.write('3'); // Sends the character '2' and that will turn on the LED
    stroke(255,0,0);
    strokeWeight(2);
    noFill();
    rect(50, 65, 150, 50, 10);
  }
      // If the button "Head Up" is pressed
  if(mousePressed && mouseX>50 && mouseX<200 && mouseY>5 && mouseY<55){
    myPort.write('4'); // Sends the character '3' and that will turn on the LED
    stroke(255,0,0);
    strokeWeight(2);
    noFill();
    rect(50, 5, 150, 50, 10);
  }
  // If the button "Bed Down" is pressed
  if(mousePressed && mouseX>250 && mouseX<435 && mouseY>65 && mouseY<115){
    myPort.write('5'); // Sends the character '4' and that will turn on the LED
    stroke(255,0,0);
    strokeWeight(2);
    noFill();
    rect(250, 65, 185, 50, 10);
  }
    // If the button "Head Down" is pressed
  if(mousePressed && mouseX>250 && mouseX<435 && mouseY>5 && mouseY<55){
    myPort.write('6'); // Sends the character '5' and that will turn on the LED
    stroke(255,0,0);
    strokeWeight(2);
    noFill();
    rect(250, 5, 185, 50, 10);
  }
}

You added ; after the () in the if.
Therefore the if will end there.
So it doesn't anything.
After that, the code enclosed in {} is executed as it is.

Your post was MOVED to its current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

Just a suggestion…
Your code might be a lot shorter and easier to work with if you look into arrays - both for your servo injects, and the controlling logic.

1 Like

doesn't look like the serialEvent() is ever invoked and data updated

what about

#include <SoftwareSerial.h>
#include <Servo.h>

Servo myservoa;
Servo myservob;
Servo myservoc;
Servo myservod;
Servo myservoe;
Servo myservof;

void setup() {
    Serial.begin(9600);
    myservoa.attach(3);  // attaches the servo on pin 2 to the servo object
    myservob.attach(4);
    myservoc.attach(5);
    myservod.attach(6);
    myservoe.attach(7);
    myservof.attach(8);
}

void loop() {
    if (! Serial.available ())
        return;

    switch (Serial.read ())  {
    case '1':
        myservoa.write(70);              // tell servo to go to position 70
        break;
    case '2':
        myservob.write(70);
        break;
    case '3':
        myservoc.write(70);
        break;
    case '4':
        myservod.write(70);
        break;
    case '5':
        myservoe.write(70);
        break;
    case '6':
        myservof.write(70);
        break;
    case '7':
        myservoa.write(90);
        myservob.write(90);
        myservoc.write(90);
        myservod.write(90);
        myservoe.write(90);
        myservof.write(90);
        break;;
    default:
        break;;
    }
}
1 Like

I have never understood why anyone would use the serialEvent() function. To me its only serves to confuse things. If it were implemented as an ISR it would be more useful (and more confusing !).

Oh my goodness thank you so much! I've been working on this for like 5 hours very confused as to why it's not working but thank you so much it works now!!!!

Thank you for that, i've used this code and this works very well. Thank you for your help, you made the program a lot simpler to use and build.