Go Down

Topic: xBee: duplicate response (Read 2 times) previous topic - next topic

maraesa1000

I have a project with two xBees on two boards. On one board there is a button, which will turn a relay connected to a light on the other  board for 5 seconds, then off (with a 1 second delay). It is doing this by sending a letter 'd' over the serial port, and then the other xBee executes the relay code in response to it.

Problem: What is going wrong is that the relay is turning on/off twice, rather than just once.

My actions so far: I have added debounce code for the button, and have looked at the serial monitor of the xBee that is sending the 'd' and it is only sending it once. However, when i look at the serial monitor of the relay-connected xBee, it responds twice for reasons beyond my comprehension. I have also tried a switch statement with a break with no joy. I have also tried utilising external variables to trigger the relay response, but they were even more problematic.

Below are snippets of my code.

button xBee
Code: [Select]

if (buttonState == LOW) {
    delay(100);
    Serial.print('D');
    delay(5000); // prevents overwhelming the serial port & increased the delay
digitalWrite(localINDICATOR, HIGH);
}



relay xBee

Code: [Select]

if (val == 'D'){
       digitalWrite(relay, HIGH);
  delay(5000);
    digitalWrite(relay, LOW);
    delay(1000);
 
    }


I'd really appreciate any help with this!

Note: Removing the relay entirely isn't an option for me as i need it to switch a larger load than the LED that is is currently being debugged with.

Cheers, Maraesa

Jack Christensen

It'd be better if we could see all the code.
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

maraesa1000

Ok, the full code is below

Button xBee

Code: [Select]

#define VERSION "1.00a0"
int BELL= 11;
int localINDICATOR= 7;
int BUTTON = 2;

void setup() {
  pinMode(BUTTON, INPUT);
    pinMode(BELL, OUTPUT);
pinMode(localINDICATOR, OUTPUT);
  Serial.begin(9600);
}

void loop() {
//      digitalWrite(BELL, HIGH);
char s2 = Serial.read();   
digitalWrite(localINDICATOR, LOW);
digitalWrite(BELL, LOW);
  // send a capital D over the serial port if the button is pressed

  if (digitalRead(BUTTON) == LOW) {
    Serial.print('D');
    delay(1000); // prevents overwhelming the serial port i increased the delay
digitalWrite(localINDICATOR, HIGH);

}
   //        digitalWrite(BELL, HIGH);

  if (Serial.available() > 0) {
//   Serial.print(s2);
    if (Serial.read() == 'K'){
      //ring the bell briefly
         //  Serial.print('k');
     digitalWrite(localINDICATOR, HIGH);
     delay(10); 
    }
    if (Serial.read() == 'H'){
      //ring the bell briefly
     digitalWrite(BELL, HIGH);
     delay(10);
//      Serial.print('k'); response code - develop later
    }
  }
}


relay xBee

Code: [Select]
//this is the device with the relay on it

int globalset = 1;
int indicatorVar = 0;
int timerVar =0; //i was using these three as external variables

int buttonState = 0;         // variable for reading the pushbutton status
#define VERSION "1.01"
int REMOTEINDICATOR = 8;
int BELL = 5;
char val;
int lightsetting = 400;
//int BUTTON = 6;
int relay = 2;

void setup() {
  pinMode(BELL, OUTPUT);
    pinMode(REMOTEINDICATOR, OUTPUT);
  Serial.begin(9600);
  pinMode(relay, OUTPUT);     
}

void loop() {

//  digitalWrite(relay, LOW);   // set the relay OFF
  if (Serial.available() > 0) {

val=Serial.read();   
        if (val == 'h'){
        }
       
    if (val == 'D'){
  //    Serial.print('K');
  //    timerVar = 1;     
      digitalWrite(relay, HIGH);
  delay(5000);
    digitalWrite(relay, LOW);
    delay(1000); 
    }
}
};


Cheers, Maraesa

PaulS

On the sender:
Code: [Select]
char s2 = Serial.read();   
Is executed on every pass through loop, even if there is nothing to read. Why? s2 is never used.

You really need to discover the wonders of Tools + Auto Format. That code wandering all over the page is a nightmare to read.

The sender sends a nothing but a 'D'. The receiver expects nothing but a 'h'. It is unlikely that a match will ever be detected.

Jack Christensen


The receiver expects nothing but a 'h'. It is unlikely that a match will ever be detected.


The last If in the receiver code checks for 'D'. The test for 'h' precedes that, but does nothing when an 'h' is detected, so macht nichts.
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Go Up