#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
// Set the CE & CSN pins
#define CE_PIN 9
#define CSN_PIN 10
// Level Sensor Pin
#define PIN_TRIG 3
#define PIN_ECHO 2
// Create a Radio
RF24 radio(CE_PIN, CSN_PIN);
// The tx/rx address
const byte rxAddr[6] = "00001";
void setup() {
Serial.begin(115200);
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
// Start the Radio!
radio.begin();
// Power setting. Due to likelihood of close proximity of the devices, set as RF24_PA_MIN (RF24_PA_MAX is default)
radio.setPALevel(RF24_PA_MIN); // RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH, RF24_PA_MAX
// Slower data rate for better range
radio.setDataRate( RF24_250KBPS ); // RF24_250KBPS, RF24_1MBPS, RF24_2MBPS
// Number of retries and set tx/rx address
radio.setRetries(15, 15);
radio.openWritingPipe(rxAddr);
// Stop listening, so we can send!
radio.stopListening();
}
void loop() {
// Start a new measurement:
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
// Read the result:
int duration = pulseIn(PIN_ECHO, HIGH);
Serial.print("Distance in CM: ");
Serial.println(duration / 58);
Serial.print("Distance in inches: ");
Serial.println(duration / 148);
int level=duration / 58;
Serial.println(level);
radio.write(level);
delay(1000);
}