Hi,
I'm creating a chicken coop door that will automatically open and close when it's night or day outside and alert me via the NF24l01 when the door is open or closed by lighting up an LED. I've successfully created a working prototype of that door opening part of the code. I am having some serious difficulty with the NF24l01! I have tried multiple examples from all corners of the internet. I have an Arduino UNO that will serve as the transmitter and another Arduino UNO that will serve as a receiver. I have 2 switches attached to the door. One that will switch on (go from 0 to 1) when the door is open and the other will switch on when the door is closed (also goes from 0 to 1). The Arduino would then send the door's state to the other Arduino. The other Arduino would then turn on an LED that is labeled as up when the door is up, and turn on another LED when the door is down. Unfortunately none of that works.
I've loaded the LED remote Example and that example doesn't send anything to the other Arduino. I tried the Getting Started example and that did work, I just don't understand how to modify it for my use.
Any help would be super appreciated!
I am using the
GitHub - nRF24/RF24: OSI Layer 2 driver for nRF24L01 on Arduino & Raspberry Pi/Linux Devices library.
Right now on the Arduino Uno attached to the door the pin layout is
CS-2
CE-4
MOS -11
MSC-12
SCK -13
For the switch it goes
Door up -8
Door down -9
And on the LED Arduino it's
CS-2
CE-4
MOS -11
MSC-12
SCK -13
The Transmitter Arduino's Code
#include <SPI.h>
#include "RF24.h"
boolean powerState = LOW;
int photocell = 0;
int upDetect = 11;
int downDetect = 9;
int manualUp = 10;
int manualDown = 3;
int upswitchReading;
int downswitchReading;
int photocellReading;
int manualUpReading;
int manualDownReading;
int DATA;
RF24 radio(2, 4);
byte addresses[][6] = {"1Node", "2Node"};
void setup(void) {
pinMode (upDetect, INPUT_PULLUP);
pinMode (downDetect, INPUT_PULLUP);
pinMode (manualDown, INPUT_PULLUP);
pinMode (manualUp, INPUT_PULLUP);
pinMode (6, OUTPUT);
pinMode (5, OUTPUT);
digitalWrite (5, HIGH);
radio.begin();
radio.setPALevel(RF24_PA_LOW);
radio.openWritingPipe(addresses[1]);
Serial.begin(9600);
delay(100);
}
void loop(void) {
Serial.println("Up Switch Reading = ");
upswitchReading = digitalRead(upDetect);
Serial.println(upswitchReading, DEC);
delay(1000);
Serial.println("Down Switch Reading = ");
downswitchReading = digitalRead(downDetect);
Serial.println(downswitchReading, DEC);
delay(1000);
Serial.println("Manual Down Switch = ");
manualDownReading = digitalRead(manualDown);
Serial.println(manualDownReading, DEC);
delay(1000);
Serial.println("Manual UP Switch = ");
manualUpReading = digitalRead(manualUp);
Serial.println(manualUpReading, DEC);
delay(1000);
Serial.println(" Photocell reading = ");
photocellReading = analogRead(photocell);
Serial.println(photocellReading);
delay(1000);
if (photocellReading < 400) {
powerState = HIGH;
}
else if (photocellReading > 400) {
powerState = LOW;
}
if (manualDownReading == 0) {
powerState = HIGH;
}
else if (manualUpReading == 0) {
powerState = LOW;
}
digitalWrite(6, powerState);
if (downswitchReading == 1) {
DATA == 100;
}
if (downswitchReading == 0 || upswitchReading == 0) {
DATA == 250;
}
if (upswitchReading == 1) {
DATA == 200;
}
radio.write( &DATA, sizeof(DATA ) );
Serial.print("DATA = ");
Serial.println(DATA);
}
The Receiver Arduino's Code
#include <SPI.h>
#include "RF24.h"
RF24 radio (2, 4);
byte addresses[][6] = {"1Node", "2Node"};
int dataReceived;
int UpLED = 10;
int DownLED = 6;
void setup() {
pinMode(UpLED, OUTPUT);
pinMode (DownLED, OUTPUT);
radio.begin();
radio.setPALevel(RF24_PA_MIN);
// myRadio.setPALevel(RF24_PA_MAX);
radio.openReadingPipe(1, addresses[0]);
radio.startListening();
Serial.begin(115200);
delay(1000);
}
void loop()
{
if ( radio.available()) // Check for incoming data from transmitter
{
while (radio.available()) // While there is data ready
{
radio.read( &dataReceived, sizeof(dataReceived) ); // Get the data payload (You must have defined that already!)
}
if (dataReceived == 200) {
digitalWrite(DownLED, HIGH);
}
if (dataReceived == 250) {
digitalWrite(DownLED, LOW);
digitalWrite(UpLED, LOW);
}
if (dataReceived == 100) {
digitalWrite(UpLED, HIGH);
}
Serial.print("Data received = ");
Serial.println(dataReceived);
}
}