Controlling Servo with a potentiometer with two arduinos connected by XBees

Hi,

I am trying to control a servo with a potentiometer. I am using Xbees to connect the arduinos. I am using a Pro mini for the transmitter side that has a potentiometer connected to it and sending out pot data. For the reciever, I am using a uno and have the Xbee shield connected to it.

here is the code for the receiver:

#include <Servo.h>
Servo test;
int lift;
int newVal=0;
int oldVal=0;

void setup()
{
Serial.begin(9600);
test.attach(9);
pinMode(12, OUTPUT);

}
void loop()
{
while(Serial.available())
{
lift= Serial.parseInt();
lift=map(lift, 0, 1023, 14, 180);
newVal=lift;

if(oldVal != newVal)
{
test.write(lift);
oldVal=newVal;
delay(250);
}
}
}

for the transmitter:

int potval1=0;
int potpin1=0;
String output;

int button=0;
void setup()
{
Serial.begin(9600);
pinMode(2, INPUT);
}

void loop()
{
potval1= analogRead(potpin1);

String str1= String(potval1);
output= (str1+"\n");
Serial.print(output);
delay(250);
}

I get little twitches in the motor but not much else. Could I be sending the data too fast and maybe too much data for the servo to respond to? This is a shorter version of the code that I actually need to use. In the final codes, I am controlling three motors with three pots, but it uses the same idea and logic. I really need some help on this, please.

You might try just wiring the arduinos together first and get things working, then introduce the wireless part. I suggest you do the flow control on the tx arduino and just send the necessary pot changes when there are changes to limit transmission churn/flooding. I made the below test code for possibly using a joystick to control two servos over a network connection .

//zoomkat dual pot/servo test 12-29-12
//view output using the serial monitor

#include <Servo.h> 
Servo myservoS1;
Servo myservoS2;

int potpinS1 = 0;  //analog input pin A0
int potpinS2 = 1;

int newvalS1, oldvalS1;
int newvalS2, oldvalS2;

void setup() 
{
  Serial.begin(9600);  
  myservoS1.attach(2);  
  myservoS2.attach(3);
  Serial.println("testing dual pot servo");  
}

void loop() 
{ 
  newvalS1 = analogRead(potpinS1);           
  newvalS1 = map(newvalS1, 0, 1023, 0, 179); 
  if (newvalS1 < (oldvalS1-2) || newvalS1 > (oldvalS1+2)){  
    myservoS1.write(newvalS1);
    Serial.print("1- ");
    Serial.println(newvalS1);
    oldvalS1=newvalS1;
  }

  newvalS2 = analogRead(potpinS2);
  newvalS2 = map(newvalS2, 0, 1023, 0, 179);
  if (newvalS2 < (oldvalS2-2) || newvalS2 > (oldvalS2+2)){  
    myservoS2.write(newvalS2);
    Serial.print("2- ");    
    Serial.println(newvalS2);
    oldvalS2=newvalS2;
  }
  delay(50); //slow down looping to better read serial monitor 
}

I have done a wired connection already and it works perfectly, a very simple code. I implemented an if statement earlier to check for reoccurring data, so it didn’t flood the servo, but it still didn’t work like the wired version. It needs to have fluid motion, because this servo is going to control a direction fin on a model hovercraft.

I implemented an if statement earlier to check for reoccurring data, so it didn’t flood the servo, but it still didn’t work like the wired version

You can’t “flood” a servo. Your “If” is in the receiver code, so you probably are still sending quite a spew between the xbees (you don’t show the tx code).

void loop()
{
   potval1= analogRead(potpin1);
 
   String str1= String(potval1);
   output= (str1+"\n");
   Serial.print(output);
   delay(250);     
}

The String constructor is converting potval1 to a string, and wrapping it in a class. Then, the second constructor is creating a copy of the original string and appending a carriage return. Then, the Serial.print() function needs to unwrap the String object to get to the string to send.

Serial.println(potval1) will convert the value to a string and append a carriage return and line feed without the need to piss away resources on the String class.

The parseInt() method will stop reading when it sees either the carriage return or the carriage return and line feed, so there is no advantage with not sending the line feed.