Have servo connected to Arduino respond to incomingByte

hi. I’m new to servos and arduino. I have a processing sketch sending signals to Arduino and I need the servo to respond in specific ways to each of 4 incoming bytes:

incomingByte1: move direction 1(backward) for 2 secs
incomingByte2: move quickly backward and forward for 2 secs (twitchy behavior)
incomingByte3: move direction 2(forward) for 1 sec
incomingByte4: move direction 2(forward) for 2 secs.

I think my code is messed up but don’t know where or how to fix it. Appreciate any guidance.

#include <Servo.h> 

Servo myservo;  // create servo object to control a servo 
// a maximum of eight servo objects can be created 

int pos = 0;    // variable to store the servo position 
int myServo = 9;     // Control pin for servo motor
int minPulse = 200;   // Minimum servo position
int maxPulse = 2400;  // Maximum servo position
int pulse = 2400;        // Amount to pulse the servo
//
long lastPulse = 0;    // the time in milliseconds of the last pulse
int refreshTime = 100; // the time needed in between pulses
//
int incomingByte = 0;

void setup() 

{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  pinMode(myServo, OUTPUT);  // Set servo pin as an output pin
  Serial.begin(9600);

  // Set the motor position value to the middle  
  pulse = (maxPulse-minPulse)/2;          
  digitalWrite(myServo, HIGH);   // Turn the motor on
  delayMicroseconds(pulse);       // Length of the pulse sets the motor position
  digitalWrite(myServo, LOW);    // Turn the motor off
}

void loop() {
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();

    { 
      for(pulse = 0; pulse < 180; pulse += 1)  // goes from 0 degrees to 180 degrees 
      {                                      // in steps of 1 degree 
        myservo.write(pulse);              // tell servo to go to position in variable 'pos' 
        delay(100);                       // waits 100ms for the servo to reach the position 
      } 
      for(pulse = 180; pulse > 0; pulse -=1)     // goes from 180 degrees to 0 degrees 
      {                                
        myservo.write(pulse);              // tell servo to go to position in variable 'pos' 
        delay(100);                       // waits 100ms for the servo to reach the position 
      } 
    } 

    if(incomingByte = 1){  // 1 = value that is comming from processing
      if(pulse<maxPulse){
        pulse -=1;
      }
    }
    if(incomingByte = 2){  // 2 = value that is comming from processing
      if(pulse>minPulse){
        pulse -=1;
      }
    }
    if(incomingByte = 3){  // 3 = value that is comming from processing
      if(pulse>minPulse){
        pulse +=1;
      }
    }

    if(incomingByte = 4){  // 4 = value that is comming from processing
      if(pulse>minPulse){
        pulse +=1;
      }
    }

    //  pulse the servo again if the refresh time (15 ms) have passed:
    if (millis() - lastPulse > refreshTime) {
      digitalWrite(myServo, HIGH);   // Turn the motor on
      delayMicroseconds(pulse);       // Length of the pulse sets the motor position
      delay(1000);

      digitalWrite(myServo, LOW);    // Turn the motor off
      lastPulse = millis();           // save the time of the last pulse
      delay(1000);

    }

  }
}
pinMode(myServo, OUTPUT);  // Set servo pin as an output pin
  Serial.begin(9600);

  // Set the motor position value to the middle  
  pulse = (maxPulse-minPulse)/2;          
  digitalWrite(myServo, HIGH);   // Turn the motor on
  delayMicroseconds(pulse);       // Length of the pulse sets the motor position
  digitalWrite(myServo, LOW);    // Turn the motor off

I think you're confused about how the servo library works; there is no need for you to generate pulses manually like this.

ok. what am I replacing exactly then? When I replaced the code below, I'm throwing errors.

int pos = 0;    // variable to store the servo position 
int myServo = 9;     // Control pin for servo motor
int minPulse = 200;   // Minimum servo position
int maxPulse = 2400;  // Maximum servo position
int pulse = 2400;        // Amount to pulse the servo

long lastPulse = 0;    // the time in milliseconds of the last pulse
int refreshTime = 100; // the time needed in between pulses

int incomingByte = 0;

void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  pinMode(myServo, OUTPUT);  // Set servo pin as an output pin
  Serial.begin(9600);

  // Set the motor position value to the middle  
  pulse = (maxPulse-minPulse)/2;          
  digitalWrite(myServo, HIGH);   // Turn the motor on
  delayMicroseconds(pulse);       // Length of the pulse sets the motor position
 digitalWrite(myServo, LOW);    // Turn the motor off
}

Let me guess; the compiler is complaining about a missing "loop"?

umm, no...

colorrecog_arduino2:31: error: expected constructor, destructor, or type conversion before '(' token
colorrecog_arduino2:32: error: expected constructor, destructor, or type conversion before '.' token
colorrecog_arduino2:35: error: expected constructor, destructor, or type conversion before '=' token
colorrecog_arduino2:36: error: expected constructor, destructor, or type conversion before '(' token
colorrecog_arduino2:37: error: expected constructor, destructor, or type conversion before '(' token
colorrecog_arduino2:38: error: expected constructor, destructor, or type conversion before '(' token
colorrecog_arduino2.cpp: In function 'void loop()':
colorrecog_arduino2:43: error: 'incomingByte' was not declared in this scope
colorrecog_arduino2:46: error: 'pulse' was not declared in this scope
colorrecog_arduino2:59: error: 'pulse' was not declared in this scope
colorrecog_arduino2:59: error: 'maxPulse' was not declared in this scope
colorrecog_arduino2:64: error: 'pulse' was not declared in this scope
colorrecog_arduino2:64: error: 'minPulse' was not declared in this scope
colorrecog_arduino2:69: error: 'pulse' was not declared in this scope
colorrecog_arduino2:69: error: 'minPulse' was not declared in this scope
colorrecog_arduino2:75: error: 'pulse' was not declared in this scope
colorrecog_arduino2:75: error: 'minPulse' was not declared in this scope
colorrecog_arduino2:81: error: 'lastPulse' was not declared in this scope
colorrecog_arduino2:81: error: 'refreshTime' was not declared in this scope
colorrecog_arduino2:82: error: 'myServo' was not declared in this scope
colorrecog_arduino2:83: error: 'pulse' was not declared in this scope

I can't see why you should be getting those errors, from the code you posted.

ok. I'll mess with it for a bit and see if I can rectify this. did I replace the correct code? I'm still in one-step-forward/one-step-back territory.

I don’t know what code you replaced, but you need to stop trying to generate servo pulses with timers.

Here’s what my code looks like now. If not the pulse, then how do I move the servo in the way that I wan? (First post).

#include <Servo.h> 

Servo myservo;  // create servo object to control a servo 
int incomingByte = 0;
//{ 
//  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
//}


pinMode(myServo, OUTPUT);  // Set servo pin as an output pin
  Serial.begin(9600);

  // Set the motor position value to the middle  
  pulse = (maxPulse-minPulse)/2;          
  digitalWrite(myServo, HIGH);   // Turn the motor on
  delayMicroseconds(pulse);       // Length of the pulse sets the motor position
  digitalWrite(myServo, LOW);    // Turn the motor off

void loop() {
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();

    if(incomingByte = 1){  // 1 = value that is comming from processing
      if(pulse<maxPulse){
        pulse -=1;
      }
    }
    if(incomingByte = 2){  // 2 = value that is comming from processing
      if(pulse>minPulse){
        pulse -=1;
      }
    }
    if(incomingByte = 3){  // 3 = value that is comming from processing
      if(pulse>minPulse){
        pulse +=1;
      }
    }

    if(incomingByte = 4){  // 4 = value that is comming from processing
      if(pulse>minPulse){
        pulse +=1;
      }
    }

//    //  pulse the servo again if the refresh time (15 ms) have passed:
//    if (millis() - lastPulse > refreshTime) {
//      digitalWrite(myServo, HIGH);   // Turn the motor on
//      delayMicroseconds(pulse);       // Length of the pulse sets the motor position
//      delay(1000);
//
//      digitalWrite(myServo, LOW);    // Turn the motor off
//      lastPulse = millis();           // save the time of the last pulse
//      delay(1000);
//
//    }

  }
}

You need to have a "setup" and a "loop". You need to use the Servo "write" method, and stop explicitly writing to the pins you you've in your "attach". http://arduino.cc/en/Reference/Servo

Thanks for trying to help, AWOL. I have read the library doc as well as played with the examples and combed these boards for similar problems before I posted. It's possible I'm more of an idiot than is welcome on this Board but I have no idea what you're trying to suggest I do. I thought I had a setup and a loop. If you know of any documentation you can point me to that helps me set up this code I would really appreciate it. I'm sure what you're saying makes sense, but I'm not there yet.

All executable code needs to be in a function. This code:

pinMode(myServo, OUTPUT);  // Set servo pin as an output pin
  Serial.begin(9600);

  // Set the motor position value to the middle  
  pulse = (maxPulse-minPulse)/2;          
  digitalWrite(myServo, HIGH);   // Turn the motor on
  delayMicroseconds(pulse);       // Length of the pulse sets the motor position
  digitalWrite(myServo, LOW);    // Turn the motor off

is not.

All sketches need a minimum of two functions, setup() and loop(). The latest code you posted does not have a setup() function.

Setting the position of a servo (not one of those continuous rotation not-really-a-servo things), Servo myServo; before setup(), and

myServo.attach(9);
myServo.write(90);

in setup().

    if(incomingByte = 1){  // 1 = value that is comming from processing

= is an assignment operator. == is the equality operator. They are not interchangeable.

Unless you have some type of special servo, it appears you are confusing a servo with a motor. Servos use PPM control signals and not PWM. If you have standard hobby servos, you can use the below test code to get your servos moving.

// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control 
  Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 
    int n = readString.toInt();  //convert readString into a number

    // auto select appropriate value, copied from someone elses code.
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n);
    }

    readString=""; //empty for next input
  } 
}