Pages: [1]   Go Down
Author Topic: xBee: duplicate response  (Read 1428 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
if (buttonState == LOW) {
    delay(100);
    Serial.print('D');
    delay(5000); // prevents overwhelming the serial port & increased the delay
digitalWrite(localINDICATOR, HIGH);
}


relay xBee

Code:
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
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 71
Posts: 3516
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It'd be better if we could see all the code.
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, the full code is below

Button xBee

Code:
#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:
//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
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 549
Posts: 46149
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

On the sender:
Code:
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.
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 71
Posts: 3516
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 71
Posts: 3516
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, the full code is below

Thanks. The debounce code is pretty basic. The button must be pressed, then released in less than one second. If you wait for the "localINDICATOR" to come on, and then release the button, then a bounce could be picked up, sending another 'D'.

On the sending side, change:

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

To the following, and release the button as soon as the "localIndicator" comes on.

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

Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks so much for your answers. They really helped!

I've made the changes that you suggested, Jack. I also stripped down the code of any unnecessary lines as Paul Suggested

This fixed my problem completely when I had an LED in place with the relay.  However, when I was using a new component with the relay, it went back to repeating itself.

I then tried using a 560ohm resistor, which stopped the repetition, but didn't quite provide enough current for the new component (enough to indicate that it was on, but not enough to make it work). I am going to pick up a couple of lower value resistors, swap them in and pick the one that stops the repetition but provides enough current for the new component. I note that the component wouldn't work with a higher value resistor. Also, the repetition I'm experiencing only happens with relays; putting something else in the circuit doesn't have the same result.

It seems odd to me that this would make any difference!

It would be interesting to know why for future reference. It seems bizarre to me that the resistance within the relay circuit would cause it to turn on and off twice and also register a false 'D'.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Update:

The system now works perfectly with the 560ohm resistor. Seems a bit temperamental though so I'll test a few times in the next 24hours before celebrating  smiley-grin
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 71
Posts: 3516
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It seems odd to me that this would make any difference!

It would be interesting to know why for future reference. It seems bizarre to me that the resistance within the relay circuit would cause it to turn on and off twice and also register a false 'D'.

I agree that is very odd indeed. Good idea trying the LED instead of the relay. I wonder if the relay isn't causing some sort of issue with the power supply, maybe even causing a reset. How is the relay driven, can you post a schematic. Glad to hear some progress was made, though.
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey,

This is a fritzing diagram and a schematic of my circuit:




Note: I have put a speaker instead of the hacked MP3 player in the circuit. What i've done is a crude override of the on/off switch using  a relay to turn it on/off remotely. I have only included the board with the relay on it.

The mp3 player is from amazon: http://www.amazon.co.uk/gp/product/B004UYTA08/ref=wms_ohs_product and powering it on and off with a relay. It has its own power supply. I would post the mp3 power details but they're unknown to me. I've tried a few different ways of doing this remote control hack, but it is now proving temperamental. Today i've gone to re-test the circuit and its not working properly. I do think it is interfering with the power source as you mentioned before. It wont stop once its started playing now for some reason.

Thanks,

Siobhan
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I read somewhere that things connected to the relay had to share a common ground with the arduino (and its been in the online tutorials/projects that ive been referring to). I just thought 'hang on a second....what if...' and I put the yellow wire on the opposite side of the relay (parallel to the orange wire) and now it works perfectly. As far as i can tell its not connected to ground. This projects slowly driving me crazy! Siobhan
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 71
Posts: 3516
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Now if I were Grumpy Mike, I'd refuse to try and interpret that schematic. Actually I gave it about three minutes and that's all I'm willing to do. Seriously, a proper schematic even sketched with pencil on paper and scanned in would be far better. The way that 1N4001 appears to be connected just makes no sense at all. Sorry about that, don't take it too personally! But try to find a better way.

Having said that, here are a couple things that strike me off the top. Driving the relay direct from an Arduino pin is probably not appropriate. Do you have specs on the relay? What are its rated coil voltage and current? The problem is that most relays need more drive than an Arduino can provide, so using a transistor at least to provide more current is usually needed. So a resistor is actually sort of the opposite of what is needed. Ditto with an MP3 player. And using a speaker in place of an MP3 player is way off in left field. If that's a dynamic speaker, it probably represents nearly a dead short to the pin. I hope your Arduino is OK.
« Last Edit: April 26, 2012, 11:44:49 am by Jack Christensen » Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Lol thanks for trying. It really is a mess! Luckily my problem is already solved. I think it was a case of not knowing how to use the relay. My relay is being powered by the arduino on 3.3v (as this is the voltage that my xBees work at). It came with a beginner kit from oomlaut.com and this is what the accompanying tutorial instructed. There was no schematic or power information provided so i was kind of guessing.

This is the basic circuit i was working with http://www.oomlout.com/oom.php/products/ardx/circ-11 i am using the same relay and transistor.

I've had to adapt it to work with my mp3 player. The way i've wired it up now makes no sense according to the diagram but it works reliably so i'm quite happy with it.

Cheers!
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 71
Posts: 3516
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

From the oomlaut page, I'd assume that it's a 5V relay. It may operate on 3.3V. Then again it may not, or it may sometimes, or mostly.
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Pages: [1]   Go Up
Jump to: