nRF24L01 Help Please. How to control relays/leds with joystick and add 1 button

Here is my current code that I saw on the internet and it works and I am now able to control 2 servos. Now I want to add another button to control a relay and another joystick (A2,A3) to control relays/led but I don't know how to code that, please help me.

Transmitter Code

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

RF24 radio(7, 8);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int msg[1];
int potpin_1 = A0;
int val_1;
int potpin_2 = A1;
int val_2;


void setup(void) {
  pinMode(button, INPUT);
  radio.begin();
  radio.openWritingPipe(pipe);
}
void loop() {
  val_1 = analogRead(potpin_1), val_1 = map(val_1, 0, 1023, 0, 127), msg[0] = val_1, radio.write(msg, 1);
  val_2 = analogRead(potpin_2), val_2 = map(val_2, 0, 1023, 128, 255), msg[0] = val_2, radio.write(msg, 1);

}

Receiver Code

#include <Servo.h>
#include <SPI.h>
#include "RF24.h"

#define led 4
Servo servo1;
Servo servo2;
RF24 radio(7, 8);
const uint64_t pipe = 0xE8E8F0F0E1LL;

int msg[1];
int data;
int pos;

void setup()
{
  servo1.attach(3);
  servo2.attach(6);
  radio.begin();
  radio.openReadingPipe(1, pipe);
  radio.startListening();
}
void loop()
{
  if (radio.available())radio.read(msg, 1);
  if (msg[0] < 128 && msg[0] > -1)data = msg[0], pos = map(data, 0, 127, 7, 177), servo1.write(pos);
  if (msg[0] > 127 && msg[0] < 255)data = msg[0], pos = map(data, 128, 254, 9, 177), servo2.write(pos);

}

The button is for turning on the relay when i pressed it. And when I push the joystick UP, it turns relay1, then DOWN turns relay 2, relay 3 for LEFT and relay4 for RIGHT

Delta_G:
I don't even know how you want them to work or what you want them to do.

Did you not think that might be important information?

Every Direction of the Joystick controls 1 relay, a total of 4 relays on the joystick then another one for the button

I would create a structure that holds joystick and button data and send the whole structure in one packet.

I dislike your misuse of the comma operator very much.

  val_1 = analogRead(potpin_1), val_1 = map(val_1, 0, 1023, 0, 127), msg[0] = val_1, radio.write(msg, 1);
  val_2 = analogRead(potpin_2), val_2 = map(val_2, 0, 1023, 128, 255), msg[0] = val_2, radio.write(msg, 1);
  if (msg[0] < 128 && msg[0] > -1)data = msg[0], pos = map(data, 0, 127, 7, 177), servo1.write(pos);
  if (msg[0] > 127 && msg[0] < 255)data = msg[0], pos = map(data, 128, 254, 9, 177), servo2.write(pos);

That only obfuscates and does not even spare characters to type.

The examples in this Simple nRF24L01+ Tutorial may help. Some of them send multiple values.

...R

Whandall:
I would create a structure that holds joystick and button data and send the whole structure in one packet.

I dislike your misuse of the comma operator very much.

  val_1 = analogRead(potpin_1), val_1 = map(val_1, 0, 1023, 0, 127), msg[0] = val_1, radio.write(msg, 1);

val_2 = analogRead(potpin_2), val_2 = map(val_2, 0, 1023, 128, 255), msg[0] = val_2, radio.write(msg, 1);





if (msg[0] < 128 && msg[0] > -1)data = msg[0], pos = map(data, 0, 127, 7, 177), servo1.write(pos);
  if (msg[0] > 127 && msg[0] < 255)data = msg[0], pos = map(data, 128, 254, 9, 177), servo2.write(pos);



That only obfuscates and does not even spare characters to type.

Would you mind if you post the code here? Thanks

A sending sketch:

#include <RF24.h>

const uint8_t button = 2;
const uint8_t potpin_1 = A0;
const uint8_t potpin_2 = A1;
const uint8_t pipe[] = "apipe";
const uint16_t maxGapBetweenPackets = 100;

struct Packet {
  uint16_t pot1;
  uint16_t pot2;
  uint8_t key;
  bool getValues();
} data;

RF24 radio(7, 8);

void setup(void) {
  pinMode(button, INPUT_PULLUP);
  radio.begin();
  radio.openWritingPipe(pipe);
}

void loop() {
  static uint32_t lastPacket;
  uint32_t topLoop = millis();
  if (data.getValues() || topLoop - lastPacket >= maxGapBetweenPackets) {
    radio.write(&data, sizeof(data));
    lastPacket = topLoop;
  }
}

bool Packet::getValues() {
  bool same = true;
  uint16_t val = analogRead(potpin_1);
  if (val != pot1) {
    same = false;
    pot1 = val;
  }
  val = analogRead(potpin_2);
  if (val != pot2) {
    same = false;
    pot2 = val;
  }
  val = digitalRead(button);
  if (val != key) {
    same = false;
    key = val;
  }
  return !same;
}

Whandall:
A sending sketch:

#include <RF24.h>

const uint8_t button = 2;
const uint8_t potpin_1 = A0;
const uint8_t potpin_2 = A1;
const uint8_t pipe[] = "apipe";
const uint16_t maxGapBetweenPackets = 100;

struct Packet {
  uint16_t pot1;
  uint16_t pot2;
  uint8_t key;
  bool getValues();
} data;

RF24 radio(7, 8);

void setup(void) {
  pinMode(button, INPUT_PULLUP);
  radio.begin();
  radio.openWritingPipe(pipe);
}

void loop() {
  static uint32_t lastPacket;
  uint32_t topLoop = millis();
  if (data.getValues() || topLoop - lastPacket >= maxGapBetweenPackets) {
    radio.write(&data, sizeof(data));
    lastPacket = topLoop;
  }
}

bool Packet::getValues() {
  bool same = true;
  uint16_t val = analogRead(potpin_1);
  if (val != pot1) {
    same = false;
    pot1 = val;
  }
  val = analogRead(potpin_2);
  if (val != pot2) {
    same = false;
    pot2 = val;
  }
  val = digitalRead(button);
  if (val != key) {
    same = false;
    key = val;
  }
  return !same;
}

how about the receiver code?

How about using your own brain?

ibenzzz:
how about the receiver code?

My tutorial has 3 sets of sending and receiving programs. Have you tried any of them?

...R

I have these 4 working codes for nrf24l01. 2 transmitter codes and 2 receiver codes. But I want to combine them but when I do, they do not work. Please help.

Combined Code RX

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include <Servo.h>
Servo FTHROWER;
Servo BLDC;
int DataMsg[1];

RF24 radio(7,8);
const uint64_t pipe = 0xE14BC8F482LL;
int Relay1 = 2;
int Relay2 = 3;
int data;
int pos;

void setup()
{
  FTHROWER.attach(9);
  BLDC.attach(10);
  pinMode(Relay1, OUTPUT);
  pinMode(Relay2, OUTPUT);
  Serial.begin(57600);
  radio.begin();
  radio.openReadingPipe(1,pipe);
  radio.startListening();
  digitalWrite(Relay1, HIGH);
  digitalWrite(Relay2, HIGH);
}

void loop()
{
  if (radio.available())
  {
    bool done = false;    
    while (!done)
    {
radio.read(DataMsg, 1);
      Serial.print("NRF24L01 Receiver: ");    
      Serial.println(DataMsg[0]);
      
      if (DataMsg[0] == 254)
      {
        delay(10);
        digitalWrite(Relay1, LOW);
        digitalWrite(Relay2, HIGH );
      }

      if (DataMsg[0] == 255)
      {
        delay(10);
        digitalWrite(Relay2, LOW );
        digitalWrite(Relay1, HIGH );
      }
      delay(100);
    }
  }
  else
  {
  Serial.println("Waiting for signal...");
  }
radio.read(msg, 1);
if (DataMsg[0] <128 && DataMsg[0] >-1)data = DataMsg[0], pos = map(data, 0, 127, 7, 177),FTHROWER.write(pos);
if (DataMsg[0] >127 && DataMsg[0] <250)data = DataMsg[0], pos = map(data, 128, 250, 9, 177),BLDC.write(pos);

}

Combined Code TX

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

int DataMsg[1];
int potpin_1 = A0;
int val_1;
int potpin_2 = A1;
int val_2;

RF24 radio(7, 8);

const uint64_t pipe = 0xE14BC8F482LL;

int RVS = 4;
int FWARD = 5;
int LEFT = 2;
int RIGHT = 3;
int FTHROW = 6;
void setup()
{
  //ativa pull-up
  pinMode(5, INPUT);
  digitalWrite(5, HIGH);
  pinMode(2, INPUT);
  digitalWrite(2, HIGH);
  pinMode(4, INPUT);
  digitalWrite(4, HIGH);
  pinMode(3, INPUT);
  digitalWrite(3, HIGH);
  pinMode(6, INPUT);
  digitalWrite(6, HIGH);


  Serial.begin(57600);
  Serial.println("NRF24L01 Transmitter");

  radio.begin();
  radio.openWritingPipe(pipe);
}

void loop()
{
  if (digitalRead(RIGHT) == LOW)
  {
    Serial.println("RIGHT pressed");
DataMsg[0] = -2;
    radio.write(DataMsg, 1);
  }
  if (digitalRead(LEFT) == LOW)
  {
    Serial.println("LEFT Button pressed");
    DataMsg[0] = 2559;
    radio.write(DataMsg, 1);
  }
  if (digitalRead(FWARD) == LOW)
  {
    Serial.println("FORWARD pressed");
    DataMsg[0] = -3;
    radio.write(DataMsg, 1);
  }
  if (digitalRead(RVS) == LOW)
  {
    Serial.println("REVERSE pressed");
    DataMsg[0] = -5;
    radio.write(DataMsg, 1);
  }
  if (digitalRead(FTHROW) == LOW)
  {
    Serial.println("FTHROWER pressed");
    DataMsg[0] = -4;
    radio.write(DataMsg, 1);
  }
  val_1 = analogRead(potpin_1), val_1 = map(val_1, 0, 1023, 0, 127), DataMsg[0] = val_1, radio.write(DataMsg, 1);
  val_2 = analogRead(potpin_2), val_2 = map(val_2, 0, 1023, 128, 250), DataMsg[0] = val_2, radio.write(DataMsg, 1);
}

The negative values send data to the receiver from 251 - 255. When I only use this:

  val_1 = analogRead(potpin_1), val_1 = map(val_1, 0, 1023, 0, 127), DataMsg[0] = val_1, radio.write(DataMsg, 1);
  val_2 = analogRead(potpin_2), val_2 = map(val_2, 0, 1023, 128, 250), DataMsg[0] = val_2, radio.write(DataMsg, 1);

It works well with controlling the servos but when I combine them with the working buttons code, only the buttons work and I lose control of the servos

Don't double Post. I have suggested to the Moderator to merge your Threads

...R

Robin2:
My tutorial has 3 sets of sending and receiving programs. Have you tried any of them?

...R

I can't find you tutorial. May i have the link?

Simple rf24 tutorial by Robin2.

Whandall:
How about using your own brain?

+1

ibenzzz:
I can't find you tutorial. May i have the link?

I gave it to you in Reply #5 - it is very frustrating to give advice and have it completely ignored.

...R

@ibenzzz, please do not cross-post. Threads merged.