nRF + motor + button trouble

Hello ! Hi, I have a problem, I don't know how to write correctly the program that will be servo using the button through the communication nrf. My goal is a situation in which one button will increase the speed (as we hold it), and the second one will reduce speed up to zero with just one click. Now i try to write program with one button and its work like: when i put battery, motor just spin on high position, buttons doesnt work. Can someone look at this and try help me ? Thanks, I am very grateful
Tx code:

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

RF24 radio(7, 8); // CE, CSN
const byte addresses[][6] = {"00001", "00002"};
boolean buttonState = 0;

void setup() {
  pinMode(3, INPUT);
  radio.begin();
  radio.openWritingPipe(addresses[1]); // 00002
  radio.openReadingPipe(1, addresses[0]); // 00001
  radio.setPALevel(RF24_PA_MIN);
}

void loop() {
  delay(5);
  radio.startListening();
  if (radio.available()){
    while (radio.available()){
      
      }
    delay(5);
    radio.stopListening();
buttonState = digitalRead(3);
radio.write(&buttonState, sizeof(buttonState));
  }
}

Rx code:

#include <SPI.h>
#include <RF24.h>
#include <Servo.h>
#include <nRF24L01.h>
RF24 radio(7, 8); // CE, CSN
const byte addresses[][6] = {"00001", "00002"};
boolean buttonState = 0;
Servo servo;

void setup() {
servo.attach(6);
radio.begin();
radio.openWritingPipe(addresses[0]); // 00001
radio.openReadingPipe(1, addresses[1]); // 00002
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_LOW);
radio.stopListening();
}

void loop() {
radio.startListening();
while (!radio.available());
radio.read(&buttonState, sizeof(buttonState));
if (buttonState == HIGH){
  servo.write(150);
  servo.write(70);
  delay(15);
  }
  else{
    servo.write(0);
    }
 }
    servo.write(150);
    servo.write(70);

This will be a problem. The servo will not move to 150 because it is immediately commanded to go to 70. The program will not wait until it gets to 150 before moving it to 70

    while (radio.available()){
     
      }

If there ever is data available from the radio, you'll be stuck in this infinite loop, because you never actually read the data.

if (buttonState == HIGH){
  servo.write(150);
  servo.write(70);
  delay(15);
  }

The result of this will be that the servo jerks to position 70 from wherever it is. It will never get to position 150.

There is nothing in your code that causes the servos to move anything other than flat out.

Forget about the radios for now. Connect everything to ONE Arduino. Get the servo acting the way you want on one Arduino. Then, you'll have a much better idea what one Arduino needs to tell the other.

In the end, you don't want to spam the receiver with "the button is pushed...the button is pushed...the button is pushed...".

Have a look at this Simple nRF24L01+ Tutorial.

Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.

The examples are as simple as I could make them and they have worked for other Forum members. If you get stuck it will be easier to help with code that I am familiar with. Start by getting the first example to work

...R

Thank u for reply :slight_smile: Actually with one board Arduino, i have perfectly working code for me (paste below).
Now i have to take tutorial again.

#include<Servo.h>
int x = 0;

Servo servo;
void setup() {
  pinMode(3, INPUT);
  pinMode(5, INPUT);
  servo.attach(6);
}
void loop() {
  while (digitalRead(5) == HIGH && x < 180) {
    x++;
    servo.write(x);
    
  }
  while (digitalRead(3) == HIGH && x > 0) {
    x--;
    servo.write(x);
   
  }
}

Actually with one board Arduino, i have perfectly working code

But it is not doing what the original code does, is it ?

Where is the

if (buttonState == HIGH){
  servo.write(150);
  servo.write(70);
  delay(15);
  }

No, its other code. In first code i just trying to send information that button is pressed then motor start spinning. Code in last post is final program that i want to send by nrf but dont know how. Even first code doesnt work properly. But i try delete and rewrite some points of first code, like someone before told me in this topic.

EDIT: Okey i got the working code for 1 button(when it's pushed, motor accelerate)
Tx

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

int msg[1];
RF24 radio(7,8);

const uint64_t pipe = 0xF0F0F0F0D2L;
int button = 3;
int buttonState = 0;

void setup(void)
{
 Serial.begin(9600);
 radio.begin();
 radio.openWritingPipe(pipe);
 pinMode(button, INPUT); 
}
void loop(void)
{
 buttonState1 = digitalRead(button);
 if (buttonState1 == HIGH)
 {
   msg[0] = 212;
   radio.write(msg, 1);
 }
}

Rx

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>      
Servo servo;   
int x = 0;
int msg[1];
RF24 radio(7,8);
const uint64_t pipe = 0xF0F0F0F0D2L;
void setup(void)
{
 myServo.attach(6);
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(1, pipe);
  radio.startListening();
  myServo.write(0);      
}
void loop(void)
{
  if(radio.available()){ 
      radio.read(msg, 1);
      Serial.println(msg[0]);
      if (msg[0] == 212) {
        x++;
       myServo.write(x); 
      }
      else {   
            myServo.write(0); 
      }
    }
  }

Now, its time to make second button, when pushed once time, motor slowing down to 0.

Code in last post is final program that i want to send by nrf but dont know how.

You don't send a program you send data for the receiving program to use.

All you need to send is one byte that is either 0 or 1.

On the Tx side, if pin 5 is HIGH send a 0, else if pin 3 is HIGH send a 1 else don't send anything

On the Rx side if data is available you read it. If its value is 0, increment the servo position else if its value is 1, decrement the servo position

froz3npk:
Thank u for reply :slight_smile: Actually with one board Arduino, i have perfectly working code

It is impossible to know if wireless communication is working with a single Arduino - there must be a transmitter and a receiver.

...R