/**
Author: tiger888
Date: 14/08/2024
Last Update: Initial Version 1.0
Sydney, Australia
******* THE PROJECT *********************************
Simulation: HC-SR04 (Ultrasonic Measurement Dustance
and NRF24L01 RF Transceiver (Trasmission Only)
*****************************************************
Project Components;
1 x Arduino Nano
1 x HC-SR04 Ultrasonic Measurement Distance Sensor
1 x NRF24L01 (RF Transceiver) - Transmitting only
1 x 7805 5v Voltage Regular
1 x 12v DC Power Suppy (Option - Can use mobile phone 5v Power Bank)
2 x Capacitors 470 micro farad (white noise filter)
2 x Resistors 220 ohm (LED current limiter)
1 x LED red (steady: distance < preset test distance, else falshing)
1 x LED yellow (steady when recived RX acknowlegement, else flashing)
*/
#include <SPI.h>
#include <RF24.h>
// PIR Pins ------
const int ledPin = 2;
const int trigPin = 9;
const int echoPin = 10;
const int detectedTime = 50; // 50cm
int presetDetectedDistance = 100; // detection for transmission
int testDistance = 300;
int delaySeconds = 200;
// TX LED Pin and Config
#define LED_TEST_TO_TX 3 // white
#define LED_TX_ON 4 // green
#define LED_TX_SUCCESS 5 // yellow
// Transmitter configurations----------
#define CE_PIN 7 // 9
#define CSN_PIN 8 // 10
RF24 radio(CE_PIN, CSN_PIN);
const byte slaveAddress[5] = {'R','x','A','A','A'};
char dataToSend[] = "Hello";
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000;
void setup() {
Serial.begin(9600);
// NRF24L01 Tranceiver-----------
pinMode(LED_TX_SUCCESS, OUTPUT); // Tx Status
digitalWrite(LED_TX_SUCCESS, LOW);
// Tx init
radio.begin();
radio.setDataRate(RF24_250KBPS);
radio.stopListening();
radio.openWritingPipe(slaveAddress);
// HC-SR04 Sensor -------
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
// Ultrasonic sensor operation
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Waits for the echo pin to get high
long duration = pulseIn(echoPin, HIGH);
int distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Simaulation fo testing only ------------
bool steady = false;
if(testDistance > distance ) steady = true;
switchLED(ledPin,steady,delaySeconds);
//----------------------------------------
// Wait to do next measurement
delay(500);
// TX only when distance is greater than 100 cm-------
if(distance>presetDetectedDistance){
unsigned long currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis) {
send();
prevMillis = millis();
}
} else {
Serial.print("distance is less than preset value: ");
Serial.println(presetDetectedDistance);
}
}
// -------------
// Transmit data
// -------------
void send() {
bool rslt = radio.write(&dataToSend, sizeof(dataToSend));
Serial.print("Data Sent: ");
Serial.print(dataToSend);
Serial.print(" @:");
Serial.println(millis());
/** testing only - remove when no longer require! *************/
rslt = false; // true: Rx Acknowledge, false: Rx No Acknowledge
/**************************************************************/
if (rslt) {
Serial.print("Rx Acknowledgment received");
Serial.print(" @:");
Serial.println(millis());
// constineously light up
digitalWrite(LED_TX_SUCCESS, HIGH);
} else {
Serial.println(" Tx failed");
// falshing LED
falshingLED(LED_TX_SUCCESS, 300);
}
}
void switchLED(int pinNbr, bool steady, int delaySeconds){
if(steady){
digitalWrite(pinNbr, HIGH);
delay(delaySeconds);
} else {
digitalWrite(pinNbr, HIGH);
delay(delaySeconds);
digitalWrite(pinNbr, LOW);
delay(delaySeconds);
}
}
// -------------
// falishing LED
// -------------
void falshingLED(int ledPin, int delayTime){
digitalWrite(ledPin, HIGH);
delay(delayTime);
digitalWrite(ledPin, LOW);
delay(delayTime);
}