Keyboard control of two dc motors via xbee

Hi

i want to control two dc motors via xbee, using keyboard, currently i am able to do this without the help of an xbee, but since i am looking for a wireless control of the robot i need the code to do the following
eg: ‘w’ moves forward
‘a’ moves left
‘d’ moves right
‘x’ moves back
‘s’ stops
i am posting the code for controlling it on the arduino, but i am not sure as to how i should split this code for the receiver and the sender ends

could anyone please help

int EN1 = 6;  
int EN2 = 5;  //Roboduino Motor shield uses Pin 9
int IN1 = 7;
int IN2 = 4; //Latest version use pin 4 instead of pin 8
  
 
 
void Motor1(int pwm, boolean reverse)
        {
          analogWrite(EN1,pwm); //set pwm control, 0 for stop, and 255 for maximum speed
         if(reverse)
         { 
          digitalWrite(IN1,HIGH);    
         }
        else
        {
          digitalWrite(IN1,LOW);    
         }
        }  
         
void Motor2(int pwm, boolean reverse)
        {
          analogWrite(EN2,pwm);
         if(reverse)
         { 
          digitalWrite(IN2,HIGH);    
         }
        else
        {
          digitalWrite(IN2,LOW);    
         }
        }  
        
void setup() 
{ 
    int i;
   // for(i=6;i<=9;i++) //For Roboduino Motor Shield
   // pinMode(i, OUTPUT);  //set pin 6,7,8,9 to output mode
 
    for(i=4;i<=7;i++)  //For Arduino Motor Shield
    pinMode(i, OUTPUT);  //set pin 4,5,6,7 to output mode
 
    Serial.begin(9600);   
} 
 
 
void loop() 
{ 
  int x,delay_en;
  char val;
  while(1)
  {
    val = Serial.read();
    if(val!=-1)
       {
          switch(val)
           {
             case 'w'://Move ahead
                        Motor1(255,true);  //You can change the speed, such as Motor(50,true)
                        Motor2(255,true);
                       
                         break;
             case 'x'://move back
                        Motor1(255,false);
                        Motor2(255,false);
                         break;
             case 'a'://turn left
                        Motor1(255,false);
                        Motor2(255,true);
                         break;       
             case 'd'://turn right
                        Motor1(255,true);
                        Motor2(255,false);
                        break;   
               case 's'://stop
                        Motor1(0,false);
                        Motor2(0,false);
                         break;
                                   
           }     
         
       }
            
  }                           
}

thanks

but i am not sure as to how i should split this code for the receiver and the sender ends

Why not? Surely the things that the sender should do are obvious, as well as the things that the receiver should do.

Frankly, it looks like all that code belongs on the receiver Arduino. The XBees are just the mechanism to get the data from point A (the PC) to point B (the Arduino).

What XBees do you have? How have you configured them? How are they connected to the Arduino and to the PC? What code is running on the PC?

i am using xbee series 1, i used puTTy to configure them, i am using a motor shield from Dfrobots to control the motors, i am also using two sparkfun xbee shields. i am running arduino 1.0 version

if the entire code is for the receiver what should the sender be commanding the receiver to do

thanks

i used puTTy to configure them

I don't care if you use putty, X-CTU, or magic pixies dust to get the configuration done. That doesn't matter. What DOES matter is exactly what you configured. What parameters did you set? To what values?

i am also using two sparkfun xbee shields

Lovely. Which ones?

what should the sender be commanding the receiver to do

Whatever you want the receiver to do. The receiver code currently expects some serial data to make it do it's happy dance. The sender should send that serial data.

Quote
i am also using two sparkfun xbee shields
Lovely. Which ones?

i just set the id of each xbee and set up a PAN thats all

The sender should send that serial data.

Can you help me get started or point to me some places where i can learn how to program these and how to send and receive data please

I am new to xbee

i just set the id of each xbee

Some numbers? Is that too much to ask?

You need to set MY and DL on each XBee. MY on one is DL on the other. Neither should be 0 or 0xFFFF.

ATMY 1000
ATDL 1001
ATID 1111
for the first one
and

ATMY 1001
ATDL 1000
ATID 1111
for the second xbee

so does that mean i have to reconfigure my xbees

so does that mean i have to reconfigure my xbees

No, they should be fine. Why do you think you need to reconfigure them?

i thought i had to because

Neither should be 0 or 0xFFFF.

that just freaked me out

allen121:
Can you help me get started or point to me some places where i can learn how to program these and how to send and receive data please
I am new to xbee

From the XBee side, you only need to key the characters from the keyboard into the terminal program. From the receiving side, the arduino needs to know what to do when it receives these characters. Some simple 'if' and 'else' loops should work.

Regarding on how to program XBees, I don't think there's an easier tutorial than this: Arduino and Xbee wireless setup (Video 1) - YouTube

Thanks a lot deathrow

i think i dint explain the question properly

i want to control the motors via two xbees not one so when i press 'w' the message is sent to one xbee which tells the other xbee to move the motors, which means that i am using two arduino uno and two xbee shields

in this case will the program be simple if statements as said below

Some simple 'if' and 'else' loops should work.

allen121:
Thanks a lot deathrow

i think i dint explain the question properly

i want to control the motors via two xbees not one so when i press 'w' the message is sent to one xbee which tells the other xbee to move the motors, which means that i am using two arduino uno and two xbee shields

in this case will the program be simple if statements as said below

Some simple 'if' and 'else' loops should work.

Yes, I understood what you want to achieve. If you're pressing 'w' on the keyboard to send a command to the robot, then you don't need two arduinos. Just one arduino on the robot will be needed. The XBee can relay the information from the laptop (or Terminal program) directly to the other XBee which will be connected to the arduino. That video should make it very clear how to achieve something like this :slight_smile:

yea i get that but i already have two xbees, can i bypass one and directly send messages to the other xbee no i guess, in that video he is connecting an xbee directly to the pc right will that be an issue, i guess so, i am using the xbees to send data to control servos as well

If you have an Arduino with XBee on the PC end, you can still read data from the PC using Serial, and send data to the XBee using SoftwareSerial, if you move the switch on the XBee shield on the sending end. No changes are needed on the receiving end, although you could do the same on that end - using SoftwareSerial to talk to the XBee and Serial to talk to the PC, until you KNOW that the XBees are sharing data correctly.

while doing that will i be able to control three servos using three different pots, if thats the case its awesome

while doing that will i be able to control three servos using three different pots, if thats the case its awesome

The hard part is getting data back and forth. What that data is, and what you do with it, are the easy part.

so are you trying to tell me that i will not be able to control two motors and three servos all at the same time,
or will i have to keep switching the UART and DLINE on the shield back and forth

so are you trying to tell me that i will not be able to control two motors and three servos all at the same time,

No, I'm not. If you send the right data, you will be able to.

or will i have to keep switching the UART and DLINE on the shield back and forth

Not while the sketch is running.

i think you dint get my point, from what i understood either you can use xbees to control dc motors using the keyboard
or
you can control three servos using pots

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