Help Needed Decoding Binary number and writing pins.

Hi all,

I’m new here and I would like to thank you very much for this helpful forum.

I’m new to programming and arduino and I would be glad if I can get some guidance. I searched, but I found no solution to what I’m trying. I hope that I post the right place.

I have setup a wireless communication using the NRF24L01. My transmitter transmits the state of 7 pins (pin 2,3,4,5,6,7,8) to my receiver as a binary number. For example all High = 1111111 and pin 3 low is 1011111. If pin 3 and pin 8 is zero, I get 1011110.

Now I want to use my code in the receiver to check for the zero’s and the locations of the zero’s and turn corresponding pins to High or Low simultaneously.

I’ve tried it with the code below, it works fine for 2 Low’s on the transmitter, but when I have 3 Low’s, the decoding options on the receiver side gets too long.

It is important for me that all the pins are read simultaneous and the is (almost) no delay in the switching.

I hope that I can get some help?

Thank you very much in advance.

Kind regards,

Example.ino (4.67 KB)

Use another array for the output pins. If I understand your goal correctly, something like this could work (untested):

int relays[]={relay1,relay2,relay3,relay4,relay5,relay6,relay7};

for(int i=0; i<7; i++) {
   if (message[i]==0) digitalWrite(relays[i],HIGH);
   else digitalWrite(relays[i],LOW);
}

The message array could be replaced by the bits of a single byte.

I would start by sending a single byte with its bits set to match that of the 7 pins

On the receiving side read the state of each bit of the byte using bitRead() and set the state of the corresponding output pin to match.

Hi All,

Thank you very much for the response.

I've been able to get my code fully functional with the code provided by: jremington.

Wishing you all a nice day further.

Hi all,

I would like to ask another question. Now I have a problem, that when my Transmitter turns off, the receiver keeps processing the last seen value. For example pin 2 was Triggered when the transmitter went off (due draining of 9V battery), the receiver holds Pin 2 High (as programmed), while this must be momentary. I want my transmitter switched to be momentary, and in case my transmitters doesn't transmit anything (because it's down), the receiver must go back to the original state.

How can I fix this?

Thank you very much in advance.

the receiver must go back to the original state.

How will you tell the difference between the transmitter not sending the data to change the state of the pin and the transmitter not sending anything?

When you can answer that question, the solution will be crystal clear.

Arvind18:
I have setup a wireless communication using the NRF24L01. My transmitter transmits the state of 7 pins (pin 2,3,4,5,6,7,8) to my receiver as a binary number. For example all High = 1111111 and pin 3 low is 1011111. If pin 3 and pin 8 is zero, I get 1011110.

I must say up front that I am lazy. So I would store the 7 pin states in a byte array and send that. Then on the receiving side all you would need is a simple FOR loop to iterate over the array - something like

for (byte n = 0; n < 7; n++) {
  digitalWrite(outputPin[n], receivedVal[n]);
}

…R
Simple nRF24L01+ Tutorial

I don’t understand:
You want write a number that you have on pins or take a number from input pins?
In the first case

void Writenumber(byte number)
{
for (byte i=0;i<8;i++)
{
digitalWrite (pin[i], number%2);
number=number/2;
}
}

For the second thing

byte Readnumber ()
{
byte number=0;
for (char i=7;i>-1;i--)
{
number=number*2+digitalRead (pin[i]);
}
return number;
}

They AREN’T codes, but just IDEAS of code. You have to understand than and transfoem than into codes