nrf24l01 bounce button problem

Hi there I would like to control few leds with pushbuttons and I am planning to use multiple nrf modules. I am using bounce2 library and wrote the transmitter and receiver codes. Codes are compiling but does not work in real life. I checked the connections and everthing seems ok. I also checked the problem by uploading simple nrf led codes from elec-cafe.com and its working. However when I upload my code, it doesnt work. Later I am going to add a reset button and I want the leds lit up until the reset is pressed up inside an infinite loop.

TRANSMITTER SIDE CODE

#include <Bounce2.h>
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int msg[1];
RF24 radio(9, 10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int SW1 = 7;
int SW2 = 6;

Bounce bouncer_one = Bounce();
Bounce bouncer_two = Bounce();


void setup(void) {

  radio.begin();
  radio.openWritingPipe(pipe);

  pinMode(SW1, INPUT);
  pinMode(SW2, INPUT);
  radio.setChannel(108);
  bouncer_one.attach(SW1);
  bouncer_two.attach(SW2);
}

void loop(void) {

  bouncer_one.update();
  bouncer_two.update();

  if (bouncer_one.rose())
  {
    msg[0] = 111;
    radio.write(msg, 1);
  }

  if (bouncer_two.rose())
  {
    msg[0] = 113;
    radio.write(msg, 1);
  }




}

RECEIVER SIDE CODE

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int msg[1];
RF24 radio(9, 10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int LED1 = 3;
int LED2 = 5;


void setup(void) {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(1, pipe);
  radio.startListening();
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  radio.setChannel(108);

}

void loop(void) {
  if (radio.available()) {
    bool done = false;
    while (!done) {
      done = radio.read(msg, 1);

      Serial.println(msg[0]);
      if (msg[0] == 111)
      {
        delay(10);
        digitalWrite(LED1, HIGH);
      }

      if (msg[0] == 113)
      {
        delay(10);
        digitalWrite(LED2, HIGH);
      }

      delay(10);
    }
  }
}
if (bouncer_one.rose())
{
msg[0] = 111;
radio.write(msg, 1);}

if (bouncer_two.rose())
{
msg[0] = 113;
radio.write(msg, 1);

Why is the check for bouncer_two (what a dumb name) INSIDE the body of the bouncer_one check?

The code does something. You expect it to do something. That the code "doesn't work" simply means that your expectations are wrong.

PaulS:
Why is the check for bouncer_two (what a dumb name) INSIDE the body of the bouncer_one check?

Paul - it’s actually not, but the } is just as poorly indented as the variable names are chosen…

However when I upload my code, it doesnt work.

what are the symptoms?
what do you see in the console?

(note that int msg[1]; is 2 bytes and radio.write(msg, 1); is only sending 1 byte - which should be still OK given the values you’ve chosen are < 255)

Paul - it's actually not, but the } is just as poorly indented as the variable names are chosen...

Missed that. Iwonderwhypeoplethatthinkthecurlybracegoesonthesamelineasthefunctionorstatementcantusespacestomakethecodereadable.

PaulS: Missed that. Iwonderwhypeoplethatthinkthecurlybracegoesonthesamelineasthefunctionorstatementcantusespacestomakethecodereadable.

:)

yep - or hit just cmd-T to properly indent the code if they are too lazy to hit return... beyond me...

as a newbie my purpose was to when the first button is pushed (HIGH) I want the first led to be lit. it goes for the second led also

what are the symptoms? what do you see in the console?

how did you wire your buttons?

hacknmake: as a newbie my purpose was to when the first button is pushed (HIGH) I want the first led to be lit. it goes for the second led also

Pretend that we can't see what you see. Because we can't. YOU must be our eyes and ears. So, open them!

What does the program ACTUALLY do? Have you confirmed that the radios communicate?

the problem is when i pressed the buttons led wont go high(lit) and i uploaded the code from the link http://www.elec-cafe.com/nrf24l01-basic-control-led-arduino/ and it worked well but when i uploaded my code to lock the led output with bounce2 library it didnt work. I tried to upload photos but the site gave some error. radios communicate well.

So, your problem has nothing to do with the radios.

Use Serial.print() statements, to make sure that the switches are working with the Bounce2 library.

And, post a schematic showing how the switches are wired.

I cannot imagine how button bounces have anything to do with an nRF24. Bounces should all be taken care of long before the data gets anywhere near the nRF24.

This Simple nRF24L01+ Tutorial may be of interest.

Get the wireless communication working first (because it can be tricky to debug) and then add on the other code.

...R

connections are the same as the link I gave before. I only added second button and led. +5v pin goes a pin of button. other pin of the button goes to a 1k resistor and the digital pin 6-7(second button) and the other leg of the resistor goes to gnd. No problem with connections. the problem is the code compiles but doesnt do the job.

the code compiles but doesnt do the job.

It does SOMETHING. You need to add Serial.print() statements to learn what it does, and where it stops doing what you expect.

from the serial monitor if I write tx side if(digitalRead(SW1)==HIGH) it writes 111 and lights up the led. with the bouncer monitor reads zero. Normally bouncer works with the code below.

#include <Bounce2.h>

byte buttonPinOne = 3;
byte ledPin = 11;
byte resetPin = 1;
byte buttonPinTwo = 5;
byte redPin = 13;

Bounce bouncer_one = Bounce();   //bouncer for on button LED
Bounce bouncer_two = Bounce();   //bouncer for reset button LED
Bounce bouncer_three = Bounce();
void setup() {

  pinMode(buttonPinOne, INPUT_PULLUP);
  pinMode(buttonPinTwo, INPUT_PULLUP);
  pinMode(resetPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  pinMode(redPin, OUTPUT);


  bouncer_one.attach(buttonPinOne);   //pin definitons for bouncers
  bouncer_three.attach(buttonPinTwo);
  bouncer_two.attach(resetPin);


}

void loop() {
  bouncer_one.update();
  bouncer_two.update();
  bouncer_three.update();

  if (bouncer_one.rose())         //turns LED on after first rising edge and stays on
  {
    digitalWrite(ledPin, HIGH);
  }

  if (bouncer_three.rose())         //turns LED on after first rising edge and stays on
  {

    digitalWrite(redPin, HIGH);
  }




  if ((bouncer_two.rose()) /*&& (ledPin == HIGH)*/) // turns of the led when reset button pushed and led is on BUT DOESNT WORK I DONT KNOW WHY?

  {
    digitalWrite(ledPin, LOW);
    digitalWrite(redPin, LOW);
  }

}
byte resetPin = 1;
...
  pinMode(resetPin, INPUT_PULLUP);

if you have that in your code don't expect to be able to use Serial.print

I am getting close. receiver side cant see the message from bounce statement

transmitter side

#include <Bounce2.h>

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int msg[1];
RF24 radio(9, 10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int SW1 = 7;
int SW2 = 6;

Bounce switch_one = Bounce();
Bounce switch_two = Bounce();

void setup(void) {
  pinMode(SW1, INPUT);
  pinMode(SW2, INPUT);
  switch_one.attach(SW1);
  switch_two.attach(SW2);

  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(pipe);


}

void loop(void) {
  switch_two.update();
  switch_one.update();

  if (digitalRead(SW1) == HIGH) ////THIS WORKS BUT I have to press the button few seconds transmitter and receiver side sees the sending signal as 111.
  {
    msg[0] = 111;
    radio.write(msg, 1);
  }
  Serial.println(msg[0]);
  /* else if (digitalRead(SW1) == LOW)
    {
     msg[0] = 112;
     radio.write(msg, 1);
    }

  */
  if (switch_two.rose())  ////THIS DOESNT WORK transmitter side sees the sending signal BUT receiver side cant see.
  {
    msg[0] = 113;
    radio.write(msg, 1);
  }

  /* if (digitalRead(SW2) == LOW)
    {
     msg[0] = 114;
     radio.write(msg, 1);
    }

  */
}

receiver side

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int msg[1];
RF24 radio(9, 10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int LED1 = 3;
int LED2 = 5;

void setup(void) {
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);

  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(1, pipe);
  radio.startListening();

}

void loop(void) {
  if (radio.available()) {
    bool done = false;
    while (!done) {
      done = radio.read(msg, 1);
      Serial.println(msg[0]);
      if (msg[0] == 111) {
        delay(10);
        digitalWrite(LED1, HIGH);
      }
      /*else if (msg[0] == 112) {
        digitalWrite(LED1, LOW);
        }*/
      if (msg[0] == 113) {
        delay(10);
        digitalWrite(LED2, HIGH);
      }
      /*else {
        digitalWrite(LED2, LOW);
        }*/
      delay(10);
    }
  }
  else {
    Serial.println("empty");
  }
}

I am getting close. receiver side cant see the message from bounce statement

Re-read reply #7.

What you are posting is nowhere near enough information.

where are the prints on the sender side?

Suggest you change

int msg[1];

into

byte msg[1];

for coherence with what you do

I still dont get the problem here some photos tx on left rx on right

How difficult is it to add a Serial.print() statement in the body of the if statements? Nearly impossible, it seems as you refuse to do it.

I'm done trying to help, since you won't do the simple things that are asked.