My code is not 100% working

Hello everyone.
I need help with my code.
I made a remote with 4 push buttons using Arduino nano + NRF24. The remote is controlling 2 servo motors (continuous rotation) which are hooked to an Arduino nano + NRF24.

Please first read my code below to understand where my problem is.

My code is actually working but the remote, when I push the UP button, is not showing "serv1 = 1" in the Serial Monitor and is also not sending this information over the radio.
Any other variant is working both showing in monitor and sending over the radio. Even the servos are working, one rotating both ways and one only one way since is not receiving the information to rotate the oder way as well.
First i thought that that I made a mistake when I soldered the button but I switched the input pins (UP with DOWN) and then the UP button works but the DOWN one is not working now.
I think my problem is only in the remote.
If anyone knows where my problem is, please let me know.

Transmitter code

#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>

RF24 radio(7, 8);

const byte address[6]= "00007";

#define BUTUP 5
#define BUTDOWN 2
#define BUTRIGHT 3
#define BUTLEFT 4

int BUTUPstate = 0;
int BUTDOWNstate = 0;
int BUTRIGHTstate = 0;
int BUTLEFTstate = 0;

int serv1 = 2;
int serv2 = 5;


void setup() {
  
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MAX);
  radio.stopListening();

  pinMode(BUTUP, INPUT_PULLUP);
  pinMode(BUTDOWN, INPUT_PULLUP);
  pinMode(BUTRIGHT, INPUT_PULLUP);
  pinMode(BUTLEFT, INPUT_PULLUP);

  Serial.begin(9600);

}

void loop() {
  BUTUPstate = digitalRead(BUTUP);
  BUTDOWNstate = digitalRead(BUTDOWN);
  BUTRIGHTstate = digitalRead(BUTRIGHT);
  BUTLEFTstate = digitalRead(BUTLEFT);

  if (BUTUPstate == 1)
  {
    serv1 = 1;
  }
  else if (BUTUPstate == 0)
  {
    serv1 = 2;
  }
  if (BUTDOWNstate == 1)
  {
    serv1 = 3;
  }
  else if (BUTDOWNstate == 0)
  {
    serv1 = 2;
  }

if (BUTRIGHTstate == 1)
  {
    serv2 = 4;
  }
  else if (BUTRIGHTstate == 0)
  {
    serv2 = 5;
  }
  if (BUTLEFTstate == 1)
  {
    serv2 = 6;
  }
  else if (BUTRIGHTstate == 0)
  {
    serv2 = 5;
  }

 Serial.print(serv1);
 Serial.print("\t");
 Serial.println(serv2);

 radio.write(&serv1, sizeof(serv1));
 radio.write(&serv2, sizeof(serv2));

 delay(100);
}

Receiver code

#include <Servo.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>

RF24 radio(7, 8);

const byte address[6]= "00007";

int serv1 = 2;
int serv2 = 5;

Servo mserv1;
Servo mserv2;

void setup() {


  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MAX);
  radio.startListening();

  Serial.begin(9600);

  mserv1.attach(2);
  mserv2.attach(3);

}

void loop() {
  if (radio.available())
  {
    radio.read(&serv1, sizeof(serv1));
    radio.read(&serv2, sizeof(serv2));

  }

  if (serv1 == 2)
  {
   mserv1.write(90);
  }
  if (serv1 == 1)
  {
   mserv1.write(20);
  }
    if (serv1 == 3)
  {
   mserv1.write(170);
  }


  if (serv2 == 5)
  {
   mserv2.write(90);
  }
  if (serv2 == 4)
  {
   mserv2.write(20);
  }
    if (serv2 == 6)
  {
   mserv2.write(170);
  }

 Serial.print(serv1);
 Serial.print("\t");
 Serial.println(serv2);

delay(100);
}

Add prints in all your if to see where the code goes.
Could you have cross talks between your buttons?
If would be safer to send a struct with the two values in one transaction

You state: "My code is actually working but the remote, when I push the UP button, is not showing "serv1 = 1" in the Serial Monitor and is also not sending this information over the radio." How do you know it is software. Post a schematic, not a frizzy thing and we quickly eliminate 1/2 of the potential problem.

Don't send and receive two single items, put both of them in a struct and send and receive that.

I have changed the code in the remote and is working as intended. Thanks for the support and replys.

#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>

RF24 radio(7, 8);
const byte address[6]= "00007";

#define BUTUP 5
#define BUTDOWN 2
#define BUTRIGHT 3
#define BUTLEFT 4

int BUTUPstate = 0;
int BUTDOWNstate = 0;
int BUTRIGHTstate = 0;
int BUTLEFTstate = 0;

int serv1 = 2;
int serv2 = 5;

void setup() {  
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MAX);
  radio.stopListening();

  pinMode(BUTUP, INPUT_PULLUP);
  pinMode(BUTDOWN, INPUT_PULLUP);
  pinMode(BUTRIGHT, INPUT_PULLUP);
  pinMode(BUTLEFT, INPUT_PULLUP);

  Serial.begin(9600);
}

void loop() {
  BUTUPstate = digitalRead(BUTUP);
  BUTDOWNstate = digitalRead(BUTDOWN);
  BUTRIGHTstate = digitalRead(BUTRIGHT);
  BUTLEFTstate = digitalRead(BUTLEFT);

   if (BUTUPstate == 1)
  {
    serv1 = 1;
  }
  else if (BUTDOWNstate == 1)
  {
    serv1 = 3;
  } 
  else
  {
    serv1 = 2;
  }
   if (BUTRIGHTstate == 1)
  {
    serv2 = 4;
  }
  else if (BUTLEFTstate == 1)
  {
    serv2 = 6;
  } 
  else
  {
    serv2 = 5;
  }
 Serial.print(serv1);
 Serial.print("\t");
 Serial.println(serv2);

 radio.write(&serv1, sizeof(serv1));
 radio.write(&serv2, sizeof(serv2));
delay(100);
}

How will you identify which int is which value?
You are aware of the fact that a transmission can fail?

Even Mentioned twice

I'm not ignoring you guys I just don't have have enough knowledge to implement a better code. I saw a lot of examples using struct or arrays but looks complicated for me. The way the code is now is really working. 2 button's from remote control one servo and the other 2 controls the other servo. Just like the code looks like.

My actual problem was in the "if, else if" section.

It will bite you somewhere,
don't be surprised when your servo commands get to the wrong servo.

struct SendStruct {
 int serv1;
 int serv2;
} sendStruct;
...
 sendStruct.serv1 = 1;
 sendStruct.serv2 = 9;

 err = radio.write(&sendStruct, sizeof(sendStruct));

does not seem complicated to me.

I don't understand what means "1" and "9" here

sendStruct.serv1 = 1;
 sendStruct.serv2 = 9;

And where you put the 3 points "..." Should be the if sentences?

I don't understand what 2 and 5 means there.

It was an example on how to access struct members.

The three dots are a placeholder for some arbitrary code.

The "2" will set first servo to 90° and the "5" will set second servo to 90°. You could see this in the code from my first post.

I made a Picavet which I'll tie on a kite. On the Picavet i will mount an GoPro and the 2 servos will rotate it to the direction i desire. So my project is not critical.

I opened this topic because my first Arduino should send to the second:

for serv1 the number 1, 2 or 3
and for serv2 the number 4, 5 or 6

everything was sent but de number 1 don't.

After I corrected my code as I posted above, now the number 1 is also showing up.

Yes it can be better written but right now is working and I'm fine with it.

I know very little from Arduino programming, and I'm doing now and then a project so I don't need do go soon deep in this.

For me is problem solved.

Anyway I really thank you for the intention to help me.

@Whandall and @J-M-L let me know how can I buy you guys a Bier :beers:.

I hope you mean that in German, and not English! :astonished:

The confusing thing is that I do not see how the original code could ever send serv2 with a value of 4.

Edit - I see now, the last if / else if are checking different variables.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.