Pages: [1]   Go Down
Author Topic: Processing and Arduino talking but Arduino and her servos not obeying  (Read 362 times)
0 Members and 1 Guest are viewing this topic.
left coast us
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a processing sketch which is working fine and an Arduino sketch that takes a command for incomingByte and is supposed to run a servo forward or backward accordingly (in response to. When I run it through Processing, SOMETHING is going on but not what I want (servo should run backward on incomingByte=1 and forward on the rest and then pause...  Any clues? It's teeth gnashing time here. The Processing sketch is attached. Here's my Arduino sketch:

#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(15);                       // waits 15ms for the servo to reach the position
      }
      for(pulse = 180; pulse >=180; pulse-=1)     // goes from 180 degrees to 0 degrees
      {                               
        myservo.write(pos);              // tell servo to go to position in variable 'pos'
        delay(15);                       // waits 15ms 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;
              delay(1);
            }
          }
          //
          //  pulse the servo again if the refresh time (15 ms) have passed:
            if (millis() - lastPulse > refreshTime) {
             digitalWrite(myServo, LOW);   // Turn the motor on
             delayMicroseconds(pulse);       // Length of the pulse sets the motor position
              digitalWrite(myServo, LOW);    // Turn the motor off
             lastPulse = millis();           // save the time of the last pulse
        }
      }
    }
  }
}
}



* color_recog_2.pde (3.73 KB - downloaded 3 times.)
Logged

left coast us
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I should add that it's an Arduino Uno...
Logged

Leighton Buzzard, UK
Offline Offline
Edison Member
*
Karma: 21
Posts: 1339
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a processing sketch which is working fine and an Arduino sketch that takes a command for incomingByte and is supposed to run a servo forward or backward accordingly (in response to. When I run it through Processing, SOMETHING is going on but not what I want (servo should run backward on incomingByte=1 and forward on the rest and then pause...  Any clues? It's teeth gnashing time here. The Processing sketch is attached. Here's my Arduino sketch:

Code:
#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();

    //=================================================
    // why do you swing the servo + then - 180 degrees?
    // this happens every time you get a byte of data!
    //=================================================
    {
      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(15);                       // waits 15ms for the servo to reach the position
      }

      //========================================
      // I think this should be
      // for (pulse = 180; pulse >=0; pulse -=1)
      //========================================

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


    //================================
    // are you expecting 1 or character "1"?
    // as noted later you use == to test!
    //================================
    if(incomingByte == 1){  // 1 = value that is comming from processing
      if(pulse<maxPulse){
        pulse +=1;
      }
    }

    //=======================================================================
    // we already know incomingByte = 1, so it would never be any other value!
    // so nothing south of here would ever get to execute
    // wonder why you commented out the "}" above (and below)
    // I put them back!
    //=======================================================================


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

    //==============================================
    // code for value 2, 4 and 4 seem to be the same
    //==============================================
    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;
        delay(1);
      }
    }
    //
    //  pulse the servo again if the refresh time (15 ms) have passed:
    if (millis() - lastPulse > refreshTime) {
      digitalWrite(myServo, LOW);   // Turn the motor on
      delayMicroseconds(pulse);       // Length of the pulse sets the motor position
      digitalWrite(myServo, LOW);    // Turn the motor off
      lastPulse = millis();           // save the time of the last pulse
    }
  }
}

well first thing I did was put it inside [ code] ... [ /code] !!

then I added some comments
finally I replaced the "}" in your tests at the end
without them only the "incomingByte=1" part would ever get executed

are you sure you mean 1 and not character 1?
depends how you are generating the data
« Last Edit: November 21, 2011, 06:55:34 am by mmcp42 » Logged

there are only 10 types of people
them that understands binary
and them that doesn't

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    {
      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(15);                       // waits 15ms for the servo to reach the position
      }
      for(pulse = 180; pulse >=180; pulse-=1)     // goes from 180 degrees to 0 degrees
      {                               
        myservo.write(pos);              // tell servo to go to position in variable 'pos'
        delay(15);                       // waits 15ms for the servo to reach the position
      }
    }
If there is serial data, wave the servo through its full range of motion, slowly. Well, OK. I can't see the purpose of that, but, hey, it appears to be what you want to do.

Code:
    if(incomingByte = 1){  // 1 = value that is comming from processing
This is assigning the value 1 to incomingByte, not comparing anything. Comparison uses ==.
Logged

left coast us
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all. I guess I should say that the incomingBytes are responding to a Processing sketch that is looking at a particular color (RG or B) and whatever the iteration of that color, it either reverses or advances. I wanted it to stop briefly after each move but couldn't figure out how to do that either (yes, I'm newb-squared)
Logged

Pages: [1]   Go Up
Jump to: