So basically my goal is to have a button on one Arduino Uno, toggle an led on and off on the other using these NRF transceiver things. I know I have the wiring right, because the sample program I wrote that simply prints out hello world on the other Arduino's cereal monitor works fine. I think I'm just overthinking how to make the button toggle and that's what's messing the circuit up. Anyway, I'll post my code, lmk if you guys see anything that I could change.
I've made some progress, but I'm still stuck. The toggle isn't working and the led is flashing really fast on and off. When I press the button, The led will switch to its opposite state but go back to flashing once I've let the button go.
I've attached my code below
transmitter-
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define button 4
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
boolean lock = 0;
int oldButtonState = LOW;
int newButtonState;
void setup()
{
pinMode (button, INPUT);
radio.begin();
radio.openWritingPipe(address[0]);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
}
void loop()
{
newButtonState = digitalRead(button);
if (newButtonState == HIGH && oldButtonState == LOW)
{
lock = !lock;
radio.write(&lock, sizeof(lock));
}
oldButtonState = newButtonState;
delay(5);
}
receiver-
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
boolean lock;
void setup()
{
radio.begin();
radio.openReadingPipe(1, address[0]);
radio.setPALevel(RF24_PA_MIN);
pinMode(4,OUTPUT);
}
void loop()
{
radio.startListening();
while (!radio.available());
if (radio.available())
{
radio.read(&lock, sizeof(lock));
if (lock == 0)
{
digitalWrite(4, 0);
}
else if (lock == 1)
{
digitalWrite(4, 1);
}
}
}
DoorLockDESK.ino (610 Bytes)
DoorLockDOOR.ino (560 Bytes)