having some trouble with a button

so long story short I’m trying to make a button press on one Arduino turn an led on/off on the other Arduino using nrf radio transceiver things. I want the button to be a toggle, and that’s pretty much where I’m having issues. I know the wiring is fine because I got it to send hello world between the two using the nrf things. End goal, A button by my desk will trigger a relay, locking or unlocking my door but once I get it working with an led I’ll have no problem swapping it for a relay.
I’ll post the code below
EDIT:
Hi, 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.

Transceiver-

#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);
    }
  }
}
    newButtonState = HIGH;

Why ?

UKHeliBob:

    newButtonState = HIGH;

Why ?

Im not sure, what should I replace that with.

Welcome to the Forum. Please read these two posts:

General Guidance and How to use the Forum
and
Read this before posting a programming question …
You may also find useful information that would answer your question here:
Useful links - check here for reference posts / tutorials

You have posted code without using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don’t do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The “Code: [Select]” feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing or review.

If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower right corner of the message. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

tlinaris61:
Im not sure, what should I replace that with.

Probably nothing.

I’d also recommend....

  1. Not transmitting continuously in a loop like that. Leave a delay after every Tx.
  2. Do nothing at the receiver if no data is available. Currently you are overriding the pin state set by a valid rx, so it will be next to impossible to detect if anything is received.

tlinaris61:
Im not sure, what should I replace that with.

Just don't do it

As it stands oldButtonState will always be HIGH as soon as loop() runs so

 if (newButtonState == HIGH && oldButtonState == LOW)

will never be true

pcbbc:
Probably nothing.

I’d also recommend…

  1. Not transmitting continuously in a loop like that. Leave a delay after every Tx.
  2. Do nothing at the receiver if no data is available. Currently you are overriding the pin state set by a valid rx, so it will be next to impossible to detect if anything is received.

I took your advice, and now what I’m getting a different problem. The led switches from on and off very fast, and if I press the button while it’s on, it will turn and stay off as long as I hold the button and vise versa if it were off when I pressed it. I’m trying to make it just stay on until I press the button again and the other way around. Ill put my updated code below

Transceiver -

#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 = 1;
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
   {
     digitalWrite(4, 1);
   }
 }
}