I have two NRF 24 boards that I am trying to get to communicate. The goal is to have an ultrasonic sensor on one board report it's data to another board via the nrf board communications. Then have an led on the other board go off when the sensor reads below a value of 30. Here are the codes for the transmitter and receiver.
Receiver:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define led 13
RF24 radio(7, 8); // CE, CSN
byte address[6] = "00001";
boolean distance = 0;
void setup() {
Serial.begin(115200);
pinMode(led,OUTPUT);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
}
void loop() {
while (!radio.available())
radio.read(&distance, sizeof(distance));
if (distance <= 30) {
digitalWrite(led, HIGH);
}
else {
digitalWrite(led, LOW);
}
}
Transmitter:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define led 13
#define trigPin 2
#define echoPin 3
int sound = 250;
RF24 radio(7, 8); // CE, CSN
byte address[6] = "00001";
boolean distance = 0;
void setup() {
Serial.begin(115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
radio.write(&distance, sizeof(distance));
}
the program is not working at the moment, any help would be appreciated.