Help Needed!! Controling Servo Angle Trhough Xbee

hi everyone…
i just start using Arduino, and now i am trying to control servo angle with pushbutton wirelessly…

i use two Arduino UNO and two Xbee Module with Xbee Shield…
But it does’t work when i try to run it =( =(.Can anyone figure out what is wrong?
servo only moves 120 degrees and a few seconds later moving to 180 degrees even though the button has been pressed
here’s the transmitter code

const int buttonPin = 11; int buttonState; void setup() { pinMode(buttonPin, INPUT); digitalWrite( buttonPin, HIGH); Serial.begin(9600); } void loop() { buttonState = digitalRead(buttonPin); if (buttonState == LOW) { Serial.print("1"); } else { Serial.print("0"); } Serial.println(buttonState); delay(100); }

and the receiver

``#include <Servo.h>
#define right 120
#define left 10
int data;

int servoPin = 9;
Servo angelaServo;
void setup()
{
Serial.begin(9600);
angelaServo.attach(servoPin);
}
void loop()
{
if (Serial.available() > 0)
{
data = Serial.read();
}
if (data == 0)
{
angelaServo.write(right);
}
else
{
angelaServo.write(left);
}
}``

Any help appreciated. Thanks. :slight_smile:

Do not cross post. You posted this already.

After

    data = Serial.read();

have you tried printing data to see what you are actually getting ?

How about some Serial.prints so that you know which portions of code are being executed ?
What have you got the line ending set to in the Serial monitor ?

The transmitter sends “1” or “0” but in the receiver you check for an int value of 1 or 0. Are they ever going to be equal I wonder …

use two Arduino UNO

We know exactly which Arduino.

and two Xbee Module

but not which XBee...

with Xbee Shield...

or shield.

Nor do we have a clue as to how you configured them. If you followed Digi's advice that they work out of the box, then you should be on Digi's site asking them why the f**k they aren't working.

It Works now guys…
thanks for help :slight_smile:
here’s the code i use

transmitter

const int buttonPin = 8; const int ledPin = 13; int buttonState = 0; void setup() { Serial.begin(9600); pinMode(buttonPin, INPUT_PULLUP); } void loop() { buttonState = digitalRead(buttonPin); if (buttonState == HIGH) { Serial.write(1); } else { Serial.write(0); } delay(50); }

and the receiver

``#include <Servo.h>
#define up 100
#define down 170
char data;

int servoPin = 9;
Servo angelaServo;
void setup()
{
pinMode(servoPin, OUTPUT);
Serial.begin(9600);
angelaServo.attach(servoPin);
angelaServo.write(down);
}

void process_incoming_command(char cmd)
{
int speed = 100;
switch (cmd)
{
case ‘1’:
case 1:
angelaServo.write(up);
break;
case ‘0’:
case 0:
angelaServo.write(down);
break;

}
}
void loop()
{
if (Serial.available() >= 1)
{
char cmd = Serial.read();

process_incoming_command(cmd);
}
}``

it works perfectly, i have another question guys, what if i use additional sensors? potentiometer for example, how to do that?? thank you

case '1':
  case 1:

You still don't quite get it, do you ?

You still don't quite get it, do you ?

What do you expect from a person who can't even paste the code INSIDE the tags?

sorry all I am still a newbie, that's why I am here asking for your help, thank you for your help

[code] 

your sketch

[/code]