i want to know if i can send for eg. 'w' from my keyboard to xbee1 which tells xbee2 to move the dc motors, and at the same time move a servo connected to xbee2 using a pot connected to xbee1
Yes, you can. Where the Arduino gets the data is irrelevant. As long as it has data, it can send it to the XBee, using the SoftwareSerial instance. One of the places it can get data is from the PC using the Serial instance. Another is from reading pots.
i have a code with me for the sender and receiver can somebody please help me
i dont understand the sender part of the code
send
const byte PIN_ANALOG_X = 0;
const byte PIN_ANALOG_Y = 1;
const int X_THRESHOLD_LOW = 450; //I have the contrains real big so there's less chance for
const int X_THRESHOLD_HIGH = 550; //analog misinterpretation.
const int Y_THRESHOLD_LOW = 450;
const int Y_THRESHOLD_HIGH = 550;
int x_position;
int y_position;
int x_direction;
int y_direction;
void setup() {
Serial.begin(9600);
}
void loop () {
x_direction = 0;
y_direction = 0;
x_position = analogRead(PIN_ANALOG_X);
y_position = analogRead(PIN_ANALOG_Y);
if (x_position > X_THRESHOLD_HIGH) {
x_direction = 1;
} else if (x_position < X_THRESHOLD_LOW) {
x_direction = -1;
}
if (y_position > Y_THRESHOLD_HIGH) {
y_direction = 1;
} else if (y_position < Y_THRESHOLD_LOW) {
y_direction = -1;
}
if (x_direction == -1) {
if (y_direction == -1) {
Serial.println("left-down");
} else if (y_direction == 0) {
Serial.println("a"); //the A is whats making it go left
} else {
// y_direction == 1
Serial.println("left-up");
}
} else if (x_direction == 0) {
if (y_direction == -1) {
Serial.println("s"); // S is for reverse
} else if (y_direction == 0) {
Serial.println("f"); // F is for Stop--it sends this until something else is sent
} else {
// y_direction == 1
Serial.println("w"); // W is forward
}
} else {
// x_direction == 1
if (y_direction == -1) {
Serial.println("right-down");
} else if (y_direction == 0) {
Serial.println("d"); // D is right
} else {
// y_direction == 1
Serial.println("right-up");
}
}}
receiver
/Code courtesy DFRobotShop Rover_Serial
int E1 = 6; //M1 Speed Control
int E2 = 5; //M2 Speed Control
int M1 = 7 ; //M1 Direction Control
int M2 = 4; //M2 Direction Control
int LED = 13;
void setup(void)
{
int i;
for(i=4;i<=7;i++)
pinMode(i, OUTPUT);
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop(void)
{
while (Serial.available() < 0) {} // Wait until a character is received
char val = Serial.read();
int leftspeed = 255; //255 is maximum speed
int rightspeed = 255;
switch(val) // Perform an action depending on the command
{
case 'w'://Move Forward
forward (leftspeed,rightspeed);
break;
case 's'://Move Backwards
reverse (leftspeed,rightspeed);
break;
case 'a'://Turn Left
left (leftspeed,rightspeed);
break;
case 'd'://Turn Right
right (leftspeed,rightspeed);
break;
case 'l'://LED
blink_LED();
break;
case 'f'://Stop
stop();
break;
default:
break;
}
}
void stop (void) //Stop
{
digitalWrite(E1,LOW);
digitalWrite(E2,LOW);
}
void forward (char a,char b)
{
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite (E2,b);
digitalWrite(M2,LOW);
}
void reverse (char a,char b)
{
analogWrite (E1,a);
digitalWrite(M1,HIGH);
analogWrite (E2,b);
digitalWrite(M2,HIGH);
}
void left (char a,char b)
{
analogWrite (E1,a);
digitalWrite(M1,HIGH);
analogWrite (E2,b);
digitalWrite(M2,LOW);
}
void right (char a,char b)
{
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite (E2,b);
digitalWrite(M2,HIGH);
}
void blink_LED ()
{
digitalWrite(LED, HIGH);
delay(200);
digitalWrite(LED, LOW);
delay(100);
digitalWrite(LED, HIGH);
delay(200);
digitalWrite(LED, LOW);
}