Wireless LED control

Hello Gentlemen's,

Sorry for bothering you, but I really need help with my project, I have to finish it soon.
So the thing is that I have four buttons and two LED that have to communicate thru Arduino UNO and NRF24L01.

So basically what I need to do is I press one button one LED turns on, I Press another button first LED goes off and Second one turns on, third button should be both LED blinking, and fourth button should work as reset.

Im struggling to write code for wireless control, if anyone could help with the code I would really appreciate it.

Bl seni tu tikrai tikiesi kad kažkas ims ir padarys visą darbą už tave?
Ne nu bet žmonių naglumas šiais laikasi...
Tu ką neįgalus ar ką? Youtube krūvos video apie tai.
Suprasčiau jei neveiktų kažkuri dalis, o čia – „dėstytojas subinę tarkuoja tad padarykit kas viską už mane, nes man per daug žemas darbas“ :stuck_out_tongue_closed_eyes:

Ieškau pagalbos. Nesitikiu, kad viską padarys, užves ant kelio ir pasidarysium, jeigu būčiau youtube ar google radęs ko man reikia, tikrai neprašyčiau čia. Ne studijų projektas čia. Gal tu gali pagelbėti su kodu, skoloj neliksiu.

It's not clear where the wireless part comes in: are the buttons on one Uno"A" and the LEDs on another Uno "B", with the instructions sent from A to B?

And if I'm right about that, and you say this....

Im struggling to write code for wireless control,

..... does that mean you have the buttons succesfully controlling the LEDs when the buttons and LEDs are on the same Uno, ie you have the button/led logic coded?

Marius93:
So basically what I need to do is I press one button one LED turns on, I Press another button first LED goes off and Second one turns on, third button should be both LED blinking, and fourth button should work as reset.

You need to clarify in your own mind (or maybe it is clear) but also to potential helpers, if these buttons are sequential or can they be pressed in any order?

For example from the start position, can you press button2 to turn led2 on even though led1 wasn't turned on first?

Can you press button4 right after button1 to reset?

Etc.....

Needs a state diagram.

Thank you so much for your reply.

So basically i need to control green and red LED

Button #1 - pressed once keeps red LED on
Button #2 - pressed once switches off red LED and turns on Green
Button #3- pressed once makes both LED blink

This is transmitter code that I'm using

#include <SPI.h>
#include "RF24.h"
byte data[1];
const uint64_t pipe = 0xF0F0F0F0A1LL;
RF24 radio(9,10);

void setup(){
pinMode(3, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
radio.begin();
radio.openWritingPipe(pipe);
}

void loop(){
if(digitalRead(3)==HIGH || digitalRead(5)==HIGH || digitalRead(7)==HIGH){
data[0]=0;
}
if(digitalRead(3)==LOW){

data[0]=1;
}
if(digitalRead(5)==LOW){

data[0]=2;
}
if(digitalRead(7)==LOW){
data[0]=3;
}
radio.write(data, 1);
}

This is receiver code

#include <SPI.h>
#include "RF24.h"
byte data[1];
boolean var;
const uint64_t pipe = 0xF0F0F0F0A1LL;
RF24 radio(9,10);

void setup(){
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(7, OUTPUT);
radio.begin();
radio.openReadingPipe(1,pipe);
radio.startListening();
}

void loop(){
if(radio.available()){
var = false;
while(!var){
var = radio.read(data, 1);
if(data[0] == 0){
digitalWrite(3, LOW);
digitalWrite(5, LOW);
digitalWrite(7, LOW);
}
if(data[0] == 1){
digitalWrite(3, HIGH);
}
if(data[0] == 2){
digitalWrite(5, HIGH);
}
if(data[0] == 3){
digitalWrite(7, HIGH);
}
}
}
}

So I want to change code, but little lost, so any help would be much appreciated.

Button #1 - pressed once keeps red LED on
Button #2 - pressed once switches off red LED and turns on Green
Button #3- pressed once makes both LED blink

I would start by looking at the state change detect example and use that to read your 4 buttons. (You said earlier 4 was a reset....).

Use that to set a state variable called say whichButtonWasPressed: if button 1 is newly pressed, set the variable to 1, to 2 for button 2 etc.

Do that in a brand new sketch, forget the radio for a while, is my advice.

Then in the same new sketch on the same Arduino, use that variable as the key into a switch...case where you will do what you want with the leds.

Only when that's working, take the switch...case stuff to the second machine, and send only the state variable from one to the other.

That's the way I would approach this, anyway. YMMV.

I have meantime written a sketch Part 1 which sets the state variable depending on which button was newly pressed. I won't post it unless you ask, since you may prefer to grapple with that a bit on your own :wink:

Parts 2 and 3 are available if you want them :wink:

Part 2 uses the variable to access the case of a switch...case and just print a message.
Part 3 does the actual led stuff, including a blink without delay for case 3.

(All still in the same Uno of course, no RF yet.... I need to dig out my nRFs and remind myself how they work.)

If you could share your codes with me, I would really appreciate it. I would love to do all myself, but I have event coming, where I will have to use it,time is not my friend in this case, especially when I have to do all the hardware as well.

These LED's will be used as a start system for rowing competition. There will be 8 light stations and one control unit.

Thank you in advance.

OK I will post them here and in the next 3 posts.

Attached is part 1, which only uses the 4 buttons to set the state variable.

You should read and understand state change detect and switch...case before you proceed.

Probably not correct to say "part" 1, since part 2 in the next post will be an enhancement to and including part 1, so more of a version 2. If I were you, I would work through each though, to make sure you understand each concept before moving to the next one.

I will say right away that these can be made a lot better by the use of arrays to keep track of buttons, and better use of functions. So before anyone out there shouts at me, the OP is obviously new to this and although my code is a bit verbose, it's simple and easy to follow I reckon. But ofc open source being open, anyone's welcome to make suggestions and changes.

wireless_leds_part1_631531.ino (5.21 KB)

Here is part 2 of 4, which builds on the previous and enters the switch...case based on the on the state variable from part 1.

wireless_leds_part2_631531.ino (6.44 KB)

This is part 3 of 4 which does the actual control of the leds, including the blinking for case 3. You need to understand blink without delay first.

But this is still all in the same Uno...

wireless_leds_part3_631531.ino (7.09 KB)

Reserved for part 4 of 4 which will follow over the weekend; no promise on actual time I'm afraid. I just dug my NRF's out of the pile....

But if you understand so far, it should be relatively simple to split the previous and have one Uno send the variable and the other to receive it and use it in the switch...case.

Ok..... the radio part is done and tests fine, but the code's a bit "scrappy" so I'll tidy it up tomorrow and post above as part 4.

I just noticed this..

Marius93:
There will be 8 light stations and one control unit.

The code I wrote and which I will post later, is based on the Getting Started examples that came with the library. That Getting Started code sends data from Radio0 to Radio1, and Radio1 sends it back so you can see on Radio1's monitor that the path was completed.

I basically just did the same thing, sending the button number from Radio0 to Radio1, where Radio1 digests that and does its thing with the LEDs and sends the number back to Radio0 for a visual check.

I imagine that you could make 8x Radio1's which would happily digest the sent number and do their LED thing, and of course they wouldn't know there were other listeners. But it would confuse things to try to have all 8 echo-ing the number back, nor would it be necessary except for testing.

Unfortunately I have only 2x radios so I can't test with more than 1 receiver. (I thought I had 3... either I imagined that or I lost one.)

So what I'll do is, make the echo optional. I'll put a line in the code to turn it on or off, and each receiver could be tested individually if necessary. (Of course, they could all be tested together with no echo, and if the LEDs work then no further echo testing would be necessary anyway.)

My logic tells me that 8 identical receivers should be able to accept the message from the controller....

By the way, have you satisfied yourself that your radios have sufficient range?

I can't thank you enough for your help. I just tested the range with my current code and it all works fine.