Really basic question about loop and math

edited February 2017 in Arduino

First I am a total NOOB... this is my first attempt at code, so no level of explanation is too much. I am sorry for this.

I am trying to finish a code that controls servos (in Arduino). My arduino code works well so is not the issue. In Processing code when I press a keyboard button I port write (to the servo) and display in the processing box a set value that the servo will move to 0,90,180 for case 'a' 'q' 'z' and so on...while not pretty this all works as intended. for case 'Q' and 'Z' I want the value displayed to go up by 5 or down by 5 respectively (already have the servos moving 5 degrees, I just want the processing display to +/- by 5). Once I have a solution I will use for 'W' 'X' and so on... I know what I currently have is not correct and the displayed value just continues to count up or down. I have tried noLoop() with no success. I know there is a simple answer and I have searched..now I am reaching out for help, thanks.

    char letter;

    int S1,S2,S3;
        // Use with"servo_processing_multiple_servo_us" arduino program
        // S J 2/2017
        // adapted from processing.org/examples/charactersstrings


        import processing.serial.*;  //import the serial library
        Serial port;   //define a port of the serial type

        void setup() {
          size(800, 600);
          S1=90;
          S2=90;
          S3=90;


          port = new Serial(this, "COM3", 9600);   //connect to the Arduino serial port 
        }

        void draw() {
          background(250,13,13); // Set background to red

          // text showing information in RED BOX
          textSize(32);
          text("Click on anywhere on the RED BOX, then a key", 50, 50);
          text("Servo 1:q/a/z  Servo 2:w/s/x  Servo 3:e/d/c",50,100);
          text("----------------------------------------------",50,130);
          text("Current key: " + letter, 50, 150);
          text("Servo 1 @ : " + S1,50,200);
          text("Servo 2 @ : " + S2,50,250);
          text("Servo 3 @ : " + S3,50,300);
          switch(letter){//switch case to assign letter pressed to angle written on RED BOX
          case'Q':
           S1=S1+5;
          break;
          case'Z':
          S1=S1-5;
          break;
            case'q':    
            S1=0;
            break;
            case'a':
            S1=90;                
            break;
            case'z':
            S1=180;                
            break;
            case'w':
            S2 = 0;
            break;
            case's':
            S2 = 90;
            break;
            case'x':
            S2= 180;
            break;
            case'e':
            S3 = 0;
            break;
            case'd':
            S3 = 90;
            break;
            case'c':
            S3= 180;
            break;           

          }

        }

        void keyTyped() {
          // The variable "key" always contains the value 
          // of the most recent key pressed.
          if ((key >= '0' && key <= '}') || key == ' ') {
            letter = key;               

            println(key);// Write the letter to console *decoration*
            port.write(key);//sends key pressed to serial 9600 so arduino can read *ESSENTIAL*


          }
        }

Any assistance will be very much appreciated Thanks again for any help Steve

Answers

  • So when you send the letter to your arduino, you interpret the letter in your arduino code as well? You are doing twice the amount of work because you are interpreting the letter in your draw function. Instead, rethink your design in the following manner:

    Your Processing GUI, which intercepts user's input, should manage actual key values and send the commands to your arduino, not the nominal letter received but the actual rotation you want to perform. On the arduino code, you keep track of your servo's positions. If your GUI ever wants to display the position of your servos (I am referring to line 31-33), then you send a command from Processing to your arduino code inquiring about the current servo positions.

    Please keep in mind that I don't know all the details of servo's functionality. This previous idea is based on the concept that when you specify a position to the servo, let's say 45, your servo will go to position 45 (out of 360). I can imagine some servos will just rotate based on how many ticks you tell it to rotate and it might not be able to keep track of actual absolute position.

    I encourage you to also share your arduino code.

    My previous idea is your a suggestion.

    Kf

  • Kf,

    Thank you very much for the response and suggestions Yes, you are correct arduino interprets the letter and then assigns a value in microseconds. I first tried to have Processing send the actual position and this will work with one servo, I was not ever able to get this to function with more than one so I went with the case 'q','a','z',...solution with pre written microseconds. Or in the case of 'Q','Z' 5 degree increments (55 microseconds)

    Yes, it seems twice the work, but it works and does what I want the servo to do. As for reading the position in arduino, servo.read() will read the last position sent to the servo (not the actual servo position) and I considered using this value but I do not know how to send this back to Processing, (or how Processing can ask to read this position). I really am new to writing any kind of code. Here is the arduino code

    // Based on serial_servo written by Adrian Glasser
    //Modified by S J 02/2017 for multiple servo
    //Modified for switch case serial input
    //for Proceessing program "push_a_letter_port_write_servo" 
    //Moodified for uS output
    
    #include <Servo.h>
    #include <SoftwareSerial.h>
    
    Servo servo1;  // create servo object to control a servo 
    Servo servo2;
    Servo servo3;
    Servo servo4;
    Servo servo5;
    
    
    
    int ServoPin9 = 9; //identify which pin the servo is connected to
    int ServoPin8 = 8;
    int ServoPin10 = 10;
    int ServoPin11 = 11;
    int ServoPin12 = 12;
    int inByte;  //create a variable
    int us;// variable for servowrite
    int N1;//variable to count
    
    void setup() {
      // start serial port at 9600 bps:
      Serial.begin(9600);
      servo1.attach(ServoPin8);  // attaches the servo on pin to the servo object
      servo2.attach(ServoPin9);
      servo3.attach(ServoPin10);
      servo4.attach(ServoPin11);
      servo5.attach(ServoPin12);
    
      servo1.writeMicroseconds(1500);//sets servos 90 degrees initial position
      servo2.writeMicroseconds(1500);
      servo3.writeMicroseconds(1500);
      servo4.writeMicroseconds(1500);
      servo5.writeMicroseconds(1500);
      us=1500;
      N1=0;
      }
    
    
    void loop() {
      // read the serial:
      if (Serial.available() > 0) {
        int inByte = Serial.read();
        //from arduino.cc/en/Tutorial/switchCase2 and Arduino.cc/en/Reference/ServoWriteMicroseconds
        // do something different depending on the character received.
        // The switch statement expects single number values for each case;
        // in this exmaple, though, you're using single quotes to tell
        // the controller to get the ASCII value for the character.  For
        // example 'a' = 97, 'b' = 98, and so forth:
    
        switch (inByte) { 
          case 'Q'://add 5 degrees
          do{  
        N1=N1+1;
        us=us+55;// about 5 degrees
        servo1.writeMicroseconds(us);
          }while (N1<5);    
      break;
       case 'Z'://subtract 5 degrees
          do{  
        N1=N1+1;
        us=us-55;// about 5 degrees
        servo1.writeMicroseconds(us);
          }while (N1<5);   
          break; 
          case 'q':
          us=2500;
            servo1.writeMicroseconds(us);//uS 2500 is full clockwise
            break;
          case 'a':
          us=1500;
            servo1.writeMicroseconds(us);//uS 1500 mid position
            break;
          case 'z':
          us=700;
            servo1.writeMicroseconds(us);// uS 700 full CCW
            break;
          case 'w':
            servo2.writeMicroseconds(2500);//uS 2500 is full clockwise
            break;
          case 's':
            servo2.writeMicroseconds(1500);//uS 1500 mid position
            break;
          case 'x':
            servo2.writeMicroseconds(700);// uS 700 full CCW
            break;
            case 'e':
            servo3.writeMicroseconds(2500);//uS 2500 is full clockwise
            break;
          case 'd':
            servo3.writeMicroseconds(1500);//uS 1500 mid position
            break;
          case 'c':
            servo3.writeMicroseconds(700);// uS 700 full CCW
            break;
    
    
        }
      }
    }
    

    Any further input is greatly appreciated. Steve

  • OK, still stumped. I tried servo.read, Serial.write in arduino (for both microseconds and angle) and port.read in Processing and now I get -1 for all values ( modified for case 'q','a','z','Q','Z') here is the arduino code

    // Based on serial_servo written by Adrian Glasser
    //Modified by S J 02/2017 for multiple servo
    //Modified for switch case serial input
    //for Proceessing program "push_a_letter_port_write_servo" 
    //Moodified for uS output
    
    #include <Servo.h>
    #include <SoftwareSerial.h>
    
    Servo servo1;  // create servo object to control a servo 
    Servo servo2;
    Servo servo3;
    Servo servo4;
    Servo servo5;
    
    
    
    int ServoPin9 = 9; //identify which pin the servo is connected to
    int ServoPin8 = 8;
    int ServoPin10 = 10;
    int ServoPin11 = 11;
    int ServoPin12 = 12;
    int inByte;  //create a variable
    int us;// variable for servowrite
    int N1;//variable to count
    int sa1;//servo1 angle
    
    void setup() {
      // start serial port at 9600 bps:
      Serial.begin(9600);
      servo1.attach(ServoPin8);  // attaches the servo on pin to the servo object
      servo2.attach(ServoPin9);
      servo3.attach(ServoPin10);
      servo4.attach(ServoPin11);
      servo5.attach(ServoPin12);
    
      servo1.writeMicroseconds(1500);//sets servos 90 degrees initial position
      servo2.writeMicroseconds(1500);
      servo3.writeMicroseconds(1500);
      servo4.writeMicroseconds(1500);
      servo5.writeMicroseconds(1500);
      us=90;
      N1=0;
      }
    
    
    void loop() {
      // read the serial:
      if (Serial.available() > 0) {
        int inByte = Serial.read();
        //from arduino.cc/en/Tutorial/switchCase2 and Arduino.cc/en/Reference/ServoWriteMicroseconds
        // do something different depending on the character received.
        // The switch statement expects single number values for each case;
        // in this exmaple, though, you're using single quotes to tell
        // the controller to get the ASCII value for the character.  For
        // example 'a' = 97, 'b' = 98, and so forth:
    
        switch (inByte) { 
          case 'Q'://add 5 degrees
          do{  
        N1=N1+1;
        us=us+5;// about 5 degrees
        servo1.write(us);
          }while (N1<5);    
          sa1 =servo1.read();
          Serial.write(sa1);
      break;
       case 'Z'://subtract 5 degrees
          do{  
        N1=N1+1;
        us=us-5;// about 5 degrees
        servo1.write(us);
          }while (N1<5); 
           sa1 =servo1.read();
          Serial.write(sa1);  
          break; 
          case 'q':
          us=180;
            servo1.write(us);//uS 2500 is full clockwise
             sa1 =servo1.read();
          Serial.write(sa1);  
            break;
          case 'a':
          us=90;
            servo1.write(us);//uS 1500 mid position
             sa1 =servo1.read();
          Serial.write(sa1);  
            break;
          case 'z':
          us=1;
            servo1.write(us);// uS 700 full CCW
             sa1 =servo1.read();
          Serial.write(sa1);  
            break;
          case 'w':
            servo2.writeMicroseconds(2500);//uS 2500 is full clockwise
            break;
          case 's':
            servo2.writeMicroseconds(1500);//uS 1500 mid position
            break;
          case 'x':
            servo2.writeMicroseconds(700);// uS 700 full CCW
            break;
            case 'e':
            servo3.writeMicroseconds(2500);//uS 2500 is full clockwise
            break;
          case 'd':
            servo3.writeMicroseconds(1500);//uS 1500 mid position
            break;
          case 'c':
            servo3.writeMicroseconds(700);// uS 700 full CCW
            break;
    
    
        }
      }
    }
    

    and Processing code

    char letter;
    
    int S1,S2,S3,N1;
    // Use with"servo_processing_multiple_servo_us" arduino program
    // S J 2/2017
    // adapted from processing.org/examples/charactersstrings
    
    
    
    import processing.serial.*;  //import the serial library
    Serial port;   //define a port of the serial type
    
    void setup() {
      size(800, 600);
      S1=90;
      S2=90;
      S3=90;
    
    
    
      port = new Serial(this, "COM3", 9600);   //connect to the Arduino serial port 
    
    }
    
    void draw() {
      background(250,13,13); // Set background to purple
    
      // text showing information in RED BOX
      textSize(32);
      text("Click on anywhere on the RED BOX, then a key", 50, 50);
      text("Servo 1:q/a/z  Servo 2:w/s/x  Servo 3:e/d/c",50,100);
      text("----------------------------------------------",50,130);
      text("Current key: " + letter, 50, 150);
      text("Servo 1 @ : " + S1,50,200);
      text("Servo 2 @ : " + S2,50,250);
      text("Servo 3 @ : " + S3,50,300);
      switch(letter){//switch case to assign letter pressed to angle written on RED BOX
      case'Q':
      S1=port.read();
      break;
    
      case'Z':
      S1=port.read(); 
       break;
        case'q':    
        S1=port.read(); 
        break;
    
    
        case'a':
        S1=port.read(); 
        break;
    
    
        case'z':
        S1=port.read(); 
        break;
    
    
        case'w':
        S2 = 0;
        break;
        case's':
        S2 = 90;
        break;
        case'x':
        S2= 180;
        break;
        case'e':
        S3 = 0;
        break;
        case'd':
        S3 = 90;
        break;
        case'c':
        S3= 180;
        break;
    
    
    
      }
    
    }
    
    void keyTyped() {
      // The variable "key" always contains the value 
      // of the most recent key pressed.
      if ((key >= '0' && key <= '}') || key == ' ') {
        letter = key;
    
    
    
    
        println(key);// Write the letter to console *decoration*
        port.write(key);//sends key pressed to serial 9600 so arduino can read *ESSENTIAL*
    
    
        }
    
    
      }
    }
    

    Help........

  • Thanks, but Ahhhhhhhhhhh...

    Processing line 95 port.write(key) seems to be giving the -1 value so I moved the port.read() below that.

    Now I just get strange values with the port.read() for what should be subtracting 5 from 180 reads 92,15, 41, 47, 52 port.read() for end and mid values were just as strange so I reassigned set values for 'q','a','z', my arduino code continues to move the servo exactly as intended, the processing display numbers remains the bug. new processing code

    char letter;
    
    int S1,S2,S3;
    // Use with"servo_processing_multiple_servo_us" arduino program
    // S J 2/2017
    // adapted from processing.org/examples/charactersstrings
    
    
    
    import processing.serial.*;  //import the serial library
    Serial port;   //define a port of the serial type
    
    void setup() {
      size(800, 600);
    
      S2=90;
      S3=90;
      S1=90;
    
    
      port = new Serial(this, "COM3", 9600);   //connect to the Arduino serial port 
    
    }
    
    void draw() {
      background(250,13,13); // Set background to purple
    
      // text showing information in RED BOX
      textSize(32);
      text("Click on anywhere on the RED BOX, then a key", 50, 50);
      text("Servo 1:q/a/z  Servo 2:w/s/x  Servo 3:e/d/c",50,100);
      text("----------------------------------------------",50,130);
      text("Current key: " + letter, 50, 150);
      text("Servo 1 @ : " + S1,50,200);
      text("Servo 2 @ : " + S2,50,250);
      text("Servo 3 @ : " + S3,50,300);
    
    
    }
    
    void keyTyped() {
      // The variable "key" always contains the value 
      // of the most recent key pressed.
      if ((key >= '0' && key <= '}') || key == ' ') {
        letter = key;
    
    
    
    
        println(key);// Write the letter to console *decoration*
        port.write(key);//sends key pressed to serial 9600 so arduino can read *ESSENTIAL*
       port.write(0);
        switch(key){//Servo# and angle to the console *decoration*
    
        case'Q':
        S1=port.read();
        println("Servo1 to " + S1);
        break;
        case'Z':
        S1=port.read(); 
        println("Servo1 to " + S1);
        break;
    
          case'q':
          println("Servo1 to " +S1);
          S1=0;
          break;
          case'a':
          println("Servo1 to "+ S1);
          S1=90;
          break;
          case'z':
          println("Servo1 to "+ S1);
          S1=180;
          break;
          case'w':
          S2=0;
          println("Servo2 to " +S2);
          break;
          case's':
          S2=90;
          println("Servo2 to "+ S2);
          break;
          case'x':
          S2=180;
          println("Servo2 to "+ S2);
          break;
          case'e':
          S3=0;
          println("Servo3 to " +S3);
          break;
          case'd':
          S3=90;
          println("Servo3 to "+ S3);
          break;
          case'c':
          S3=180;
          println("Servo3 to "+ S3);
          break;
    
        }
    
    
      }
    }
    

    Again, thanks for any and all assistance

    Steve

  • edited February 2017 Answer ✓

    Your port.read() statements are assuming that there is a byte waiting in the Serial buffer. But if there isn't a -1 will be returned. So here is a code fragment to give you some ideas??

    port.write(0);
    delay(25);
    switch(key){//Servo# and angle to the console *decoration*
        case'Q':
        if(port.available() > 0) S1=port.read();
        ...
        ...
    
  • pxgator, thanks that is just what I needed.

    kf, thanks for steering me in the right direction.

    Got it all to work. The servo.read still gives some incorrect readings but not off by much, it is useful with the count up and down. I went with the set values for end (0,180) and mid (90) points. Thanks again for all the help !

  • Glad to hear that your code is working. It would be good if you could post your 'working code' so others can learn from it.

  • Here is the “working” code. I am still having some strange results with +/- 5 degrees display (on Processing) on Servo 1 with Q and Z input and Servo 1,2 ( and at times 3) with 4 and 5 input. The servos actually move as desired. The display of servo angles just is just off. I initially put in delays (I tried from delay(5) to delay(25)) in the arduino code to see if this helped (not sure it did), but had the unintended effect of slowing the whole process down. This is most notable when holding the Q or Z down to reach a desired angle and as expected was 5x worse with holding the 4 or 5 down, so I took them out.

    If anyone wants to try a fix feel free.

    Thanks again to all...

    Processing code

    char letter;
    
    int S1,S2,S3;
    int S4,S5;
    // Use with"servo_processing_multiple_servo_us" arduino program
    // S Johnston 2/2017
    // adapted from processing.org/examples/charactersstrings
    //thanks to Kfrajer and pxgator for assist with serial.read servo.read
    //for 5 servos q,w,e,r,t move to 180
    //a,s,d,f,g move to 90
    //z,x,c,v,b move to 0
    // Q,W,E,R,T  and Z,X,C,V,B move +/- 5 degrees
    //1,2,3,4,5 move all servos 180,90,0,+/- 5 degrees
    
    
    
    import processing.serial.*;  //import the serial library
    Serial port;   //define a port of the serial type
    
    void setup() {
      size(500, 450);
    
      S2=0;
      S3=0;
      S1=0;
      S4=0;
      S5=0;  
      port = new Serial(this, "COM3", 9600);   //connect to the Arduino serial port 
    
    }
    
    void draw() {
      background(250,13,13); // Set background to purple
    
      // text showing information in RED BOX
      textSize(18);
      text("Click on anywhere on the RED BOX, then a key", 50, 30);
      text("Position   180    90      0      +5       -5 ",50,70);
      text("Servo 1:    q        a       z        Q         Z  ",50,100);
      text("Servo 2:    w        s       x        W        X  ",50,130);
      text("Servo 3:    e        d       c         E         C  ",50,160);
      text("Servo 4:    r         f        v        R         V  ",50,190);
      text("Servo 5:    t         g       b        T         B  ",50,220);
      text("Servo All:  1        2       3        4         5", 50,250);
      text("--------------------------------------",50,280);
      text("Key Pressed: " + letter, 250, 310);
      text("Servo 1 @ : " + S1,50,310);
      text("Servo 2 @ : " + S2,50,340);
      text("Servo 3 @ : " + S3,50,370);
      text("Servo 4 @ : " + S4,50,400);
      text("Servo 5 @ : " + S5,50,430);
    }
    
    void keyTyped() {
      // The variable "key" always contains the value 
      // of the most recent key pressed.
      if ((key >= '0' && key <= '}') || key == ' ') {
        letter = key;
    
    
        println(key);// Write the letter to console *decoration*
        port.write(key);//sends key pressed to serial 9600 so arduino can read *ESSENTIAL*
       port.write(0);
       delay(5);
        switch(key){//Servo# and angle to the console *decoration*
    
        case'Q':
        if(port.available()>0)S1=port.read();
        println("Servo1 to " + S1);
        break;
        case'Z':
        if(port.available()>0)S1=port.read(); 
        println("Servo1 to " + S1);
        break;    
          case'q':
          println("Servo1 to " +S1);
          S1=180;
          break;
          case'a':
          println("Servo1 to "+ S1);
          S1=90;
          break;
          case'z':
          println("Servo1 to "+ S1);
          S1=0;
          break;
    
        case'W':
        if(port.available()>0)S2=port.read();
        println("Servo2 to " + S2);
        break;
        case'X':
        if(port.available()>0)S2=port.read(); 
        println("Servo2 to " + S2);
        break;    
          case'w':
          S2=180;
          println("Servo2 to " +S2);
          break;
          case's':
          S2=90;
          println("Servo2 to "+ S2);
          break;
          case'x':
          S2=0;
          println("Servo2 to "+ S2);
          break;
    
        case'E':
        if(port.available()>0)S3=port.read();
        println("Servo3 to " + S3);
        break;
        case'C':
        if(port.available()>0)S3=port.read(); 
        println("Servo3 to " + S3);
        break;    
          case'e':
          S3=180;
          println("Servo3 to " +S3);
          break;
          case'd':
          S3=90;
          println("Servo3 to "+ S3);
          break;
          case'c':
          S3=0;
          println("Servo3 to "+ S3);
          break;
    
        case'R':
        if(port.available()>0)S4=port.read();
        println("Servo4 to " + S4);
        break;
        case'V':
        if(port.available()>0)S4=port.read(); 
        println("Servo4 to " + S4);
        break;    
          case'r':
          S4=180;
          println("Servo4 to " +S4);
          break;
          case'f':
          S4=90;
          println("Servo4 to "+ S4);
          break;
          case'v':
          S4=0;
          println("Servo4 to "+ S4);
          break;
    
        case'T':
        if(port.available()>0)S5=port.read();
        println("Servo5 to " + S5);
        break;
        case'B':
        if(port.available()>0)S5=port.read(); 
        println("Servo5 to " + S5);
        break;    
          case't':
          S5=180;
          println("Servo5 to " +S5);
          break;
          case'g':
          S5=90;
          println("Servo5 to "+ S5);
          break;
          case'b':
          S5=0;
          println("Servo5 to "+ S5);
          break;
    
          case '1':
          port.write('q');
          S1=180;
          delay(25);
          port.write('w');
          S2=180;
          delay(25);
          port.write('e');
          S3=180;
          delay(25);
          port.write('r');
          S4=180;
          delay(25);
          port.write('t');
          S5=180;
          break;
    
          case '2':
          port.write('a');
          delay(25);
          port.write('s');
          delay(25);
          port.write('d');
          delay(25);
          port.write('f');
          delay(25);
          port.write('g');
          S1=90;
          S2=90;
          S3=90;
          S4=90;
          S5=90;
          break;
    
          case '3':
          port.write('z');
          delay(25);
          port.write('x');
          delay(25);
          port.write('c');
          delay(25);
          port.write('v');
          delay(25);
          port.write('b');
          S1=0;
          S2=0;
          S3=0;
          S4=0;
          S5=0;
          break;
    
          case '4':
          port.write('Q');
          delay(5);
          if(port.available()>0)S1=port.read();
          port.write('W');
          delay(5);
          if(port.available()>0)S2=port.read();
          port.write('E');
          delay(5);
          if(port.available()>0)S3=port.read();
          port.write('R');
          delay(5);
          if(port.available()>0)S4=port.read();
          port.write('T');
          delay(5);
          if(port.available()>0)S5=port.read();      
          break;
    
          case '5':
          port.write('Z');
          delay(5);
          if(port.available()>0)S1=port.read();
          port.write('X');
          delay(5);
          if(port.available()>0)S2=port.read();
          port.write('C');
          delay(5);
          if(port.available()>0)S3=port.read();
          port.write('V');
          delay(5);
          if(port.available()>0)S4=port.read();
          port.write('B');
          delay(5);
          if(port.available()>0)S5=port.read();      
          break;
        }
    
    
      }
    }
    

    arduino code

    // Based on serial_servo written by Adrian Glasser
    //Modified by S Johnston 02/2017 for multiple servo
    //Modified for switch case serial input
    //for Proceessing program "push_a_letter_port_write_servo_plus5" 
    //Moodified for uS output
    
    #include <Servo.h>
    #include <SoftwareSerial.h>
    
    Servo servo1;  // create servo object to control a servo 
    Servo servo2;
    Servo servo3;
    Servo servo4;
    Servo servo5;
    
    
    
    int ServoPin9 = 9; //identify which pin the servo is connected to
    int ServoPin8 = 8;
    int ServoPin10 = 10;
    int ServoPin11 = 11;
    int ServoPin12 = 12;
    int inByte;  //create a variable
    int us1;// variable for servowrite
    int us2;
    int us3;
    int us4;
    int us5;
    int N1,N2,N3,N4,N5;//variable to count
    int sa1;//servo angle
    int sa2;
    int sa3;
    int sa4;
    int sa5;
    
    void setup() {
      // start serial port at 9600 bps:
      Serial.begin(9600);
      servo1.attach(ServoPin8);  // attaches the servo on pin to the servo object
      servo2.attach(ServoPin9);
      servo3.attach(ServoPin10);
      servo4.attach(ServoPin11);
      servo5.attach(ServoPin12);
    
      servo1.writeMicroseconds(600);//sets servos 90 degrees initial position
      servo2.writeMicroseconds(600);
      servo3.writeMicroseconds(600);
      servo4.writeMicroseconds(600);
      servo5.writeMicroseconds(600);
      us1=600;
      us2=600;
      us3=600;
      us4=600;
      us5=600;
      N1=0;
      N2=0;
      N3=0;
      N4=0;
      N5=0;
      }
    
    
    void loop() {
      // read the serial:
      if (Serial.available() > 0) {
        int inByte = Serial.read();
        //from arduino.cc/en/Tutorial/switchCase2 and Arduino.cc/en/Reference/ServoWriteMicroseconds
        // do something different depending on the character received.
        // The switch statement expects single number values for each case;
        // in this exmaple, though, you're using single quotes to tell
        // the controller to get the ASCII value for the character.  For
        // example 'q' = 2500 microseconds, 'z' = 600 microseconds, and so forth:
        //servosed used had values of 600 uS = 0 degrees, 2500= 180 degrees, other servos will vary
    servo1.writeMicroseconds(us1);
    servo2.writeMicroseconds(us2); 
    servo3.writeMicroseconds(us3);
    servo4.writeMicroseconds(us4); 
    servo5.writeMicroseconds(us5);      
        switch (inByte) { 
          case 'Q'://add 5 degrees case1(servo1,N1,us1,sa1)
          do{  
        N1=N1+1;
        us1=us1+55;// about 5 degrees    
          }while (N1<5);    
          servo1.writeMicroseconds(us1);
          sa1 =servo1.read();//read servo angle
          if (Serial.available() > 0);
          Serial.write(sa1);//write servo angle to Processing
      break;
       case 'Z'://subtract 5 degrees
          do{  
        N1=N1+1;
        us1=us1-55;// about 5 degrees    
          }while (N1<5); 
          servo1.writeMicroseconds(us1);
           sa1 =servo1.read();//read servo angle
           if (Serial.available() > 0);
          Serial.write(sa1);//write servo angle to Processing 
          break; 
          case 'q':
          us1=2500;
            servo1.writeMicroseconds(us1);//uS 2500 is full clockwise
            break;
          case 'a':
          us1=1500;
           servo1.writeMicroseconds(us1);//uS 1500 mid position           
            break;
          case 'z':
          us1=600;
            servo1.writeMicroseconds(us1);// uS 600 full CCW          
            break;
    
           case 'W'://add 5 degrees case2(servo2,N2,us2,sa2)
          do{  
        N2=N2+1;
        us2=us2+55;// about 5 degrees    
          }while (N2<5);    
          servo2.writeMicroseconds(us2);
          sa2 =servo2.read();//read servo angle
          if (Serial.available() > 0);
          Serial.write(sa2);//write servo angle to Processing
      break;
       case 'X'://subtract 5 degrees
          do{  
        N2=N2+1;
        us2=us2-55;// about 5 degrees    
          }while (N2<5); 
          servo2.writeMicroseconds(us2);
           sa2 =servo2.read();//read servo angle
           if (Serial.available() > 0);
          Serial.write(sa2);//write servo angle to Processing 
          break; 
          case 'w':
          us2=2500;
            servo2.writeMicroseconds(us2);//uS 2500 is full clockwise
            break;
          case 's':
          us2=1500;
           servo2.writeMicroseconds(us2);//uS 1500 mid position           
            break;
          case 'x':
          us2=600;
            servo2.writeMicroseconds(us2);// uS 600 full CCW          
            break;
    
             case 'E'://add 5 degrees case3(servo3,N3,us3,sa3)
          do{  
        N3=N3+1;
        us3=us3+55;// about 5 degrees    
          }while (N3<5);    
          servo3.writeMicroseconds(us3);
          sa3 =servo3.read();//read servo angle
          if (Serial.available() > 0);
          Serial.write(sa3);//write servo angle to Processing
      break;
       case 'C'://subtract 5 degrees
          do{  
        N3=N3+1;
        us3=us3-55;// about 5 degrees    
          }while (N3<5); 
          servo3.writeMicroseconds(us3);
           sa3 =servo3.read();//read servo angle
           if (Serial.available() > 0);
          Serial.write(sa3);//write servo angle to Processing 
          break; 
          case 'e':
          us3=2500;
            servo3.writeMicroseconds(us3);//uS 2500 is full clockwise
           break;
          case 'd':
          us3=1500;
           servo3.writeMicroseconds(us3);//uS 1500 mid position           
            break;
          case 'c':
          us3=600;
            servo3.writeMicroseconds(us3);// uS 600 full CCW          
            break;
    
     case 'R'://add 5 degrees case4(servo4,N4,us4,sa4)
          do{  
        N4=N4+1;
        us4=us4+55;// about 5 degrees    
          }while (N4<5);    
          servo4.writeMicroseconds(us4);
          sa4 =servo4.read();//read servo angle
          if (Serial.available() > 0);
          Serial.write(sa4);//write servo angle to Processing
      break;
       case 'V'://subtract 5 degrees
          do{  
        N4=N4+1;
        us4=us4-55;// about 5 degrees    
          }while (N4<5); 
          servo4.writeMicroseconds(us4);
           sa4 =servo4.read();//read servo angle
           if (Serial.available() > 0);
          Serial.write(sa4);//write servo angle to Processing 
          break; 
          case 'r':
          us4=2500;
            servo4.writeMicroseconds(us4);//uS 2500 is full clockwise
            break;
          case 'f':
          us4=1500;
           servo4.writeMicroseconds(us4);//uS 1500 mid position           
            break;
          case 'v':
          us4=600;
            servo4.writeMicroseconds(us4);// uS 600 full CCW          
            break;
    
             case 'T'://add 5 degrees case5(servo5,N5,us5,sa5)
          do{  
        N5=N5+1;
        us5=us5+55;// about 5 degrees    
          }while (N5<5);    
          servo5.writeMicroseconds(us5);
          sa5 =servo5.read();//read servo angle
          Serial.write(sa5);//write servo angle to Processing
      break;
       case 'B'://subtract 5 degrees
          do{  
        N5=N5+1;
        us5=us5-55;// about 5 degrees    
          }while (N5<5); 
          servo5.writeMicroseconds(us5);
           sa5 =servo5.read();//read servo angle
          Serial.write(sa5);//write servo angle to Processing 
          break; 
          case 't':
          us5=2500;
            servo5.writeMicroseconds(us5);//uS 2500 is full clockwise
            break;
          case 'g':
          us5=1500;
           servo5.writeMicroseconds(us5);//uS 1500 mid position           
            break;
          case 'b':
          us5=600;
            servo5.writeMicroseconds(us5);// uS 600 full CCW          
            break;
    
        }
      }
    }
    
Sign In or Register to comment.