Pages: [1]   Go Down
Author Topic: Using Xbee S1 to communicate between two arduinos.  (Read 1027 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to use two XBee modules to send commands to the other arduino to do something.
The code I have (see code bellow) works and I wanted to use it so I can use the same keypad attached to my first arduino to control the other arduino to do exactly the same thing as the code shows.
I configured the two XBees (set the Pan ID same for both, Channel 11 for bought, Baud rate = 115200 for bought) and don't know what to do to the code. So I just uploaded it to both Arduinos but didn't work.
I matched the baud rate to my computer. the code has the serial print command.
 I guess I am not understanding something. Any suggestion is helpful.
Here is the code
Code:
  #include <Keypad.h>

const byte IOXpin = 2;

const byte ROWS = 4; // rows of the keypad
const byte COLS = 4; //columns of the kepad
char keys[ROWS][COLS] =
  {
    { '1','2','3','A'},
    { '4','5','6','B' },
    { '7','8','9','C' },
    { '*','0','#', 'D' }
  };

byte rowPins [ROWS] = { 3, 4, 5, 6};
byte colPins [COLS] = { 7, 8, 9,10 };

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


void setup()
{
  pinMode(IOXpin, OUTPUT); 
  digitalWrite(IOXpin, HIGH);
  Serial.begin (115200);   
                         


void processNumber (const unsigned long amount)
  {
  Serial.print ("Dispensing ");
  Serial.print (amount);
  Serial.println (" uL.");

 
 
  if (amount < 19 || amount > 300)
    {
    Serial.println ("Invalid Amount!!");
    return;   
    }
   
  unsigned long startTime = millis ();
  unsigned long runTime = amount * 100;   // assume 0.1 secs per uL
 
  digitalWrite (IOXpin, LOW);
  while (millis () - startTime < runTime)
    {
    if (kpd.getKey() == '*')  // cancel button
       {
       Serial.println ("Cancelled.");
       break; 
       } 
    }  //
  digitalWrite (IOXpin, HIGH);

  Serial.println ("Done.");
 
  }

void handleKeypress (const char key)
  {
  Serial.print ("Entered: ");
  Serial.println (key);

  static unsigned long receivedNumber = 0;
 
  switch (key)
    {
    case '#': 
      processNumber (receivedNumber);
      receivedNumber = 0;
      break;
     
    case '0' ... '9':
      receivedNumber *= 10;
      receivedNumber += key - '0';
      break;

    default:
      Serial.println ("Unexpected input!");
      break;
     
    } // end of switch 
   
  }  // end of handleKeypress
 

void loop()
{
  byte key = kpd.getKey();
  if (key)
    handleKeypress (key);
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46090
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Series 1 XBees need to have MY and DL set. MY on one must match DL on the other, and neither should be 0. Did you set these?
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What's the MY stand for?
Logged

Western New York, USA
Offline Offline
Faraday Member
**
Karma: 25
Posts: 4111
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I am trying to use two XBee modules to send commands to the other arduino to do something.

In this scenario the XBees are substituting a radio link for the wires that would otherwise interconnect your two Arduinos. 

Get your program working properly using wires first, then deal with the XBees.



Quote
What's the MY stand for?

Ask 'Digi' or look in their datasheet.

Don
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Very good idea flores. Thank you
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry for dispelling your name. Very good idea Floresta. I guess by the two wires you meant RX and TX right?
Well here what I did:
I tried to see if it'll first work with a simple program like the one made by Jeremy blum. where he used two arduinos (first one to read a pot input and the second to get the input of the pot from the first and turn servo motor on the second arduino) His code is correct, and verified, but when I apply it to mine the motor just goes crazy when I turn the pot and it doesn't stop. It just vibrates like a phone. No turning as you turn the pot.
Here is the code of Jeremy just that you see what I am talking about:
Code:
/*The pot in the first Arduino
int potPin = 0;

void setup()
{
 
  Serial.begin(115200);
}

void loop()
{
  int val = map(analogRead(potPin), 0, 1023, 0, 9);
  Serial.println(val);
  delay(50);
 
}
Code:
//The servo in the second arduino

#include <Servo.h>

int servoPin = 9;

Servo mysServo;

void setup()
{
  Serial.begin(115200);
 
  mysServo.attach(servoPin);
 
  delay(500);
}

void loop()


  while( Serial.available() == 0);
  int data = Serial.read() -'0';

  int pos = map(data, 0, 9, 0, 180);
  pos = constrain(pos, 0, 180);

  //turn
 
  Serial.println(pos);
  mysServo.write(pos);
  Serial.flush(); 
 
 
}
please provide some feedback.
Thanks
Logged

Western New York, USA
Offline Offline
Faraday Member
**
Karma: 25
Posts: 4111
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  Serial.println(val);

Have you tried   Serial.print(val); ?

Don
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46090
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  Serial.flush(); 
Throw away random amounts of unread data (0023 and earlier) or wait until all pending data has been sent (1.0+). Why do you need to do either one?
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

but why the same code worked for Jeremy and not for me. I did the same thing. This means I have some problems with the Xbees not the code of the arduino
Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11173
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

but why the same code worked for Jeremy and not for me. I did the same thing. This means I have some problems with the Xbees not the code of the arduino

No, it probably means that you have some problems with the Xbees as well as the problems PaulS pointed out with the code of the Arduino.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: