Respected members. I have 2 arduino nano with NRF24L01 modules. I have followed Robin's tutorial and everything works perfectly, this makes sure there is no hardware problem. I have wired everything according to his tutorial. I have also ensured that I am using the same library that Robin has recommended ( tmrh version). I have also connected capacitors to nrf24l01 module and used a dedicated 3.3v power supply to ensure nothing goes wrong.
I have coded the arduino to run different animations on a strip of WS2812B LED. There are 11 arduino pins configured as input that detect a 5v input and the animation on LED strip is changed accordingly with the help of IF statements. And this circuit works flawlessly.
Now I want to assign a "unique code" to these 11 conditions and then I want to send the unique code to another arduino through the NRF24L01 module so that both arduino run the same animation wirelessly.
I am a business graduate and I do electronics on weekends because I love making stuff. I really appreciate all the help this forum has provided me with.
One simple solution is to have an array with an element for each input and when you read the inputs save the values in the array. Then send the array to the other Arduino. Something like
// at the top of the program
const byt numInputs = 11;
byte inputPIns[numInputs] = {2,3,4,5,6,7,8,9,10,11}; // these should be replaced with the numbers for your input pins
byte pinStates [numInputs];
// in loop()
for (byte n = 0; n < numInputs, n++) {
pinStates[n] = digitalRead(inputPins[n]);
}
// when sending
radio.write(&pinStates, sizeof(pinStates));
This approach would allow you to have the exact same code on both Arduinos for choosing the animations as both would just use the array pinStates[]
In your Original Post you mention 11. However 11 inputs would give you a possible 2048 combinations so I suspect that is not what you have in mind. Perhaps you can explain more clearly.
Another way of dealing with the inputs, which may help simplify the IF statements, is to assign a power-of-two value to each input pin with something like this
byte inputValue = 0;
for (byte n = 0; n < numInputs, n++) {
pinState = digitalRead(inputPins[n]);
inputValue += pinState << n;
}
The << operator shifts the value (in this case a 0 or a 1) a number of positions to the left. Which is the same as multiplying by 2 n times.
At the end the contents of inputValue will be unique for any combination of inputs. For example if there were three inputs it would have one of the 8 values
0000
0001
0010
0011
0100
0101
0110
0111
and you could use a SWITCH/CASE to choose between them
Note that if you have more than 256 options inputVal will have to be an int rather than a byte
Thanks A lot Robin and anyone that will come across this post. I was finally able to complete the project I started with a 100% success. It was only possible with the help of experts like you. I really appreciate all the help this forum has provided me with.