I am working on getting an uno(transmitter) to send multiple button states to a mega(reciever) to activate relays. This is my first time messing with any arduino so I have been scratching along trying to get this working. This is what i have come up with so far. The loop on the uno(transmitter) wont run unless I quote out 2 of the radio.write functions and leave just one remaining but when I do that and jump the pin to make it high the mega changes all 3 states to high instead of just one.. Any help would be appreciated...
Transmitter code
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define bin1 2
#define bin2 3
#define bin3 4
RF24 radio(7, 8); // CE, CSN
const byte addresses[][6] = {"00001", "00002"};
boolean bin1state = 0;
boolean bin2state = 0;
boolean bin3state = 0;
void setup() {
pinMode(bin1, INPUT);
pinMode(bin2, INPUT);
pinMode(bin3, INPUT);
radio.begin();
radio.setChannel(120);
radio.openWritingPipe(addresses[0]); // 00002
radio.openReadingPipe(1, addresses[1]); // 00001
radio.setPALevel(RF24_PA_MIN);
Serial.begin(9600);
radio.stopListening();
Serial.println("setup finished");
}
void loop() {
delay(500);
Serial.println("loop begun");
bin1state = digitalRead(bin1);
radio.write(&bin1state, sizeof(bin1state));
bin2state = digitalRead(bin2);
radio.write(&bin2state, sizeof(bin2state)); <------If i quoute out this
bin3state = digitalRead(bin3);
radio.write(&bin3state, sizeof(bin3state)); <------ and this the code will loop like it should but i need
Serial.println("Loop finish"); these states sent also.
}
And, to make it easier for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum
I think I read around and looked at enough examples to figured it out. i Have it running now. There is a bit of delay but thats not a big deal. This is what I ended up with.
You should avoid delay and other blocking code when programming any type of communication.
There is no need to use ints for bins, bytes work just the same.
You could even use bits and transmit a single byte with 5 unused bits.
My version sends a new packet when any button changes, or each 500 ms.