Hello all.
I would like to ask for help for my garage door indicator project.
The intention is to detect with an HC-SR04 ultrasonic sensor (within 50cm) that the garage door is open and again transmits wirelessly through 2.4GHz NRF24L01 modules so that an LED lights up on the other arduino.
Only problem is that I dont't get the code working.
Here below a picture of the connection and the code!
Transmitter code:
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int msg[1];
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int trigPin=6;
int echoPin=7;
void setup()
{
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(pipe);
pinMode(trigPin, INPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
int duration,distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2)/29.1;
Serial.print(msg[0]);
Serial.println("");
delay(500);
if (distance <50) {
msg[0] = 111;
radio.write(msg, 1);
}
if (distance >50) {
msg[0] = 000;
radio.write(msg, 1);
}
}
Reciever code:
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int msg[1];
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int led = 7;
void setup()
{
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(1,pipe);
radio.startListening();
pinMode(led, OUTPUT);
}
void loop()
{
if (radio.available())
{
bool done = false;
done = radio.read(msg, 1);
if (msg[0] == 111)
{
digitalWrite(led, HIGH);
Serial.print("LED ");
Serial.print(msg[0]);
Serial.print(" On , ");
}
Serial.print("Received = ");
Serial.println(msg[0]);
delay(500);
}
if (msg[0] == 000)
{
digitalWrite(led, LOW);
Serial.print("LED ");
Serial.print(msg[0]);
Serial.print(" Off , ");
}
Serial.print("Received = ");
Serial.println(msg[0]);
delay(500);
}