Garage door indicator

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);
}

In what way doesn't it work? Do you see sensible output on the transmit side? Is there any indication that data is transmitted and received (cant remember if nrf24 radios have LED's for TX/RX)? You could add a debug print just after "radio.read()" to check if you are getting anything.

int msg[1];
...
if (distance <50) {
  msg[0] = 111;
  radio.write(msg, 1);

The variable "msg" is a two-byte field (that's how big an int is on this platform). However you are only writing one byte.

Ditto for the receiving end.

Okay and what in the code do I have to change for writing two-byte?

  radio.write(msg, 1);

That writes one byte. See if you can guess what you change to write 2 bytes.

I have no idea, That's why i'm asking. :wink:

Anyway i've got it working :slight_smile:

How?