problem with digital input

Hello everyone.
I'm working on my radio controller for my airplane.
I want to use the joystick button to change a servo position between 3 positions.
I soldered the button to D3 of Arduino nano and here is the code I wrote, but it doesn't work.

#include <SPI.h>
#include <nRF24L01.h> //Remember to isntall this bibrry: http://www.electronoobs.com/engarduino_NRF24_lib.php
#include <RF24.h>

const uint64_t my_radio_pipe = 0xE8E8F0F0E1LL;


RF24 radio(9, 10);

struct Data_to_be_sent {
  byte ch1;
};

Data_to_be_sent sent_data;

const int btnleft = 3; // left joystick button as digital pin 3
int btnval = 0;
int flaps = 0;
int btnstate;

void setup()
{
  
  pinMode(btnleft, INPUT);
  
  radio.begin();
  radio.setAutoAck(false);
  radio.setDataRate(RF24_250KBPS);
  radio.openWritingPipe(my_radio_pipe);

  //Reset each channel value
  sent_data.ch1 = 0;
}

void loop()
{
  
  btnstate = digitalRead(btnleft);
  
  if (btnstate == HIGH && btnval == 0){
    btnval = 1;
    doflaps(); //see: "void doflaps()" in the buttom of this page!
  }
  
  if (btnstate == LOW && btnval==1){
    btnval=0;
  }
  
  if (flaps == 0){
    sent_data.ch1 = 0;
  }
  
  if (flaps == 1){
    sent_data.ch1 = 127;
  }
  
  if (flaps == 2){
    sent_data.ch1 = 255;
  }
  
  radio.write(&sent_data, sizeof(Data_to_be_sent));

}

void doflaps()
{
  if (flaps == 0){
    flaps = 1;
  }
  if (flaps == 1){
    flaps = 2;
  }
  if (flaps == 2){
    flaps = 0;
  }    
}

The final purpose of this code is to send "sent_data.ch1" whether as 0 or 127 or 255.
But I don't get any response at the receiver side.
I guess the problem is why my code because before this, I would change this servo position using a potentiometer but as I only need 3 positions I thought it would be better to use a button to change its position.

Attach the wiring of the button to D3.

Railroader:
Attach the wiring of the button to D3.

Thank you for your reply
What do you mean by attaching it?
It's on a perfboard and I soldered the button pin to D3.
This is the joystick that I use:

Send the wiring diagram of the "button" here in a post showing how the circuit looks like. The picture does not tell how it is wired inside.

When you press the button, does the SW pin go to 5V or GND?

I think "VRx" and "VRy" are a strong clue - "variable resistor" or potentiometer. I'll bet you find 2.5V there when 5V and ground are connected.

What id the voltage at the pin when the button is not pressed?

Oh, sorry, now I see the "SW" so it's probably a joystick and a button switch. So yeah, what do you see on all the pins, just to be sure.

Hi,
Do you have a DMM?
Have you built your code in stages?
If so do you have code that just reads the joystick/buttons and shows the result on the IDE serial monitor to prove you are reading the joystick correctly?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile: