/**
Author: tiger888
Date: 21/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 (alert condition: steady: distance < preset test distance, else falshing)
1 x LED yellow (steady when recived RX acknowlegement, else flashing
1 x LED green (ON when no detection of any motion)
1 x Buzzer (optional - can have both with red LED)
*/
#include <SPI.h>
#include <RF24.h>
// PIR Pins ------
const int detectionPin = 2;
const int sensorActivePin = 3;
const int trigPin = 9;
const int echoPin = 10;
/* alert distance with buzzer sounds and flashing Red LED
must be set at installtion time with respect to the detect object
Sensor <-detected zone (distance by sensor)-><-undetected zone: (alertDistance)->
** green LED >> no detection of motion
** red LED flashing and buzzer (optional) sound
*/
int alertDistance = 400; // 400 cm :: must adjust it when on site release
int delaySeconds = 200;
// TX LED Pin and Config
#define LED_TX_SUCCESS 5 // yellow
// Transmitter configurations------
#define CE_PIN 7
#define CSN_PIN 8
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(detectionPin, OUTPUT);
pinMode(sensorActivePin, OUTPUT);
// set default at the start
digitalWrite(detectionPin, LOW);
digitalWrite(sensorActivePin, LOW);
}
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.print(" cm");
// here, we test when the distance is outside
// of detection zone i.e. object has been removed
Serial.print(", Alert Distance: ");
Serial.println(alertDistance);
// Wait to do next measurement
delay(500);
/* activate the alarm when object has been removed
from detected zone (distance). Here we preset the alarm distance
i.e. when object is outside or moved from detected zone. We preset
this value with (alertDistance = 400 cm): Buzzer ON and Red LED flashing
*/
// default status of sensor detection
digitalWrite(sensorActivePin, LOW);
// test for detection
if(alertDistance > distance) {
unsigned long currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis) {
send();
// activate the alert
switchLED(detectionPin,false,delaySeconds);
prevMillis = millis();
}
} else {
Serial.print("distance is less than preset value: ");
Serial.println(alertDistance);
// green LED indicates, object is stationed
digitalWrite(sensorActivePin, HIGH);
}
}
/* Send alert RF signal */
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 = true; // true: Rx Acknowledge, false: Rx No Acknowledge
/**************************************************************/
if (rslt) {
Serial.print("Rx Acknowledgment received");
Serial.print(" @:");
Serial.println(millis());
// yellow LED ON (steady)
switchLED(LED_TX_SUCCESS,true,500);
} else {
Serial.println(" Tx failed");
// yellow LED (flashing)
switchLED(LED_TX_SUCCESS,false,500);
}
}
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);
}
}