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.
