Pages: [1]   Go Down
Author Topic: Fio constantly resets  (Read 377 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have 2 Fio boards with which I'm trying to cross communicate (for test puposes) a switch being pressed.  The intent is to press a momentary switch and see a local LED light up AND an LED on the other Fio.  The same should happen on the other Fio.  The software is identical on both.

It seems to work but quickly starts a back and forth reset of the 2 Fio boards.

I've seen notes about halting the resets on a basic arduino by putting a 120 ohm resister between reset and VCC; or a capacitor.  But I have not seen how it's done on the Fio.

Anyone have any tips about how to stop the Fio from resetting when I send a signal from Fio serial to Fio serial?

Thanks in advance,
Chris.


XBee 1
PAN ID: 1111
DH: 0
DL: 12
MY: 11 (radio 17)

XBee 2
PAN ID: 1111
DH: 0
DL: 11
MY: 12 (radio 18)

The code I'm testing.

Code:
// twowaytest.ino
// testing 2-way communications with the Fio

int buttonPin = 2; // button with 10k to gnd
int localHbLed = 13; // led to gnd
int remoteHbLed = 12; // led to gnd

int buttonState = 0;
int lastState = 0;
const int terminatingChar = 13; //Terminate lines with CR

void setup(){

  pinMode(localHbLed,OUTPUT);   
  pinMode(remoteHbLed,OUTPUT);
  pinMode(buttonPin, INPUT); 

  Serial.begin(57600);
  Serial.print(255);
  delay(2000);
 
  analogReference(EXTERNAL);
  blink(3); // show we're on...
}


void loop(){

  buttonState = digitalRead(buttonPin);

  if (lastState != buttonState) {
    if (buttonState == HIGH) {     
      // turn LED on:   
      digitalWrite(localHbLed, HIGH);
      sendDataToOther("pulseUp");
      lastState = HIGH;
    }
    else {
      // turn LED off:
      digitalWrite(localHbLed, LOW);
      sendDataToOther("pulseDn"); 
      lastState = LOW;
    }
  }

  getDataFromOther();
  delay(20);                          //  take a break
}

void sendDataToOther(String pulseVal){
  // send info to the other xbee
  if (pulseVal == "pulseUp"){
    Serial.print("pulseUp\r");
  }
  else {
    Serial.print("pulseDn\r");
  }
}

void getDataFromOther() {

  char* theOtherPulse = serialReader();
  // Serial.print("Other is: ");
  // Serial.println(theOtherPulse);

  // if (theOtherPulse == theOtherTest[0]){
  if (strstr(theOtherPulse, "PulseUp") != 0) {
    digitalWrite(remoteHbLed,HIGH);
    // Serial.print("recd... HIGH ");
    // Serial.println(theOtherPulse);
  }
  else {
    digitalWrite(remoteHbLed,LOW);
    // Serial.print("recd... LOW ");
    // Serial.println(theOtherPulse);
  }
}

// read serial port
char* serialReader(){
  int makeSerialStringPosition;
  int inByte;
  char serialReadString[50] = "";
 

  inByte = Serial.read();
  makeSerialStringPosition=0;

  if (inByte > 0 && inByte != terminatingChar) { //If we see data (inByte > 0) and that data isn't a carriage return
    delay(100); //Allow serial data time to collect (I think. All I know is it doesn't work without this.)

    while (inByte != terminatingChar && Serial.available() > 0){ // As long as EOL not found and there's more to read, keep reading
      serialReadString[makeSerialStringPosition] = inByte; // Save the data in a character array
      makeSerialStringPosition++; //Increment position in array
      //if (inByte > 0) Serial.println(inByte); // Debug line that prints the charcodes one per line for everything recieved over serial
      inByte = Serial.read(); // Read next byte
    }

    if (inByte == terminatingChar) //If we terminated properly
    {
      serialReadString[makeSerialStringPosition] = 0; //Null terminate the serialReadString (Overwrites last position char (terminating char) with 0
      // Serial.print("from method: ");
      // Serial.println(serialReadString);
      // if (strcmp(serialReadString, "LEDOn") == 0) digitalWrite(13, HIGH);
      // if (strcmp(serialReadString, "LEDOff") == 0) digitalWrite(13, LOW);

      return serialReadString;
    }
  }
  return "x"; // return this when nothing is recieved...
}

int stringToInt(String thisString) {
  int i, value, length;
  length = thisString.length();
  char blah[(length+1)];
  for(i=0; i<length; i++) {
    blah[i] = thisString.charAt(i);
  }
  blah[i]=0;
  value = atoi(blah);
  return value;
}

// blink n times
void blink(int howManyTimes){
  digitalWrite(localHbLed, LOW);
  digitalWrite(remoteHbLed, LOW);
  delay(200);
  for (int i=0; i< howManyTimes; i++){
    digitalWrite(localHbLed, HIGH);
    digitalWrite(remoteHbLed, LOW);
    delay(200);
    digitalWrite(localHbLed, LOW);
    digitalWrite(remoteHbLed, HIGH);
    delay(200);
  }
  digitalWrite(remoteHbLed, LOW);
}

Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 224
Posts: 6619
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

There are at least 2 problems in function serialReader:

1. You are returning the address of local variable serialReadString. That variable is created on the stack and ceases to exist when the function returns. Declaring it static will fix the problem.

2. You have a classic buffer overflow bug. if the terminating character is not received within 50 characters, the buffer will overflow. You should not increment variable makeSerialStringPosition beyond 49.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I seem to have it working.  Thanks, dc42.

Code:
// twowaytest.ino
// testing 2-way communications with the Fio

// XBee settings (use X-TCU)
// XBee 1:
// PAN ID: 1111
// DH: 0
// DL: 12
// MY: 11 (radio 17)
//
// XBee 2:
// PAN ID: 1111
// DH: 0
// DL: 11
// MY: 12 (radio 18)

int buttonPin = 2; // button with 10k to gnd
int localHbLed = 13; // LED to gnd
int remoteHbLed = 12; // LED to gnd

int index = 0;
int buttonState = 0;
int lastState = 0;

const int terminatingChar = 13; // Terminate lines with CR


// ---------------------------------------------
void setup(){

  pinMode(localHbLed,OUTPUT);   
  pinMode(remoteHbLed,OUTPUT);
  pinMode(buttonPin, INPUT); 

  Serial.begin(57600);
  Serial.print(255); 
  analogReference(EXTERNAL); // set up for 3.3v usage

  delay(2000);
  blink(3); // show we're on...
}

// ---------------------------------------------
void loop(){

  buttonState = digitalRead(buttonPin);

  if (lastState != buttonState) {
    if (buttonState == HIGH) {     
      // turn LED on:   
      digitalWrite(localHbLed, HIGH);
      sendDataToOther("pulseUp");
      lastState = HIGH;
    }

    if (buttonState == LOW) {
      // turn LED off:
      digitalWrite(localHbLed, LOW);
      sendDataToOther("pulseDn");
      lastState = LOW;
    }
  }

  getDataFromOther();
  delay(50);    //  take a break
}


// ---------------------------------------------
void sendDataToOther(String pulseVal){
  // send info to the other xbee
  if (pulseVal == "pulseUp"){
    Serial.print("pulseUp\r");
  }
  else {
    Serial.print("pulseDn\r");
  }
}


// ---------------------------------------------
void getDataFromOther() {

  char* theOtherPulse = serialReader();

  if (strcmp(theOtherPulse, "pulseUp") == 0) {
    digitalWrite(remoteHbLed,HIGH);
    Serial.println("recd... HIGH ");
  }

  if (strcmp(theOtherPulse, "pulseDn") == 0) {
    digitalWrite(remoteHbLed,LOW);
    Serial.println("recd... LOW ");
  }
}


// ---------------------------------------------
// read serial port
char* serialReader(){
  static char serialReadString[50] = "";
  index=0;
  int inByte = Serial.read();

  if (inByte > 0 && inByte != terminatingChar) {
    delay(50); //Allow serial data time to collect

    while (Serial.available() > 0 && inByte != terminatingChar && index <= 49) {
      serialReadString[index] = inByte; // Save the data in a character array
      index++; // increment position in array
      inByte = Serial.read(); // Read next byte
    }

    if (index >= 49){
      return "b"; // return this when the buffer is full...
    }

    // If we terminated properly
    if (inByte == terminatingChar) {
      serialReadString[index] = 0; //Null terminate the serialReadString (Overwrites last position char (terminating char) with 0
      return serialReadString;
    }
  }
  return "n"; // return this when nothing is received...
}


// ---------------------------------------------
// convert string to int
int stringToInt(String thisString) {
  int i, value, length;
  length = thisString.length();
  char blah[(length+1)];
  for(i=0; i<length; i++) {
    blah[i] = thisString.charAt(i);
  }
  blah[i]=0;
  value = atoi(blah);
  return value;
}

// ---------------------------------------------
// blink n times
void blink(int howManyTimes){
  digitalWrite(localHbLed, LOW);
  digitalWrite(remoteHbLed, LOW);
  delay(200);
  for (int i=0; i< howManyTimes; i++){
    digitalWrite(localHbLed, HIGH);
    digitalWrite(remoteHbLed, LOW);
    delay(200);
    digitalWrite(localHbLed, LOW);
    digitalWrite(remoteHbLed, HIGH);
    delay(200);
  }
  digitalWrite(remoteHbLed, LOW);
}

Logged

Pages: [1]   Go Up
Jump to: