constant serial print

it should only print forward if the button is pressed but it is constantly saying fporward instead

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
int msg[1];
RF24 radio(22, 23);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int SW1 = 7;

void setup(void)
{
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(pipe);
pinMode(SW1,INPUT);
}

void loop(void)
{
  
  // if there is data ready
  if( digitalRead(SW1) == HIGH)
  {
    delay(10);
    msg[0] = 111;
    radio.write(msg, 1);
    Serial.println("Forward");
  }
}

How is your switch wired? Do you have a pulldown resistor?

Yellow to pin 7 as input
White to 5v
Black to grnd

The preferred way to wire a momentary pushbutton switch; Use pinMode(pin, INPUT_PULLUP); to enable the internal pullup. Note that the input is pulled low on switch closure.

The wiring of some 4 leg button switches can be confusing because two of the four legs are always connected to each other . If you can confirm the operational behavior or your button with a multimeter it would be best. I think that if you wire diagonally across the button then it will always be open when not pressed, and it will close the circuit when pressed.

Wiring should be from the input pullup pin, diagonally across the switch to ground.

so i completly unplugged the button and it is still cantinually putting out "Forward" in the serial monitor

Have you got a pulldown resistor on pin 7 ?

If not, then change

  pinMode(SW1, INPUT);

to

  pinMode(SW1, INPUT_PULLUP);

and

  if ( digitalRead(SW1) == HIGH)

to

  if ( digitalRead(SW1) == LOW)

and make sure that the switch is wired to take the pin LOW when pressed.

ether way if the button isnt connected shouldnt it not be printing anything?

merkzilla:
ether way if the button isnt connected shouldnt it not be printing anything?

Nope, if it isn’t connected to anything then it is floating, so you can’t say what state the pin is in. It might be HIGH the whole time.

What might the circuit look like for it to react when low?

cattledog:
Wiring should be from the input pullup pin, diagonally across the switch to ground.

Thanks for the lo I will try this and update later

Thanks for the help this worked now it only prints when k push the button.... Now if I can only get the other nrf24 module to read the signal