Has anyone used nrf24L01, H-Bridge, Servo code together?

Servo code used simultaneously with H-bridge code with nrf24L01 inhibits communication to the second device in the code. If Servo is first it works. If H-bride is first it works but not both. Has anyone had success using these together?

it probably would help if you were to post links to the libraries you used and more documentation about your set up. A Minimal, Reproducible Example would go a long way...

// SERVER
#include <nRF24L01.h>
#include <RF24.h>
#include<SPI.h>
RF24 radio(9,10);

const byte address[6] = "00001";
const int threshold = 20;

#define pot1 A0 
int potValue1 = 0;
int pwmValue1 = 0;
int check1 = 0;
// Packet Length: payload of up to 32 bytes at a time (8 byte payload much higher success rate)
const char var1[8] = "motor1";//began at "32"

#define horn A1
int hornValue2 = 0;
int pwmValue2 = 0;
int check2 = 0;
const char var2[8] = "motor2";//began at "32"

#define pot2 A2
int potValue2 = 0;
int angleValue = 0;
int check3 = 0; 
const char var3[8] = "servo3";

void setup() 
{
Serial.begin(9600);

radio.begin();
radio.openWritingPipe(address);
radio.setChannel(100);        //Channels (0 -125) 1Mhz wide start at 2400Mhz-2525Mhz
radio.setDataRate(RF24_250KBPS); //nRF24 supports 250kbps, 1Mbs,2Mbps (250kbps most error free)
radio.setPALevel(RF24_PA_MAX);
radio.stopListening();
}

void loop() 
{
potValue1 = analogRead(pot1);
if(potValue1 > check1 + threshold || potValue1 < check1 - threshold)
{
  radio.write(&var1, sizeof(var1));
  pwmValue1 = map(potValue1, 0, 1023, 0, 255);
  radio.write(&pwmValue1, sizeof(pwmValue1));
  check1 = potValue1;
  Serial.println(var1);//Jay added
Serial.println("INPUT:1");
Serial.print("PWM Value:");
Serial.println(pwmValue1);
Serial.print("Voltage Level:");
Serial.println(potValue1);
Serial.println("----------------------------------");
}
hornValue2 = analogRead(horn);
if(hornValue2 > check2 + threshold || hornValue2 < check2 - threshold)
{
  radio.write(&var2, sizeof(var2));
  pwmValue2 = map(hornValue2, 0, 1023, 0, 255);
  radio.write(&pwmValue2, sizeof(pwmValue2));
  check2 = hornValue2;
Serial.println("INPUT:2");
Serial.print("PWM Value:");
Serial.println(pwmValue2);
Serial.print("Voltage Level:");
Serial.println(hornValue2);
Serial.println("----------------------------------");
}
potValue2 = analogRead(pot2);
if(potValue2 > check3 + threshold || potValue2 < check3 - threshold)
{
  radio.write(&var3, sizeof(var3));
  angleValue =map(potValue2, 0, 1023, 0, 180);
  radio.write(&angleValue, sizeof(angleValue));
  check3 = potValue2;
Serial.println("INPUT:Wave");
Serial.print("Angle Value:");
Serial.println(angleValue);
Serial.print("Voltage Level:");
Serial.println(potValue2);
Serial.println("----------------------------------");
}
}
// CLIENT Code
#include <RF24.h>
#include<SPI.h>
#include<Servo.h>
RF24 radio(9,10);
const byte address[6] = "00001";

Servo servo2;//For Man waving
int angle2 = 0;
char input[32] = "";//???????
const char var3[8]="Servo2";

int in1 = 5;
int in2 = 3;
int PWM1;
int MotorSpeed1 = 0;
int MotorSpeed2 = 0;
// Packet Length: payload of up to 32 bytes at a time (8 byte payload much higher success rate)
//char input[8] = "";                 //began at "32"
const char var1[8] = "motor1";      //began at "32"

//////////// Set up Train Horn
int horn = 7;
int PWM2 = 0;
const char var2[8] = "motor2";

void setup() 
{
 Serial.begin(9600); 
 servo2.attach(2);//attach to digital Pin 2
 
pinMode(in1,OUTPUT);//H bridge
pinMode(in2,OUTPUT);//H Bridge

pinMode(horn,OUTPUT);//Set uo Train Horn

radio.begin();
radio.openReadingPipe(0, address);
radio.setChannel(100);        //Channels (0 -125) 1Mhz wide start at 2400Mhz-2525Mhz
radio.setDataRate(RF24_250KBPS);//(RF24_250KBPS); //nRF24 supports 250kbps, 1Mbs,2Mbps (250kbps most error free)
radio.setPALevel(RF24_PA_MAX);
radio.startListening();
}

void loop() 
{
delay(5);
while(!radio.available());
radio.read(&input, sizeof(input));

//MOTOR CONTROL
if((strcmp(input,var1) == 0))
  {
   
while(!radio.available());
radio.read(&PWM1, sizeof(PWM1));
  if (PWM1 < 125){
   MotorSpeed1 = map(PWM1, 124,0, 0, 255);//Limit Reverse? by using (80, 0, 0, 255)?
   analogWrite(in1, MotorSpeed1);
   digitalWrite(in2, LOW);
}
  else if (PWM1 >125){
   MotorSpeed2 = map(PWM1, 125,255, 0, 255);                              
   analogWrite(in2, MotorSpeed2);
   digitalWrite(in1,LOW);
 }
Serial.println(input);
Serial.print("PWM1 Value:");
Serial.println(PWM1);
Serial.println("--------------------------------");
Serial.println("MotorSpeed1");
Serial.println(MotorSpeed1);
Serial.println("MotorSpeed2");
Serial.println(MotorSpeed2);
}
//Set up Train Horn 
else if((strcmp(input,var2) == 0))
  {
while(!radio.available());
radio.read(&PWM2, sizeof(PWM2));
analogWrite(horn,PWM2);
Serial.println(input);
Serial.print("PWM Value:");
Serial.println(PWM2);
Serial.println("--------------------------------");
 }
//SERVO CONTROL  
  else if((strcmp(input,var3) == 0))
 {
while(!radio.available());
radio.read(&input, sizeof(input));

while(!radio.available());
radio.read(&angle2, sizeof(angle2));
servo2.write(angle2);
Serial.println(input);
Serial.print("Angle:");
Serial.println(angle2);
Serial.println("--------------------------------");
}
}

Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation of your ask).

--

Have you considered grouping your payload into one message instead of doing two radio.write() ?

fixed format above rather than a new post. please post more details about the setup

Do yourself a favor, put all data in a struct, so you only have a single type of packet.

Don't wait for packets, process them when they are available.

Whandall, I'm a hardware guy struggling with the software. I don't know what you mean. Can you show me what you mean?
Thanks, Jay

Hi, @anon70430984
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

Can you please post a circuit diagram including power supply?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

unfortunately I don't have a circuit diagram, I build it from the code and the rest from basic electronics. I'm heading out of town for a week maybe after that I could draw it out.

// in both sketches, copied or included

struct Packet {
 int pwmValue1;
 int pwmValue2;
 int angleValue;

};

 // in the sender
Packet txPack;

 if (timeToSend) {
   // fill packet with new data before sending it
   result = radio.write(&txPack, sizeof(pack);
 }

 // in the receiver
 Packet rxPack;

 if (radio.available()) {
   radio.read(&rxPack, sizeof(pack));
   // process new received data here
 }

Thank you Whandall !! I'll have to work with this when I get back.

It's much faster (6x) and you don't have to sort out any packet.

enableDynamicPayloads gives additional speedup
by reducing the data size from 32 to 6.

whandall

I have the following sketches communicating but I can't get the Pot data sent and I can't get the Servo written into the sketch without an error. Successfully communication but zero data.
`//Transmitter
#include <SPI.h>
#include "RF24.h";
RF24 radio(9,10);

struct package
{
int potValue =A0;
int angleValue = 0;
};
byte adresses[6] ={"0"};
typedef struct package Package;
Package data;

void setup() {
Serial.begin(9600);
radio.begin();
radio.setRetries(15, 15);
radio.setChannel(100);
radio.setPALevel(RF24_PA_LOW);
radio.setDataRate(RF24_250KBPS);
radio.enableAckPayload();
radio.enableDynamicPayloads();
radio.openWritingPipe(adresses[1]);
radio.openReadingPipe(1,adresses[0]);
delay(1000);
}

void loop() {
data.potValue = analogRead(9);
delay(20);
if(radio.write(&data, sizeof(data)))
{
Serial.println(" transmitted successfully");
if(!radio.available()){ // If nothing in the buffer, we got an ack but it is blank
Serial.println("Got blank response");
}

  else{      
        while(radio.available() ){                  // If an ack with payload was received
            radio.read(&data, sizeof(data));  
            Serial.print("Ack payload is: ");
            Serial.print(data.angleValue);
            }
    }

}
else {
Serial.println("Failed Sending");
}
delay(50);
}`

t//Receiver 
#include <SPI.h>
#include <Servo.h>
#include "RF24.h";
RF24 radio(9,10);

Servo servo;

struct package
{
 int potValue = A0;
 int angleValue = 0;
};
byte adresses[][6] ={"0"};
typedef struct package Package;
Package data;

void setup() {
  Serial.begin(9600);
  
  servo.attach(8);
  
  radio.begin();
  radio.setRetries(15, 15);
  radio.setChannel(100);
  radio.setPALevel(RF24_PA_LOW);
  radio.setDataRate(RF24_250KBPS);
  radio.enableAckPayload();           
  radio.enableDynamicPayloads(); 
  radio.openWritingPipe(adresses[0]);
  radio.openReadingPipe(1,adresses[1]);
  radio.startListening();
  delay(1000);
}

void loop() {
  data.angleValue = analogRead(9);
  delay(20);
  
  while( radio.available()){              
      radio.read(&data,sizeof(data));                              
      Serial.println("Received potValue:");
      Serial.print(data.potValue);
      radio.writeAckPayload(1,&data,sizeof(data));  
     }
    delay(50);
 }ype or paste code here

I give up on this topic as it seems you can't post code with code tags correctly. It's just unreadable.

Whandall
Looks like I miss pasted! Resend

t//Transmitter 
#include <SPI.h>
#include "RF24.h";
RF24 radio(9,10);

struct package
{
 int potValue =A0;
 int angleValue = 0;
};
byte adresses[][6] ={"0"};
typedef struct package Package;
Package data;

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.setRetries(15, 15);
  radio.setChannel(100);
  radio.setPALevel(RF24_PA_LOW);
  radio.setDataRate(RF24_250KBPS);
  radio.enableAckPayload();           
  radio.enableDynamicPayloads(); 
  radio.openWritingPipe(adresses[1]);
  radio.openReadingPipe(1,adresses[0]);
    delay(1000);
}

void loop() {
  data.potValue = analogRead(9);
  delay(20);
  if(radio.write(&data, sizeof(data)))
  {
      Serial.println("  transmitted successfully");
      if(!radio.available()){         // If nothing in the buffer, we got an ack but it is blank
      Serial.println("Got blank response");
        }
        
      else{      
            while(radio.available() ){                  // If an ack with payload was received
                radio.read(&data, sizeof(data));  
                Serial.print("Ack payload is: ");
                Serial.print(data.angleValue);
                }
        }
  }
  else {
    Serial.println("Failed Sending");
  }
   delay(50);
  }ype or paste code here
t//Receiver 
#include <SPI.h>
#include <Servo.h>
#include "RF24.h";
RF24 radio(9,10);

Servo servo;

struct package
{
 int potValue = A0;
 int angleValue = 0;
};
byte adresses[][6] ={"0"};
typedef struct package Package;
Package data;

void setup() {
  Serial.begin(9600);
  
  servo.attach(8);
  
  radio.begin();
  radio.setRetries(15, 15);
  radio.setChannel(100);
  radio.setPALevel(RF24_PA_LOW);
  radio.setDataRate(RF24_250KBPS);
  radio.enableAckPayload();           
  radio.enableDynamicPayloads(); 
  radio.openWritingPipe(adresses[0]);
  radio.openReadingPipe(1,adresses[1]);
  radio.startListening();
  delay(1000);
}

void loop() {
  data.angleValue = analogRead(9);
  delay(20);
  
  while( radio.available()){              
      radio.read(&data,sizeof(data));                              
      Serial.println("Received potValue:");
      Serial.print(data.potValue);
      radio.writeAckPayload(1,&data,sizeof(data));  
     }
    delay(50);
 }ype or paste code here

Sorry!! This is day two of using this site. Trying hard for a 74 year old man!

OK I went and got another cup of coffee. It helped. I now have pot data sent to receiver!

The initialization is crap, structs need no typedef,
addresses should not contain longer sequences of binary ones or zeros.

struct Package {
 int potValue;
 int angleValue;
};
byte adresses[6] ={"0clue"};

radio.openReadingPipe(1,adresses[1]);

There is only one address.

delay(1000); Why?

data.angleValue = analogRead(9);

Your pot is connected to A9 of your Mega?

delay(20); Why?

Why is there no data for angleValue?

delay(50); Why?

There is no preload of Ack data in the receiver.

And returning the same data as was sent into the same variables makes it quite hard to debug.