Just looking for help on this code using nrf24l01

Very new to coding and have been pulling my hair out trying to figure out how to add a few features to this transmitter receiver code i found online. my problem is i want to have channel 9 to 11 to trigger a LOW or HIGH signals when their corresponding buttons are pressed on the transmitter instead of being set up to control servos. Again im very new to coding. here are the codes in question Use code tags to format code for the forum

TRANSMITTER CODE

#include <SPI.h>
  #include <nRF24L01.h>
  #include <RF24.h>
  
 
  
  const uint64_t pipeOut = 000322;         // NOTE: The same as in the receiver 000322 | Alıcı kodundaki adres ile aynı olmalı
  RF24 radio(9, 10);                       // select CE,CSN pin | CE ve CSN pinlerin seçimi

  struct Signal {
  byte throttle;
  byte pitch;
  byte roll;
  byte yaw;
  byte aux1;
  byte aux2;
  byte aux3;
  byte aux4;
  byte aux5;
  byte aux6;
  byte aux7;
};
  Signal data;
  void ResetData() 
{
  data.throttle = 0;
  data.pitch = 127;
  data.roll = 127;
  data.yaw = 127;
  data.aux1 = 0;                       // Signal lost position | Sinyal kesildiğindeki pozisyon
  data.aux2 = 0;
  data.aux3 = 0;
  data.aux4 = 0;
  data.aux5 = 0;
  data.aux6 = 0;
  data.aux7 = 0;
}
  void setup()
{ 
                                       //Configure the NRF24 module  | NRF24 modül konfigürasyonu
  radio.begin();
  radio.openWritingPipe(pipeOut);
  radio.setAutoAck(false);
  radio.setDataRate(RF24_250KBPS);    // The lowest data rate value for more stable communication  | Daha kararlı iletişim için en düşük veri hızı.
  radio.setPALevel(RF24_PA_MAX);      // Output power is set for maximum |  Çıkış gücü maksimum için ayarlanıyor.
  radio.stopListening();              // Start the radio comunication for Transmitter | Verici için sinyal iletişimini başlatır.
  ResetData();
 
}
                                      // Joystick center and its borders | Joystick merkez ve sınırları
  int Border_Map(int val, int lower, int middle, int upper, bool reverse)
{
  val = constrain(val, lower, upper);
  if ( val < middle )
  val = map(val, lower, middle, 0, 128);
  else
  val = map(val, middle, upper, 128, 255);
  return ( reverse ? 255 - val : val );
}
  void loop()
{


                                     // Control Stick Calibration for channels  |  Her bir kanal için kumanda Kol Kalibrasyonları 

  data.roll = Border_Map( analogRead(A3), 0, 512, 1023, true );        // "true" or "false" for signal direction | "true" veya "false" sinyal yönünü belirler
  data.pitch = Border_Map( analogRead(A2), 0, 512, 1023, true );      
  data.throttle = Border_Map( analogRead(A1),0, 512, 1023, false );  // For Single side ESC | Tek yönlü ESC için
  data.yaw = Border_Map( analogRead(A0), 0, 512, 1023, true );        
  
  data.aux1 = digitalRead(2);//forward 
  data.aux2 = digitalRead(3);
  data.aux3 = digitalRead(4);//eye
  data.aux4 = digitalRead(5);
  data.aux5 = digitalRead(6);//talk  
  data.aux6 = digitalRead(7);//reverse
  data.aux7 = digitalRead(8);//reverse
  
 
  radio.write(&data, sizeof(Signal));
}

RECEIVER CODE

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

int ch_width_1 = 0;
int ch_width_2 = 0;
int ch_width_3 = 0;
int ch_width_4 = 0;
int ch_width_5 = 0;
int ch_width_6 = 0;
int ch_width_7 = 0;
int ch_width_8 = 0;
int ch_width_9 = 0;
int ch_width_10 = 0;
int ch_width_11 = 0;

Servo ch1;
Servo ch2;
Servo ch3;
Servo ch4;
Servo ch5;
Servo ch6;
Servo ch7;
Servo ch8;
Servo ch9;
Servo ch10;
Servo ch11;

struct Signal {

byte throttle;
byte pitch;  
byte roll;
byte yaw;
byte aux1;
byte aux2;
byte aux3;
byte aux4;     
byte aux5;
byte aux6;
byte aux7;
};

Signal data;

const uint64_t pipeIn = 000322;
RF24 radio(9, 10); 

void ResetData()
{

data.throttle = 0;
data.roll = 127;
data.pitch = 127;
data.yaw = 127;
data.aux1 = 0;                                              // Define the inicial value of each data input. | Veri girişlerinin başlangıç değerleri
data.aux2 = 0;
data.aux3 = 0;
data.aux4 = 0;  
data.aux5 = 0;                                                
data.aux6 = 0;
data.aux7 = 0;
}

void setup()
{
                                                           // Set the pins for each PWM signal | Her bir PWM sinyal için pinler belirleniyor.
  ch1.attach(0);
  ch2.attach(2);
  ch3.attach(3);
  ch4.attach(4);
  ch5.attach(5);
  ch6.attach(6);
  ch7.attach(7);
  ch8.attach(8);
  ch9.attach(A3);
  ch10.attach(A4);
  ch11.attach(A5);

  ResetData();                                             // Configure the NRF24 module  | NRF24 Modül konfigürasyonu
  radio.begin();
  radio.openReadingPipe(1,pipeIn);
  radio.setAutoAck(false);
  radio.setDataRate(RF24_250KBPS);                          // The lowest data rate value for more stable communication  | Daha kararlı iletişim için en düşük veri hızı.
  radio.setPALevel(RF24_PA_MAX);                            // Output power is set for maximum |  Çıkış gücü maksimum için ayarlanıyor.
  radio.startListening();                                   // Start the radio comunication for receiver | Alıcı için sinyal iletişimini başlatır.

}

unsigned long lastRecvTime = 0;

void recvData()
{
while ( radio.available() ) {
radio.read(&data, sizeof(Signal));
lastRecvTime = millis();                                    // Receive the data | Data alınıyor
}
}

void loop()
{
recvData();
unsigned long now = millis();
if ( now - lastRecvTime > 1000 ) {
ResetData();                                                // Signal lost.. Reset data | Sinyal kayıpsa data resetleniyor
}


ch_width_1 = map(data.roll, 0, 255, 600, 2000);
ch_width_2 = map(data.pitch, 0, 255, 1200, 2000); 
ch_width_3 = map(data.throttle, 0, 255, 1000, 2000); 
ch_width_4 = map(data.yaw, 0, 255, 600, 2000); 
ch_width_5 = map(data.aux5, 1, 0, 1000, 2000); 
ch_width_6 = map(data.aux4, 1, 0, 800, 2300); 
ch_width_7 = map(data.aux3, 1, 0, 1200, 2000); 
ch_width_8 = map(data.aux2, 1, 0, 1350, 2000); 
ch_width_9 = map(data.aux1, 1, 0, 1000, 2000);                                             //FORWARD 
ch_width_10 = map(data.aux6, 0, 1, 1000, 2000);                                               //REVERSE
ch_width_11 = map(data.aux7,  0, 1, 1000, 2000);                                            //IRIS OPEN
                                               

ch1.writeMicroseconds(ch_width_1);                          // Write the PWM signal | PWM sinyaller çıkışlara gönderiliyor
ch2.writeMicroseconds(ch_width_2);
ch3.writeMicroseconds(ch_width_3);
ch4.writeMicroseconds(ch_width_4);
ch5.writeMicroseconds(ch_width_5);
ch6.writeMicroseconds(ch_width_6);                          
ch7.writeMicroseconds(ch_width_7);
ch8.writeMicroseconds(ch_width_8);
ch9.writeMicroseconds(ch_width_9);
ch10.writeMicroseconds(ch_width_10);
ch11.writeMicroseconds(ch_width_11);
}

Halt this project and start playing with more simple tasks and simple coding, and learn coding. It's better to learn walking before trying running.

1 Like

It looks like channel 9-11 are data.aux1, .aux6, and .aux7. From your transmitter, those are set by pins 2, 7,8 on the transmitter

  data.aux1 = digitalRead(2);//forward 
  data.aux6 = digitalRead(7);//reverse
  data.aux7 = digitalRead(8);//reverse

On the receiver side, you just need to use these values to set whatever pins you want to HIGH or LOW. Do not connect them to a servo or use the map() function to rescale the values

void setup()
{
                                                           // Set the pins for each PWM signal | Her bir PWM sinyal için pinler belirleniyor.
  ch1.attach(0);
  ch2.attach(2);
  ch3.attach(3);
  ch4.attach(4);
  ch5.attach(5);
  ch6.attach(6);
  ch7.attach(7);
  ch8.attach(8);
//  ch9.attach(A3);
//  ch10.attach(A4);
//  ch11.attach(A5);
  pinMode(A3, OUTPUT);
  pinMode(A4, OUTPUT);
  pinMode(A5, OUTPUT);
  ...
...
void loop()
{
recvData();
unsigned long now = millis();
if ( now - lastRecvTime > 1000 ) {
ResetData();                                                // Signal lost.. Reset data | Sinyal kayıpsa data resetleniyor
}


ch_width_1 = map(data.roll, 0, 255, 600, 2000);
ch_width_2 = map(data.pitch, 0, 255, 1200, 2000); 
ch_width_3 = map(data.throttle, 0, 255, 1000, 2000); 
ch_width_4 = map(data.yaw, 0, 255, 600, 2000); 
ch_width_5 = map(data.aux5, 1, 0, 1000, 2000); 
ch_width_6 = map(data.aux4, 1, 0, 800, 2300); 
ch_width_7 = map(data.aux3, 1, 0, 1200, 2000); 
ch_width_8 = map(data.aux2, 1, 0, 1350, 2000); 
//ch_width_9 = map(data.aux1, 1, 0, 1000, 2000);                                             //FORWARD 
//ch_width_10 = map(data.aux6, 0, 1, 1000, 2000);                                               //REVERSE
//ch_width_11 = map(data.aux7,  0, 1, 1000, 2000);   
ch_width_9 = data.aux1;                                             //FORWARD 
ch_width_10 = data.aux6;                                               //REVERSE
ch_width_11 = data.aux7;   
                                               

ch1.writeMicroseconds(ch_width_1);                          // Write the PWM signal | PWM sinyaller çıkışlara gönderiliyor
ch2.writeMicroseconds(ch_width_2);
ch3.writeMicroseconds(ch_width_3);
ch4.writeMicroseconds(ch_width_4);
ch5.writeMicroseconds(ch_width_5);
ch6.writeMicroseconds(ch_width_6);                          
ch7.writeMicroseconds(ch_width_7);
ch8.writeMicroseconds(ch_width_8);
//ch9.writeMicroseconds(ch_width_9);
//ch10.writeMicroseconds(ch_width_10);
//ch11.writeMicroseconds(ch_width_11);
digitalWrite(A3, ch_width_9);
digitalWrite(A4, ch_width_10);
digitalWrite(A5, ch_width_11);

}

Hint: Do not use the 3V3 pin of the Arduino, use an external power supply.

Below is some sample code, This was part of another project I did sometime ago where I made a electric scissor lift remote control, I changed it to suit all buttons but not had time to test it has I don't have any hardware set up for it.
It should give you some idea. I made it from this site which also should help you
https://electronoobs.com/eng_robotica_tut5_2.php

Tx code:

#include <Wire.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display

//########################################
// Define all the input/output pins      #
//########################################

#define Button_1 3 //forward signal from joystick
#define Button_2 4 //reverse signal from joystick
#define Button_3 5 //steer left input
#define Button_4 6 //sterr right input
#define Button_5 7 //left select drive default
#define Button_6 8
#define Button_7 A3
#define Button_8 1
#define Button_9 2
#define Button_10 9
#define Button_11 A2


unsigned long previousMillis = 0;        // will store last time LED was updated
const long interval = 150;
unsigned long Timer = 5000;
unsigned long Timer_currentMillis = 0;
int Input_1;
int Input_2;
int Input_3;
int Input_4;
int Input_5;
int Input_6;
int Input_7;
int Input_8;
int Input_9;
int Input_10;
int Input_11;

//########################################
// Set the transreiciver module          #
//########################################
RF24 radio(9, 10); // select  CSN  pin
const uint64_t pipeOut = 0xB3B4B5B6A3LL;//0xE8E8F0F0E1LL; //IMPORTANT: The same as in the receiver

// The sizeof this struct should not exceed 32 bytes
// This gives us up to 32 8 bits channals
struct MyData {
  byte B1_flag; //throttle signal
  byte B2_flag; //throttle signal
  byte B3_flag; //throttle signal
  byte B4_flag; //throttle signal
  byte B5_flag; //throttle signal
  byte B6_flag; //throttle signal
  byte B7_flag; //throttle signal
  byte B8_flag; //throttle signal
  byte B9_flag; //throttle signal
  byte B10_flag; //throttle signal
  byte B11_flag; //throttle signal


};

MyData data;

void resetData()
{
  //This are the start values of each channal
  // Throttle is 0 in order to stop the motors
  data.B1_flag = 0;
  data.B2_flag = 0;
  data.B3_flag = 0; //Steer left signal
  data.B4_flag = 0;
  data.B5_flag = 0;
  data.B6_flag = 0;
  data.B7_flag = 0;
  data.B8_flag = 0;
  data.B9_flag = 0;
  data.B10_flag = 0;
  data.B11_flag = 0;
}

void setup()
{
  Serial.begin(115200);
  lcd.init();
  lcd.backlight();
  pinMode(Button_1, INPUT_PULLUP);
  pinMode(Button_2, INPUT_PULLUP);
  pinMode(Button_3, INPUT_PULLUP);
  pinMode(Button_4, INPUT_PULLUP);
  pinMode(Button_5, INPUT_PULLUP);
  pinMode(Button_6, INPUT_PULLUP);
  pinMode(Button_7, INPUT_PULLUP);
  pinMode(Button_8, INPUT_PULLUP);
  pinMode(Button_9, INPUT_PULLUP);
  pinMode(Button_10, INPUT_PULLUP);
  pinMode(Button_11, INPUT_PULLUP);

  radio.begin();
  radio.setAutoAck(false);
  radio.setDataRate(RF24_250KBPS);
  radio.openWritingPipe(pipeOut);
  resetData();
  lcd.clear();
  
}


void loop()
{
 Input_1  = digitalRead(Button_1); //Steer left signal
 Input_2  = digitalRead(Button_2); //Steer left signal
 Input_3  = digitalRead(Button_3); //Steer left signal
 Input_4  = digitalRead(Button_4); //Steer left signal
 Input_5  = digitalRead(Button_5); //Steer left signal
 Input_6  = digitalRead(Button_6); //Steer left signal
 Input_7  = digitalRead(Button_7); //Steer left signal
 Input_8  = digitalRead(Button_8); //Steer left signal
 Input_9  = digitalRead(Button_9); //Steer left signal
 Input_10  = digitalRead(Button_10); //Steer left signal
 Input_11  = digitalRead(Button_11); //Steer left signal
  
  //############ Button 1 ######################
  if ( Input_1  == LOW) {
    data.B1_flag = 1;
  }
  else {
    data.B1_flag = 0;
  }
   //############ Button 2 ######################
  if ( Input_2  == LOW) {
    data.B2_flag = 1;
  }
  else {
    data.B2_flag = 0;
  }
   //############ Button 3 ######################
  if ( Input_3  == LOW) {
    data.B3_flag = 1;
  }
  else {
    data.B3_flag = 0;
  }
   //############ Button 4 ######################
  if ( Input_4  == LOW) {
    data.B4_flag = 1;
  }
  else {
    data.B4_flag = 0;
  }
   //############ Button 5 ######################
  if ( Input_5  == LOW) {
    data.B5_flag = 1;
  }
  else {
    data.B5_flag = 0;
  }
   //############ Button 6 ######################
  if ( Input_6  == LOW) {
    data.B6_flag = 1;
  }
  else {
    data.B6_flag = 0;
  }
   //############ Button 7 ######################
  if ( Input_7  == LOW) {
    data.B7_flag = 1;
  }
  else {
    data.B7_flag = 0;
  }
   //############ Button 8 ######################
  if ( Input_8  == LOW) {
    data.B8_flag = 1;
  }
  else {
    data.B8_flag = 0;
  }
   //############ Button 9 ######################
  if ( Input_9  == LOW) {
    data.B9_flag = 1;
  }
  else {
    data.B9_flag = 0;
  }
   //############ Button 10 ######################
  if ( Input_10  == LOW) {
    data.B10_flag = 1;
  }
  else {
    data.B10_flag = 0;
  }
   //############ Button 11 ######################
  if ( Input_11  == LOW) {
    data.B11_flag = 1;
  }
  else {
    data.B11_flag = 0;
  }
  
  radio.write(&data, sizeof(MyData));
}


RX Code:

#include <Wire.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display
//########################################
// Define all the input/output pins      #
//########################################
#define OutPut_1 3 //forward signal from joystick
#define OutPut_2 4 //reverse signal from joystick
#define OutPut_3 5 //steer left input
#define OutPut_4 6 //sterr right input
#define OutPut_5 7 //left select drive default
#define OutPut_6 8
#define OutPut_7 A3
#define OutPut_8 1
#define OutPut_9 2
#define OutPut_10 9
#define OutPut_11 A2                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
const uint64_t pipeIn =  0xB3B4B5B6A3LL;//0xE8E8F0F0E1LL; // Need to be the same has TX
RF24 radio(9, 10);
// The sizeof this struct should not exceed 32 bytes
// This gives us up to 32 8 bits channals
struct MyData {
  byte B1_flag; //throttle signal
  byte B2_flag; //throttle signal
  byte B3_flag; //throttle signal
  byte B4_flag; //throttle signal
  byte B5_flag; //throttle signal
  byte B6_flag; //throttle signal
  byte B7_flag; //throttle signal
  byte B8_flag; //throttle signal
  byte B9_flag; //throttle signal
  byte B10_flag; //throttle signal
  byte B11_flag; //throttle signal


};

MyData data;

void resetData() // Comes here is siganl is ;ost fail safe
{
  data.B1_flag = 0;
  data.B2_flag = 0;
  data.B3_flag = 0; //Steer left signal
  data.B4_flag = 0;
  data.B5_flag = 0;
  data.B6_flag = 0;
  data.B7_flag = 0;
  data.B8_flag = 0;
  data.B9_flag = 0;
  data.B10_flag = 0;
  data.B11_flag = 0;
}
/**************************************************/
void setup()
{
  pinMode(OutPut_1, OUTPUT); //setup the drive forward pin
  pinMode(OutPut_2, OUTPUT); //setup the drive forward pin
  pinMode(OutPut_3, OUTPUT); //setup the drive forward pin
  pinMode(OutPut_4, OUTPUT); //setup the drive forward pin
  pinMode(OutPut_5, OUTPUT); //setup the drive forward pin
  pinMode(OutPut_6, OUTPUT); //setup the drive forward pinMode
  pinMode(OutPut_7, OUTPUT); //setup the drive forward pin
  pinMode(OutPut_8, OUTPUT); //setup the drive forward pinMode
  pinMode(OutPut_9, OUTPUT); //setup the drive forward pin
  pinMode(OutPut_10, OUTPUT); //setup the drive forward pin
  pinMode(OutPut_11, OUTPUT); //setup the drive forward pinMode
  resetData(); //turn all ouputs off
  lcd.begin(20, 4);
  radio.begin();
  radio.setDataRate(RF24_250KBPS); // Both endpoints must have this set the same
  radio.setAutoAck(false);
  lcd.clear();
  radio.openReadingPipe(1, pipeIn);
  radio.startListening();
}
/**************************************************/
unsigned long lastRecvTime = 0;

void recvData()
{
  while ( radio.available() ) {
    radio.read(&data, sizeof(MyData));
    lastRecvTime = millis();
  }
}

/**************************************************/
void loop()
{
  recvData();
  unsigned long now = millis();
  if ( now - lastRecvTime > 500 ) { //if data stops coming turn everything off with a half second
    // signal lost?
    resetData();
  }
  //############ Button 1 ####################
  if (data.B1_flag == 1) { // if we recieve a 1 turn on machine
    digitalWrite(OutPut_1, HIGH);
    }
  else {
    digitalWrite(OutPut_1, LOW);
 }
 //############ Button 2 ####################
  if (data.B2_flag == 1) { // if we recieve a 1 turn on machine
    digitalWrite(OutPut_2, HIGH);
    }
  else {
    digitalWrite(OutPut_2, LOW);
 }
 //############ Button 3 ####################
  if (data.B3_flag == 1) { // if we recieve a 1 turn on machine
    digitalWrite(OutPut_3, HIGH);
    }
  else {
    digitalWrite(OutPut_3, LOW);
 }
 //############ Button 4 ####################
  if (data.B4_flag == 1) { // if we recieve a 1 turn on machine
    digitalWrite(OutPut_4, HIGH);
    }
  else {
    digitalWrite(OutPut_4, LOW);
 }
 //############ Button 5 ####################
  if (data.B5_flag == 1) { // if we recieve a 1 turn on machine
    digitalWrite(OutPut_5, HIGH);
    }
  else {
    digitalWrite(OutPut_5, LOW);
 }
 //############ Button 6 ####################
  if (data.B6_flag == 1) { // if we recieve a 1 turn on machine
    digitalWrite(OutPut_6, HIGH);
    }
  else {
    digitalWrite(OutPut_6, LOW);
 }
 //############ Button 7 ####################
  if (data.B7_flag == 1) { // if we recieve a 1 turn on machine
    digitalWrite(OutPut_7, HIGH);
    }
  else {
    digitalWrite(OutPut_7, LOW);
 }
 //############ Button 8 ####################
  if (data.B8_flag == 1) { // if we recieve a 1 turn on machine
    digitalWrite(OutPut_8, HIGH);
    }
  else {
    digitalWrite(OutPut_8, LOW);
 }
 //############ Button 9 ####################
  if (data.B9_flag == 1) { // if we recieve a 1 turn on machine
    digitalWrite(OutPut_9, HIGH);
    }
  else {
    digitalWrite(OutPut_9, LOW);
 }
 //############ Button 10 ####################
  if (data.B10_flag == 1) { // if we recieve a 1 turn on machine
    digitalWrite(OutPut_10, HIGH);
    }
  else {
    digitalWrite(OutPut_10, LOW);
 }
 //############ Button 11 ####################
  if (data.B11_flag == 1) { // if we recieve a 1 turn on machine
    digitalWrite(OutPut_11, HIGH);
    }
  else {
    digitalWrite(OutPut_11, LOW);
 }
}
/**************************************************/

I have used the arrays before for the buttons and outputs, it I thought it may make it easier for the OP to get an understanding and move up to the arrays next stage

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