Society of Robots - Robot Forum

Mechanics and Construction => Mechanics and Construction => Topic started by: Eithman on May 23, 2012, 02:54:15 PM

Title: Xbee problems
Post by: Eithman on May 23, 2012, 02:54:15 PM
I am trying to make a simple device where if I push a button on one Arduino, it sends a signal to the other and turns on an led. I used the Arduino Uno with an Xbee shield to do this. To program the Xbee's I followed the instructions in this video ( Tutorial 09 for Arduino: Wireless Communication (http://www.youtube.com/watch?v=vKVNmA8C6m8#ws) ) <--- about 4 minutes in. Here is the code I used for the transmitting Arduino:

int readpin = 8;

void setup()  {
  pinMode(readpin, INPUT);
  Serial.begin(9600);
}

void loop()  {
 
 if ( digitalRead(readpin) == HIGH)
{
  Serial.println(1);
}
 else
{
   Serial.println(2);
}
  delay(500);
   }


And here is the code for the receiving Arduino.

 int ledPin = 13;

void setup()  {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop()  {

  while(Serial.available() == 0);
 
  int data = Serial.read() - '0';
 
if ( data == 1)
{
  digitalWrite(ledPin, HIGH);
}
 else
 {
   digitalWrite(ledPin, LOW);
 }
  delay(500);
   }

Here is a link to how I wired up both Arduinos. https://docs.google.com/document/d/1gutGrC1MvyxaZVaXN6wNnfweBJIWJN1LM7k3FR4oq2I/edit (https://docs.google.com/document/d/1gutGrC1MvyxaZVaXN6wNnfweBJIWJN1LM7k3FR4oq2I/edit)

I know the first Arduino works for sure because I dismounted the Xbee shield and plugged the cable into the computer, opened up the serial terminal from the Arduino screen and I got the desired numbers when I clicked the button. I am not sure whether it is the Xbee's being configured wrong or if the code for the second Arduino is incorrect.  Can anyone give me any input?
Title: Re: Xbee problems
Post by: Eithman on May 23, 2012, 03:11:27 PM
I switched the blue jumpers on both Xbee shields and now I am getting some weird results. The led on the receiver Arduino does come on but I cannot control it. If I push the button on the sending Arduino a few times and play with the wires, eventually the led turns on. But some times it blinks and some times it stays on continually. I have determined that the led is turning on because of the sending Arduino ( because it will not happen unless it is turned on ) But it is a guessing game on how to control the led. Any ideas?
Title: Re: Xbee problems
Post by: Eithman on May 23, 2012, 03:18:01 PM
I timed it and it turns out that if I hit the button, it takes 32ish seconds for the light to turn on. Then if I hit the button again it takes 32ish seconds to turn off. It does this consistently. Maybe the Xbees are only transmitting every 32 seconds? Is that possible and if so, how can I change it?
Title: Re: Xbee problems
Post by: Wesley888 on May 23, 2012, 03:42:54 PM
Hmm...  I seriously doubt Xbee would be that slow.  A pair of Xbee basically is just a wireless serial connections between two devices.

Did you check on the receiving side that your loop is stuck on the Serial.available() == 0?

If it's not, check the serial monitor and see what kind of message coming through from the Serial.read()