-
-
Save chaeplin/84831755abe7268f60c4 to your computer and use it in GitHub Desktop.
nRF24 multiple transmitters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include "nRF24L01.h" | |
#include "RF24.h" | |
int senderId; | |
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10 | |
//Contacts from the radio to connect NRF24L01 pinamnam -> Arduino | |
//SCK -> 13 | |
//MISO -> 12 | |
//MOSI -> 11 | |
//CSN -> 10 | |
//CE -> 9 | |
RF24 radio(9, 10); | |
// this is not the channel address, but the transmitter address | |
const uint64_t pipe = 0xE8E8F0F0E1LL; | |
//LEDs connected to these pins | |
// ENSURE YOU HAVE THE RIGHT DIGITAL PINS HERE | |
int LEDpins[5] = { 2, 3, 4, 5, 6 }; | |
void setup(void) { | |
Serial.begin(9600); | |
radio.begin(); | |
// the following statements improve transmission range | |
radio.setPayloadSize(2); // setting the payload size to the needed value | |
radio.setDataRate(RF24_250KBPS); // reducing bandwidth | |
radio.openReadingPipe(1, pipe); // Open one of the 6 pipes for reception | |
radio.startListening(); // begin to listen | |
// Enable all the LED pins as output | |
for (int i = 0; i < 5; i++) { | |
pinMode(LEDpins[i], OUTPUT); | |
digitalWrite(LEDpins[i], LOW); // this is unnecessary but good practice nonetheless | |
} | |
} | |
void loop(void) { | |
// Turns off all the LEDs | |
for (int i = 0; i < 5; i++) { | |
digitalWrite(LEDpins[i], LOW); | |
} | |
if (radio.available()) { | |
// this while is here to throw away all the packets but the last one | |
bool done = false; | |
while (!done) { | |
// read and write expect a reference to the payload (& symbol) | |
// second argument is the packet length in bytes (sizeof(int) == 2) | |
done = radio.read(&senderId, 2); | |
} | |
//Light up the correct LED for 50ms | |
digitalWrite(LEDpins[senderId], HIGH); | |
Serial.print("LED "); | |
Serial.print(senderId); | |
Serial.println(" On"); | |
delay(50); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include "nRF24L01.h" | |
#include "RF24.h" | |
int transmitterId; | |
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10 | |
//Contacts from the radio to connect NRF24L01 pinamnam -> Arduino | |
//SCK -> 13 | |
//MISO -> 12 | |
//MOSI -> 11 | |
//CSN -> 10 | |
//CE -> 9 | |
RF24 radio(9, 10); | |
// this is not the channel address, but the transmitter address | |
const uint64_t pipe = 0xE8E8F0F0E1LL; | |
//button connected to these pins | |
int buttonPin1 = 2; | |
void setup(void) { | |
// CHANGE THIS PER EACH TRANSMITTER, from 0 to 4 | |
transmitterId = 0; | |
radio.begin(); | |
// the following statements improve transmission range | |
radio.setPayloadSize(2); // setting the payload size to the needed value | |
radio.setDataRate(RF24_250KBPS); // reducing bandwidth | |
radio.openWritingPipe(pipe); // set the transmitter address | |
} | |
void loop(void) { | |
//until the button (buttonPin1) pressed send the package (id) to receiver Arduino | |
if (digitalRead(buttonPin1) == HIGH) { | |
// some implementations automatically shut down the radio after a transmission: this | |
// ensures the radio is powered up before sending data | |
radio.powerUp(); | |
// read and write expect a reference to the payload (& symbol) | |
// second argument is the packet length in bytes (sizeof(int) == 2) | |
radio.write(&transmitterId, 2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment