Go Down

Topic: controling servos between 2 arduino's over serial (Read 367 times) previous topic - next topic

corosus

hi

I'm looking into building a serup where 1 arduino reads a couple of Pot meters and sends the data to an other arduino that controlls and equal amount of servos. (they communicate over xbees)

right now i'm trying with just one servo/pot setup and a serial line

i've set up an arduino with a pot meter and a servo and modified the servo-knob example to also output to serial

Quote


// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  Serial.begin(9600);
}

void loop()
{
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);  // sets the servo position according to the scaled value
  Serial.print(val);
  delay(15);                           // waits for the servo to get there
}


and wrote a skech to read the serial  and write it to a servo



Quote

#include <Servo.h>

int incomingByte = 0;   // for incoming serial data

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

void setup() {
        Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
         myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {

        // send data only when you receive data:
        if (Serial.available() > 0) {
                // read the incoming byte:
                incomingByte = Serial.read();
                myservo.write(pos);
        Serial.print(pos);
        }

}




i suspect i am doing something wrong with how i send/recieve the serial data, but can't for the life of me figure it out
anny ideas welkom
kind regards

corosus

zoomkat

On the sending end, you probably need to add a delimiter (kike the , below) to the val sent. On the receiving end you might use code like below.

Code: [Select]

//zoomkat 3-5-12 simple delimited ',' string parce
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter

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-delomit-test-22-dual-input"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like 700, or 1500, or 2000,
  //or like 30, or 90, or 180,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        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);
        }

        //do stuff with the captured readString
        readString=""; //clears variable for new input
      }
    } 
    else {     
      readString += c; //makes the string readString
    }
  }
}

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

Go Up