Go Down

Topic: best way to send pot over xbee for servo control? (Read 2205 times) previous topic - next topic

merkzilla

im looking to simply send my pot. data over xbee to control my servo on the other side without interfering with my other controls heres my tx side
Code: [Select]
#include <SoftwareSerial.h>
SoftwareSerial mySerial(11, 12); // RX, TX
int potpin = 0;  // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
const int SW1 = 8; //forward
const int SW2 = 9; //backward
const int SW3 = 3; //left
const int SW4 = 2; //right
void setup()
{
  pinMode(SW1, INPUT_PULLUP);
  pinMode(SW2, INPUT_PULLUP);
  pinMode(SW3, INPUT_PULLUP);
  pinMode(SW4, INPUT_PULLUP);
  //pinMode(Light, INPUT);
  Serial.begin(9600);
mySerial.begin(9600);
}
void loop(void)
{
  byte data = 0;
  if (digitalRead(SW1) == HIGH)
  {
    data += 1;
  }
  if (digitalRead(SW2) == HIGH)
  {
    data += 2;
  }
  if (digitalRead(SW3) == HIGH)
  {
    data += 4;
  }
  if (digitalRead(SW4) == HIGH)
  {
    data += 8;
  }
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 180); 
  if(data > 0)
  {
    delay(50);
  mySerial.write(data);
 Serial.print(data);
  }

}

Code: [Select]
#include <MotorDriver.h>
#include <Servo.h>
Servo myservo;
byte data = 0;
void setup()
{
  motordriver.init();
  motordriver.setSpeed(250, MOTORB);
  motordriver.setSpeed(250, MOTORA); // steering motor
myservo.attach(4);
  Serial.begin(9600);
}
void loop()
{
  if (Serial.available() > 0)
  {
    data = Serial.read();
  }
  if (data == 1)
  {
    motordriver.goForward();
  }
  else if (data == 2)
  {
    motordriver.goBackward();
  }
  else if (data == 4)
  {
    motordriver.goLeft();
  }
  else if (data == 8)
  {
    motordriver.goRight();
  }
  else if (data == 5)
  {
    motordriver.goForward();
    motordriver.goLeft();
  }
  else if (data == 9)
  {
    motordriver.goForward();
    motordriver.goRight();
  }
  else if (data == 6)
  {
    motordriver.goBackward();
    motordriver.goLeft();
  }
  else if (data == 10)
  {
    motordriver.goBackward();
    motordriver.goRight();
  }
  else
  {
    motordriver.stop();
  }
  delay(50);
  if (data > 0)
  {
    Serial.print("data=");
    Serial.println(data, DEC);
  }
  data -= data;

}

zoomkat

What is your specific question or issue? What does not work with your code?
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

merkzilla

im just unsure of how to send it and then decifer it on the other end like would i just myserial.write(map);?

zoomkat

I made the below code to do something similar with joysticks and servos. Easiest to develop receiving code first, then develop sending code that works with the receiving code.

tx code

Code: [Select]

//zoomkat multi pot/servo test 3-23-13
//includes dead band for testing and limit servo hunting
//view output using the serial monitor

#include <Servo.h>
Servo myservo1;  //declare servos
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;

int potpin1 = 0;  //analog input pin A0
int potpin2 = 1;
int potpin3 = 2;
int potpin4 = 3;
int potpin5 = 4;

int newval1, oldval1;  //pot input values
int newval2, oldval2;
int newval3, oldval3;
int newval4, oldval4;
int newval5, oldval5;

void setup()
{
  Serial.begin(9600); 
  myservo1.attach(2); 
  myservo2.attach(3);
  myservo3.attach(4);
  myservo4.attach(5);
  myservo5.attach(6);
  Serial.println("testing multi pot servo"); 
}

void loop()
{
  newval1 = analogRead(potpin1);           
  newval1 = map(newval1, 0, 1023, 0, 179);
  if (newval1 < (oldval1-2) || newval1 > (oldval1+2)){ //dead band
    myservo1.write(newval1); //position the servo
    Serial.print(newval1); //print the new value for testing
    Serial.print("a,");
    oldval1=newval1; //set the current old value
  }

  newval2 = analogRead(potpin2);
  newval2 = map(newval2, 0, 1023, 0, 179);
  if (newval2 < (oldval2-2) || newval2 > (oldval2+2)){ 
    myservo2.write(newval2);
    Serial.print(newval2);
    Serial.print("b,");
    oldval2=newval2;
  }

  newval3 = analogRead(potpin3);           
  newval3 = map(newval3, 0, 1023, 0, 179);
  if (newval1 < (oldval1-2) || newval3 > (oldval3+2)){ 
    myservo1.write(newval3);
    Serial.print(newval3);
    Serial.print("c,");
    oldval3=newval3;
  }

  newval4 = analogRead(potpin4);           
  newval4 = map(newval4, 0, 1023, 0, 179);
  if (newval1 < (oldval1-2) || newval4 > (oldval4+2)){ 
    myservo1.write(newval4);
    Serial.print(newval4);
    Serial.print("d,");
    oldval4=newval4;
  }

  newval5 = analogRead(potpin5);           
  newval5 = map(newval5, 0, 1023, 0, 179);
  if (newval1 < (oldval5-2) || newval5 > (oldval5+2)){ 
    myservo1.write(newval5);
    Serial.print(newval5);
    Serial.print("e,");
    oldval5=newval5;
  }
  delay(50);  //to slow loop for testing, adjust as needed
}




rx code

Code: [Select]

//zoomkat 11-22-12 simple delimited ',' string parse
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h>
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo

void setup() {
  Serial.begin(9600);

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  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);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    } 
    else {     
      readString += c; //makes the string readString
    }
  }
}

Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

merkzilla


zoomkat

so servo write on the tx side????
Yes, so the servos can be directly connected to the tx arduino for testing and verification that the tx setup is working as desired. Note that below the servo write is also the serial write functions used for transmission to the rx arduino via the serial port. The serial monitor can be used to test and verify the functioning of the rx arduino
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

merkzilla

oh ok thank you i was trying to send the values without messing up my movement of the car 1,2,4,8 with the servo values

merkzilla

so everything checks out on my tx side (i think lol) but on my rx side things arnt going to well....the servo moves sometimes but so slightly and eratic i dont think its from data received. i think my movement data is getting mixed in the bunch somewhere
tx
Code: [Select]
#include <SoftwareSerial.h>
#include <Servo.h>
Servo myservo;  //declare servos
SoftwareSerial mySerial(11, 12); // RX, TX
int potpin = A0;  // analog pin used to connect the potentiometer
int newval, oldval; // variable to read the value from the analog pin
const int SW1 = 8; //forward
const int SW2 = 9; //backward
const int SW3 = 3; //left
const int SW4 = 2; //right
void setup()
{
  myservo.attach(A1);
  pinMode(SW1, INPUT_PULLUP);
  pinMode(SW2, INPUT_PULLUP);
  pinMode(SW3, INPUT_PULLUP);
  pinMode(SW4, INPUT_PULLUP);
  //pinMode(Light, INPUT);
  Serial.begin(9600);
mySerial.begin(9600);
}
void loop(void)
{
  int data = 0;
  if (digitalRead(SW1) == HIGH)
  {
    data += 1;
  }
  if (digitalRead(SW2) == HIGH)
  {
    data += 2;
  }
  if (digitalRead(SW3) == HIGH)
  {
    data += 4;
  }
  if (digitalRead(SW4) == HIGH)
  {
    data += 8;
  }
  newval = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  newval = map(newval, 0, 1023, 0, 180);
   if (newval < (oldval-2) || newval > (oldval+2))
   { //dead band
    myservo.write(newval); //position the servo
    mySerial.write(newval); //print the new value for testing
    mySerial.write("a,");
    Serial.print(newval); //print the new value for testing
    Serial.print("a,");
    oldval=newval; //set the current old value
   }
  if(data > 0)
  {
    delay(50);
  mySerial.write(data);
 Serial.print(data);
  }

}

rx
Code: [Select]
#include <MotorDriver.h>
#include <Servo.h>
String readString;
Servo myservo;
int data = 0;
void setup()
{
  motordriver.init();
  motordriver.setSpeed(250, MOTORB);
  motordriver.setSpeed(250, MOTORA); // steering motor
myservo.attach(40);
  Serial.begin(9600);
}
void loop()
{
  if (Serial.available())
  {
   char data = Serial.read();
    //gets one byte from serial buffer
    //camera coontrol
    if (data == ',') {
      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);
          if(readString.indexOf('a') >0) myservo.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservo.write(n);
        }
         readString=""; //clears variable for new input
      }
    } 
    else {     
      readString += data; //makes the string readString
    }
  }
  //movement control
  if (data == 1)
  {
    motordriver.goForward();
  }
  else if (data == 2)
  {
    motordriver.goBackward();
  }
  else if (data == 4)
  {
    motordriver.goLeft();
  }
  else if (data == 8)
  {
    motordriver.goRight();
  }
  else if (data == 5)
  {
    motordriver.goForward();
    motordriver.goLeft();
  }
  else if (data == 9)
  {
    motordriver.goForward();
    motordriver.goRight();
  }
  else if (data == 6)
  {
    motordriver.goBackward();
    motordriver.goLeft();
  }
  else if (data == 10)
  {
    motordriver.goBackward();
    motordriver.goRight();
  }
  else
  {
    motordriver.stop();
  }
  delay(50);
    Serial.print("data=");
    Serial.println(data, DEC);
 

}

zoomkat

Quote
but on my rx side things arnt going to well....the servo moves sometimes but so slightly and eratic i dont think its from data received.
Probably 90% of servo problems are due to trying to power the servo from the arduino instead of an external power supply, or if the external power supply is used, the grounds are not properly connected. Why do you have a 50ms delay in the receiving code?
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

merkzilla

i just used the servo code and it still isnt working on the rx side

tx
Code: [Select]

//zoomkat multi pot/servo test 3-23-13
//includes dead band for testing and limit servo hunting
//view output using the serial monitor
#include <SoftwareSerial.h>
#include <Servo.h>
Servo myservo1;  //declare servos
SoftwareSerial mySerial(11, 12); // RX, TX

int potpin1 = A0;  //analog input pin A0

int newval1, oldval1;  //pot input values


void setup()
{
  Serial.begin(9600); 
  myservo1.attach(A1); 
  Serial.println("testing multi pot servo"); 
}

void loop()
{
  newval1 = analogRead(potpin1);           
  newval1 = map(newval1, 0, 1023, 0, 179);
  if (newval1 < (oldval1-2) || newval1 > (oldval1+2)){ //dead band
    myservo1.write(newval1); //position the servo
    mySerial.write(newval1); //print the new value for testing
    mySerial.write("a,");
    oldval1=newval1; //set the current old value
  }
  delay(50);  //to slow loop for testing, adjust as needed
}

Code: [Select]

//zoomkat 11-22-12 simple delimited ',' string parse
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added
// Powering a servo from the arduino usually *DOES NOT WORK*.

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

void setup() {
  Serial.begin(9600);

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(A0);  //the pin for the servoa control
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  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);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          }
         readString=""; //clears variable for new input
      }
    } 
    else {     
      readString += c; //makes the string readString
    }
  }
}

zoomkat

Very basic servo test code to see if you can get your servo to position.

Code: [Select]

//zoomkat 7-30-10 serial servo test
//type servo position 0 to 180 in serial monitor
// 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.attach(9);
  Serial.println("servo-test"); // 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
    Serial.println(n); //so you can see the integer
    myservo.write(n);
    readString="";
  }
}

Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

merkzilla

keep this for tx side with that
Code: [Select]

//zoomkat multi pot/servo test 3-23-13
//includes dead band for testing and limit servo hunting
//view output using the serial monitor
#include <SoftwareSerial.h>
#include <Servo.h>
Servo myservo1;  //declare servos
SoftwareSerial mySerial(11, 12); // RX, TX

int potpin1 = A0;  //analog input pin A0

int newval1, oldval1;  //pot input values


void setup()
{
  Serial.begin(9600); 
  myservo1.attach(A1); 
  Serial.println("testing multi pot servo"); 
}

void loop()
{
  newval1 = analogRead(potpin1);           
  newval1 = map(newval1, 0, 1023, 0, 179);
  if (newval1 < (oldval1-2) || newval1 > (oldval1+2)){ //dead band
    myservo1.write(newval1); //position the servo
    mySerial.write(newval1); //print the new value for testing
    mySerial.write("a,");
    oldval1=newval1; //set the current old value
  }
  delay(50);  //to slow loop for testing, adjust as needed
}

zoomkat

The below may have timer conflicts, so you may need to sort that out if you are going to use SoftwareSerial.

Code: [Select]
#include <SoftwareSerial.h>
#include <Servo.h>
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

merkzilla

my main issue is getting it to work on my rx side it works on my tx side fine but for some reason it isnt carrying over

merkzilla

movement data is 1-10 so cant i simply say if there val availible from the potentiameter to add 11 (so not to confused with movement data) to the val and send it then on the rx side say if the data received is > than 10 cammove = data - 10 then myservo.write camMove???

Go Up