Hello
,
I try to make 4 arduino uno boards communicate by nrf24l01 radio modules.
Every board is wired with an LED as well as a button. By pressing the button the radio module should change from receive mode to transmit mode and make the LEDs of the other boards flash.
Since other codes I wrote work well, I know that the LEDs, the buttons and the radio modules are wired correctly.
My code looks like that:
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int msg[1];
RF24 radio(9, 10);
const uint64_t pipe1 = 0xE8E8F0F0E1LL;
const uint64_t pipe2 = 0xF0F0F0F0AA;
const uint64_t pipe3 = 0xF0F0F0F066;
const uint64_t pipe4 = 0xF0F0F0F0F0;
int SW1 = 7;
int LED1 = 3;
int interruptPin = 0;
int radionumber = 1;
void leuchten() {
if (radio.available()) {
radio.read(msg, 1);
digitalWrite(LED1, HIGH);
}
if (!radio.available()) {
digitalWrite(LED1, LOW);
msg[0] = 0;
}
}
void setup(void) {
attachInterrupt(interruptPin, leuchten, FALLING);
Serial.begin(115200);
radio.begin();
//radio.openWritingPipe(pipe);
//radio.openReadingPipe(1, pipe);
pinMode(LED1, OUTPUT);
if (radionumber == 1) {
radio.openWritingPipe(pipe1);
radio.openReadingPipe(1, pipe2);
radio.openReadingPipe(2, pipe3);
radio.openReadingPipe(3, pipe4);
}
if (radionumber == 2) {
radio.openWritingPipe(pipe2);
radio.openReadingPipe(1, pipe1);
radio.openReadingPipe(2, pipe3);
radio.openReadingPipe(3, pipe4);
}
if (radionumber == 3) {
radio.openWritingPipe(pipe3);
radio.openReadingPipe(1, pipe1);
radio.openReadingPipe(2, pipe2);
radio.openReadingPipe(3, pipe4);
}
if (radionumber == 4) {
radio.openWritingPipe(pipe4);
radio.openReadingPipe(1, pipe1);
radio.openReadingPipe(2, pipe2);
radio.openReadingPipe(3, pipe3);
}
}
void loop(void) {
if (digitalRead(SW1) == HIGH) {
radio.stopListening();
Serial.println("send");
msg[0] = 1;
radio.write(msg, 1);
}
else {
radio.startListening();
Serial.println("receive");
Serial.println(msg[0]);
}
}
I adapt the variable radionumber for every board of course.
The serial monitor displays "receive" and "0". Pressing the button causes the monitor to display "send" several times, but then nothing more happens. The program seems to abort. No LED flashes.
Does anybody notice a mistake in my setup or code?