Hi everyone , i got my NRF24L01 kit last week and i’m trying to turn 2 leds remotely using 2 switches , i was able to turn one led but the other didn’t work. i really tried a lot of things , i tried do changes to my codes , looked everyone didn’t find any good examples , i’m having problem to control multi things , is there anything i can add to my code to make it work ? i just want to find a method to control multi things so i’m able to add more stuff , thanks all and here is the code
Code for Transmitter
#include <SPI.h>
#include “nRF24L01.h”
#include “RF24.h”
int msg[2];
RF24 radio(8,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int SW1 = 2;
int SW2=3;
void setup(void){
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(pipe);}
void loop(void){
if (digitalRead(SW1) == HIGH){
msg[0] = 111;
radio.write(msg, 2);}
if (digitalRead(SW2)==HIGH) {
msg[1]=222;
radio.write(msg, 2);
} }
Code for Receiver
#include <SPI.h>
#include “nRF24L01.h”
#include “RF24.h”
#define CE_PIN 9
#define CSN_PIN 10
int msg[2];
const uint64_t pipe = 0xE8E8F0F0E1LL;
RF24 radio(CE_PIN, CSN_PIN);
int LED1 = 3;
int led=2;
void setup(){
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(1,pipe);
radio.startListening();
pinMode(LED1, OUTPUT);
pinMode(2,OUTPUT);
}
void loop(void){
if ( radio.available() )
{
// Read the data payload until we’ve received everything
axisxx:
Hi everyone , i got my NRF24L01 kit last week and i’m trying to turn 2 leds remotely using 2 switches , i was able to turn one led but the other didn’t work. …
And the winner is … which led/digital pin? On what arduino?
Do you realise that you have the function of the switches and leds swapped on the transmitter and receiver? SW1 on transmitter pin 2 will activate the led on receiver pin 3. Sw2 on pin 3 will activate the led on pin 2.
Also once the transmitter sets msg[0] to 111, or msg[1] to 222 they never change. You never clear those values so they will always be sent that way. The receiver never has to set it’s pins otherwise.
Well, you said “i was able to turn one led but the other didn’t work”. That is a kinda strange problem description. Were you able to turn it on, or off? Or how did the other not work. Never turns on? Or stays on but never goes off? Before I suggest any code I want to fully understand what it is supposed to do. And what is it doing that it should not be doing. Your explanation was not clear enough.
And if you don’t know how to set or clear values in variables then I suggest you start learning the basics of Arduino or C-programming first. Then using complicated radio modules is way above you head at this moment. Did you write this code yourself?
Are you sure this is a programming error? Or could this be an electrical error? Explain more about your wiring. Can you get the led to light up by applying a voltage to the particular Arduino pin with a wire (from Vcc)? “Turning a led” on a breadboard is one of the ways to make it not light up. Current can only go one way through it.
the other led didn’t turn on only the first one got on , i’m trying to control 2 leds using 2 switches when i press a switch a led must go on , i think it’s most likely a programming problem , i have checked the connections a lot and they are all good
and i have already passed the basic of arduino but i don’t understand what you mean with clearing the variables , so it would be nice if you talk to me in codes , also english is my second language , what’s the code for clearing variables ?thanks a lot
Well, clearing a variable means you give it a safe different value, that other code does not respond to. In that code you provided you assign the value 111 to msg[0] with the command msg[0] = 111; Similarly you assign 222 to msg[1]. This gets sent to the receiver which turns on a led if msg[0] is 111, and turns off if it isn’t. But the transmitter never checks if the switch is released and so never sets it to something else. To do that you make the if-statements in the transmitter code similar to the receiver code. So with an else clause that sets the element of msg for that switch to 0 for example. The example how to structure it is already there in the receiver code.
While that cannot be the cause of the other led not turning on, it does confuse you as you cannot repeat trying to turn it on. It always stays turned on. It would need both megas and NRF24L01 to be powercycled or resetted to become functional again.
I can’t see a reason in the code why the led on pin 2 of the receiver does not work. You are not consistent in using variables for pin-names and use hardcoded numbers instead. But the numbers that you used should work, it just is a bit confusing to read. So I would now question if the receiver gets a properly formatted message. I would want to know what is received exactly. To make sure of that I would report the contents of the received message back to the pc over the serial port. I only changed the loop function of the receiver, adding serial.print statements and reformating the indentation:
void loop(void){
if ( radio.available() )
{
// Read the data payload until we've received everything
bool done = false;
while (!done) {
done = radio.read( msg, sizeof(msg) );
Serial.print("Message received: ");
Serial.print(msg[0]);
Serial.print(",");
Serial.println(msg[1]);
if(msg[0]==111) {
digitalWrite(3,HIGH);
}
else {
digitalWrite(3,LOW);
}
if(msg[1]==222){
digitalWrite(2,HIGH) ;
}
else {
digitalWrite(2,LOW);
}
}
}
}
You still haven’t confirmed that you can light up the led by applying a voltage to it manually. So I am not convinced that it isn’t an electrical error. Does “checking the connections” mean where the wiring goes? Or that you can simulate the Arduino mega by manually perform the pin functions of it and it actually lights up that desired led.
If it takes this long to find and fix the issue I start questioning my assumptions and second-guess anything. Even the simplest things.
thank you so much for putting time to answer me and explaining it i really appreciate it , but i managed to fix the problem by changing the whole coding using i code i found for a joystick ,just did little changes and it worked