Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // SimpleTx - the master or the transmitter
- #include <SPI.h>
- #include <nRF24L01.h>
- #include <RF24.h>
- #define CE_PIN 9
- #define CSN_PIN 10
- const byte slaveAddress[][5] = {{'R', 'x', 'A', 'A', 'A'}, {'R', 'x', 'A', 'A', 'B'},
- {'R', 'x', 'A', 'A', 'C'}, {'R', 'x', 'A', 'A', 'D'}};
- RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
- char dataToSend[10] = "Message 0";
- char txNum = '0';
- unsigned long currentMillis;
- unsigned long prevMillis;
- unsigned long txIntervalMillis = 1000; // send once per second
- void setup() {
- Serial.begin(9600);
- Serial.println("SimpleTx Starting");
- radio.begin();
- radio.setDataRate( RF24_250KBPS );
- radio.setRetries(3, 5); // delay, count
- int numSlaves=4;
- radio.enableDynamicPayloads() ;
- radio.setAutoAck( true ) ;
- }
- //====================
- void loop() {
- currentMillis = millis();
- if (currentMillis - prevMillis >= txIntervalMillis) {
- send();
- prevMillis = millis();
- }
- }
- //====================
- void send() {
- bool rslt;
- for (byte n = 1; n < 3; n++) {
- radio.openWritingPipe(slaveAddress[n]);
- rslt = radio.write( &dataToSend, sizeof(dataToSend) );
- // Always use sizeof() as it gives the size as the number of bytes.
- // For example if dataToSend was an int sizeof() would correctly return 2
- Serial.print("Data Sent to Slave ");
- Serial,println(n);
- Serial.print(dataToSend);
- if (rslt) {
- Serial.print(" Acknowledge received from Slave ");
- Serial.println(n);
- updateMessage();
- }
- }
- else {
- Serial.println(" Tx failed");
- }
- }
- //================
- void updateMessage() {
- // so you can see that new data is being sent
- txNum += 1;
- if (txNum > '9') {
- txNum = '0';
- }
- dataToSend[8] = txNum;
- }
Add Comment
Please, Sign In to add comment