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.
// 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);
}