Show Posts
Pages: 1 2 [3]
31  Using Arduino / Networking, Protocols, and Devices / Loop stops running after several cycles - XBee S2 network on: March 01, 2012, 08:27:03 am
Hi,

I'm running a ZBee network of 1 AT coordinator and up to 4 AT routers.

All radios are Series 2 and attached to RBBB arduino boards.
The sketches on the arduinos is adapted from the examples by Rob Faludi, linked to from this page: http://www.faludi.com/itp_coursework/meshnetworking/XBee/XBee_example.html

Can anyone tell me why the coordinator code seems to run for about 6 cycles and then stop?

Thanks,
nikki
(newbie to programming, very newbie to XBee, so please bear in mind I will probably have to google your answers to be able to understand them!)

Here's the Coordinator code:

Code:
#include <SoftwareSerial.h>
SoftwareSerial XbeeSerial(8, 7);

// a digital input is on port 12
int switchPin = 12;

// a status light is on port 13
int ledPin = 13;

// a byte to send out data:
char thisByte = 0;


void setup () {
  // set pins to input and output appropriately
  pinMode(ledPin, OUTPUT);
  pinMode(switchPin, INPUT);

  // start up the serial connection with 9600-8-n-1-true (non-inverted):
  Serial.begin(9600);
XbeeSerial.begin(9600);
  // blink the status LED
  blinkLED(ledPin, 3);

  // for some reason it seems to help to send an arbitrary character first
  //then pause for the guard time before requesting command mode
  XbeeSerial.print("X");
  Serial.print("X");
  delay(1100);
  // put the XBee in command mode
  XbeeSerial.print("+++");
  Serial.println("+++ setting local radio");
   delay(1100);
  // wait for a response from the XBee for 2000 ms, or start
  // over with setup if no valid response comes

  
  if (returnedOK() == 'T') {
    // if an OK was received then continue
                 Serial.println("command mode acheived!");
  }
  else {
                 Serial.println("failed to connect");
    setup(); // otherwise go back and try setup again

  }
  
XbeeSerial.print("ATID2001,");
Serial.print("ATID2001,");
XbeeSerial.print("DH00000000,DL0000FFFF,");
Serial.print("DH00000000,DL0000FFFF,");
  // exit command mode (note that we use Serial.printLN here
//to issue a linefeed that completes the command sequence)
  XbeeSerial.println("CN");
  Serial.println("CN");
  
   if (returnedOK() == 'T') {
    // if an OK was received then continue
     Serial.println("T");
  }
  else {
    setup(); // otherwise go back and try setup again
         Serial.println("failed to ok");
  }

}


void loop () {
  // read the switch:
  int startTime = millis();
while (millis() - startTime < 2000) {
    thisByte = 1;
    XbeeSerial.print(thisByte, DEC);
    Serial.println(thisByte, DEC);
}
while ((millis() - startTime) >= 2000 && (millis() - startTime) < 4000) {
    thisByte = 0;
    XbeeSerial.print(thisByte, DEC);
    Serial.println(thisByte, DEC);
}

} //end loop

void blinkLED(int targetPin, int numBlinks) {
  // this function blinks the status LED light as many times as requested
  for (int i=0; i<numBlinks; i++) {
    digitalWrite(targetPin, HIGH);
    Serial.println("LED HIGH");  // sets the LED on
    delay(250);                     // waits for a second
    digitalWrite(targetPin, LOW);
     Serial.println("LED LOW");  // sets the LED off
    delay(250);
  }
  

}


char returnedOK () {
  // this function checks the response on the serial port to see if it was an "OK" or not
  char incomingChar[3];
  char okString[] = "OK";
  char result = 'n';
  int startTime = millis();
  while (millis() - startTime < 2000 && result == 'n') {  // use a timeout of 10 seconds
    if (XbeeSerial.available() > 1) {
      // read three incoming bytes which should be "O", "K", and a linefeed:
                   Serial.println("serial available");
      
      for (int i=0; i<3; i++) {
        incomingChar[i] = XbeeSerial.read();
      }
      if ( strstr(incomingChar, okString) != NULL ) { // check to see if the respose is "OK"
 //     if (incomingChar[0] == 'O' && incomingChar[1] == 'K') { // check to see if the first two characters are "OK"
        result = 'T'; // return T if "OK" was the response
            Serial.println("OK");
      }  
      else {
        result = 'F'; // otherwise return F
                    Serial.println("NOT OK");
      }
    }
  }
  return result;
}




And the Router code:
Code:
#include <SoftwareSerial.h>

SoftwareSerial XbeeSerial(8, 7);

// an output light is on port 11
int outputPin = 13;

// a status light is on port 13
int ledPin = 11;

// a byte to receive data:
char inByte = 0;


void setup () {
  // set pins to input and output appropriately
  pinMode(ledPin, OUTPUT);
  pinMode(outputPin, OUTPUT);

  // start up the serial connection with 9600-8-n-1-true (non-inverted):
  Serial.begin(9600);
XbeeSerial.begin(9600);
  // blink the status LED
  blinkLED(ledPin, 3);

  // for some reason it seems to help to send an arbitrary character first
  //then pause for the guard time before requesting command mode
  XbeeSerial.print("X");
   Serial.print("X");
  delay(1100);
  // put the XBee in command mode
  XbeeSerial.print("+++");
  Serial.print("+++");
  delay(1100);
  // wait for a response from the XBee for 2000 ms, or start
  // over with setup if no valid response comes


  if (returnedOK() == 'T') {
    // if an OK was received then continue
  }
  else {
    setup(); // otherwise go back and try setup again
  }



  // set the PAN (personal area network) ID number
  // this example uses 0x3330, but you'll want to choose your own
  // unique hexadecimal number between 0x0 and 0xFFFE
  // (note the comma at the end of the command which indicates that another command will follow)
  XbeeSerial.print("ATID2001,");
  Serial.println("ATID2001, ");
//  XbeeSerial.print("ATNIBLUE");
//    Serial.println("ATNIBLUE");
//      XbeeSerial.print("ATDNCOORD");
//  Serial.println("ATDNCOORD");
    
  // exit command mode (note that we use Serial.printLN here to issue a linefeed that completes the command sequence)
 XbeeSerial.println("CN");
  Serial.println("CN");

  // wait for a response from the XBee for 2000 ms, or start
  // over with setup if no valid response comes

  if (returnedOK() == 'T') {
    // if an OK was received then continue
  }
  else {
    setup(); // otherwise go back and try setup again
  }

}


void loop () {
// get any incoming data:
     if (XbeeSerial.available() > 1) {
      // read a byte
        inByte = XbeeSerial.read();

   // light the LED if a 1 has been received
     }
   if (inByte == '1') {
   digitalWrite(outputPin, HIGH);
     Serial.println("LED HIGH");
   }
   // douse the LED if anything else was received
   else if (inByte == '0'){
   digitalWrite(outputPin, LOW);
        Serial.println("LED LOW");
   }
}

void blinkLED(int targetPin, int numBlinks) {
  // this function blinks the status LED light as many times as requested
  for (int i=0; i<numBlinks; i++) {
    digitalWrite(targetPin, HIGH);   // sets the LED on
    delay(250);                     // waits for a second
    digitalWrite(targetPin, LOW);    // sets the LED off
    delay(250);
         Serial.print("BLINK");
         Serial.println(i);
  }
}


char returnedOK () {
  // this function checks the response on the serial port to see if it was an "OK" or not
  char incomingChar[3];
  char okString[] = "OK";
  char result = 'n';
  int startTime = millis();
  while (millis() - startTime < 2000 && result == 'n') {  // use a timeout of 10 seconds
    if (XbeeSerial.available() > 1) {
      // read three incoming bytes which should be "O", "K", and a linefeed:
      for (int i=0; i<3; i++) {
        incomingChar[i] = XbeeSerial.read();
      }
      if ( strstr(incomingChar, okString) != NULL ) { // check to see if the respose is "OK"
//      if (incomingChar[0] == 'O' && incomingChar[1] == 'K') { // check to see if the first two characters are "OK"
        result = 'T'; // return T if "OK" was the response
                    Serial.println("OK");
      }  
      else {
        result = 'F'; // otherwise return F
      }
    }
  }
  return result;
}



32  Using Arduino / Networking, Protocols, and Devices / Re: Using master_reader and slave_sender for multiple bytes on: April 27, 2011, 07:10:49 am
Ha! Good catch!
That'll teach me to try and sneakily do these things while I'm at work...
33  Using Arduino / Networking, Protocols, and Devices / Re: Using master_reader and slave_sender for multiple bytes on: April 27, 2011, 03:36:40 am
Sorry for the belated reply, but thanks Nick - it works with that approach.

Next step is to add a second slave arduino into the mix...

Would I be right in thinking I just repeat the section of code on the master, but change the address I'm making it to?
Something like:

Code:
Wire.requestFrom(2, 2);    // request bytes from slave device #2

if (Wire.available()) {
b0 = Wire.receive(); // receive a byte
Serial.print("b0:" ); Serial.print(b0, BIN); Serial.print("\t"); // print b0
  }

if (Wire.available()) {
b1 = Wire.receive(); // receive a byte
Serial.print("b1:" ); Serial.println(b1, BIN); // print b1
  }

Wire.requestFrom(2, 3);    // request bytes from slave device #3

if (Wire.available()) {
c0 = Wire.receive(); // receive a byte
Serial.print("c0:" ); Serial.print(c0, BIN); Serial.print("\t"); // print c0
  }

if (Wire.available()) {
c1 = Wire.receive(); // receive a byte
Serial.print("c1:" ); Serial.println(c1, BIN); // print c1
  }
34  Using Arduino / Networking, Protocols, and Devices / Using master_reader and slave_sender for multiple bytes on: April 24, 2011, 03:03:48 pm
Hi,

I'm hoping to eventually be able to request a long variable from a slave arduino via I2C. Starting from scratch knowledge-wise, so I've been trying to gradually build on the master-reader and slave-sender examples from the IDE.

I've managed to be able to request a single binary number (byte) from the slave arduino, but I'm running into problems when I try and introduce a second.

byte b0 = B11001110;
byte b1 = B11101101;

Running the code below gives an output of:

b0:11101101   b1:11111111
b0:11101101   b1:11111111
b0:11101101   b1:11111111
b0:11101101   b1:11111111
...

Which is the value of b1 being printed as the value of b0.
I've no idea where the 11111111 has come from. (Where does the 11111111 come from?)

If I instead request a single byte, comment out the second Wire.receive(); in the master code, and also comment out either Wire.send(b0); or Wire.send(b1); in the slave code, then the output gives the correct values.

Where am I going wrong when adding in the second byte? Can somebody please explain what's happening for me (and I'd also be grateful for help in putting in right!)?

Thanks.

Master (reader) code:

Code:
// Wire Master Reader
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Reads data from an I2C/TWI slave device
// Refer to the "Wire Slave Sender" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

byte b0 = 0;
byte b1 = 0;

void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop()
{

Wire.requestFrom(2, 2);    // request bytes from slave device #2

if (Wire.available()) {
b0 = Wire.receive(); // receive a byte
Serial.print("b0:" ); Serial.print(b0, BIN); Serial.print("\t"); // print b0
  }

if (Wire.available()) {
b1 = Wire.receive(); // receive a byte
Serial.print("b1:" ); Serial.println(b1, BIN); // print b1
  }

delay(500);
}

Slave (sender) code:

Code:
// Wire Slave Sender
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Sends data as an I2C/TWI slave device
// Refer to the "Wire Master Reader" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

// 0001 1111 0100 0010 1110 1101 1100 1110 example long I'm eventually hoping to send

byte b0 = B11001110;
byte b1 = B11101101;


void setup()
{
  Wire.begin(2);                // join i2c bus with address #2
  Wire.onRequest(requestEvent); // register event
}

void loop()
{
  delay(100);
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
 
  Wire.send(b0);
  Wire.send(b1);
}
35  Forum 2005-2010 (read only) / Troubleshooting / Re: Problem installing USB drivers on vista on: May 19, 2009, 05:38:07 pm
Solved. (not quite sure how, or would detail it here, but involved deleting things in device manager, letting vista install via updates, going back in through device manager and updating - that's what let me point to the driver folder - removing bluetooth devices to free up lower number ports....)
36  Forum 2005-2010 (read only) / Troubleshooting / Problem installing USB drivers on vista on: May 19, 2009, 03:06:38 pm
Hi,

I've just got an arduino and have got into a mess with the USB drivers.

The first time I tried it I pressed the wrong button when the "new hardware found" box came up and it went off searching for updates.

I was typing as it finished and bad timing with me hitting the return key meant I didn't see whatever message it had flashed up!

I went into device managers, saw something with a yellow exclamation mark and uninstalling that meant I could start again with plugging in the arduino and getting the "new hardware found" window again. I've now tried all of the options and can't see a way to direct the wizard at the driver files on my machine. What should I have clicked?

I'm now also no longer getting the yellow exclamation mark under the device drivers so I no longer get a response from windows when I plug in the arduino.

Where should I go from here and/or how do I get back to the beginning again?

I'd be grateful for any help.

nikki
Pages: 1 2 [3]