Please modify this code for controlling Servo, data transmitted by nrf24l01

I want to use 4 DC Motors regulated by a joystick, a servo motor commanded by a push-button, and all data sent by an nrf24l01. This is a copy of my question on another group (guessed that group).

Transmitter code:

#include <RHReliableDatagram.h>
#include <RH_NRF24.h>
#include <SPI.h>

#define joyV A0
#define joyH A1
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2

int joyposV = 512;
int joyposH = 512;

RH_NRF24 RadioDriver;
RHReliableDatagram RadioManager(RadioDriver, CLIENT_ADDRESS);

uint8_t motorcontrol[3];
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];

void setup() {
  // put your setup code here, to run once:
  pinMode(3,INPUT);
  if(!RadioManager.init())
  motorcontrol[2] = 0;
}

void loop() {
  // put your main code here, to run repeatedly:
  joyposV = analogRead(joyV);
  joyposH = analogRead(joyH);

  if(joyposV < 460){
    motorcontrol[2] = 1;

    motorcontrol[0] = map(joyposV, 460, 0, 0, 255);
    motorcontrol[1] = map(joyposV, 460, 0, 0, 255);
  }
  else if(joyposV > 564){
    motorcontrol[2] = 0;

    motorcontrol[0] = map(joyposV, 564, 1023, 0, 255);
    motorcontrol[1] = map(joyposV, 564, 1023, 0, 255);
  }
  else{
    motorcontrol[0] = 0;
    motorcontrol[1] = 0;
    motorcontrol[2] = 0;
  }

  if(joyposH < 460){
    joyposH = map(joyposH, 460, 0, 0, 255);

    motorcontrol[0] = motorcontrol[0] - joyposH;
    motorcontrol[1] = motorcontrol[1] + joyposH;

    if(motorcontrol[0] < 0)motorcontrol[0] = 0;
    if(motorcontrol[1] > 255)motorcontrol[1] = 255;
  }
  else if(joyposH > 564){
    joyposH = map(joyposH, 564, 1023, 0, 255);

    motorcontrol[0] = motorcontrol[0] + joyposH;
    motorcontrol[1] = motorcontrol[1] - joyposH;

    if(motorcontrol[0] > 255)motorcontrol[0] = 255;
    if(motorcontrol[1] < 0)motorcontrol[1] = 0;
  }

  if(motorcontrol[0] < 8)motorcontrol[0] = 0;
  if(motorcontrol[1] < 8)motorcontrol[1] = 0;

  if(RadioManager.sendtoWait(motorcontrol, sizeof(motorcontrol), SERVER_ADDRESS)){
    uint8_t len = sizeof(buf);
    uint8_t from;
  }
  delay(50);
}

Receiver:

#include <RHReliableDatagram.h>
#include <RH_NRF24.h>
#include <SPI.h>

#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2

int enA = 9;
int in1 = 14;
int in2 = 4;

int enB = 5;
int in3 = 7;
int in4 = 6;

RH_NRF24 RadioDriver;

RHReliableDatagram RadioManager(RadioDriver, SERVER_ADDRESS);

uint8_t ReturnMessage[] = "JoyStick Data Received"; 

uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];

void setup() {
  // put your setup code here, to run once:
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(RadioManager.available()){
    uint8_t len = sizeof(buf);
    uint8_t from;
    if(RadioManager.recvfromAck(buf, &len,&from)){
      if(buf[2] == 1){
        digitalWrite(in1, LOW);
        digitalWrite(in2, HIGH);
        digitalWrite(in3, LOW);
        digitalWrite(in4, HIGH);
      }
      else{
        digitalWrite(in1, HIGH);
        digitalWrite(in2, LOW);
        digitalWrite(in3, HIGH);
        digitalWrite(in4, LOW);
      }
      analogWrite(enA, buf[1]);
      analogWrite(enB, buf[0]);
    }
  }
}

You forgot the question.

Is this supposed to be a paid job?

I'm making a robot for a competition. I made code so that I can run 4 motors by 1st motor driver and other to by 2nd motor driver. 1st set for going forward and backward and another set for steering and flipper. 1 motor for steering and other for the flipper.

Are these codes ok for my requirements?

Transmitter:

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

RF24 radio(8, 9); // CE, CSN

const byte address[6] = "00001";
char xyData[32] = "";
String xAxis, yAxis, button;

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

void loop() {
  xAxis = analogRead(A0); // Read Joysticks X-axis
  yAxis = analogRead(A1); // Read Joysticks Y-axis
  button = digitalRead(4);
  // X value
  xAxis.toCharArray(xyData, 5); // Put the String (X Value) into a character array
  radio.write(&xyData, sizeof(xyData)); // Send the array data (X value) to the other NRF24L01 modile
  // Y value
  yAxis.toCharArray(xyData, 5);
  radio.write(&xyData, sizeof(xyData));
  //push value
  button.toCharArray(xyData, 5);
  radio.write(&xyData, sizeof(xyData));
  delay(20);
}

Receiver:

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

#define enA1 2
#define in1 4
#define in2 5
#define enB1 3
#define in3 6
#define in4 7
#define enA2 0
#define in5 A0
#define in6 A1
#define enB2 1
#define in7 A2
#define in8 A3

RF24 radio(8, 9); // CE, CSN
const byte address[6] = "00001";
char receivedData[32] = "";
int  xAxis, yAxis, button;
int motorSpeedA = 0;
int motorSpeedB = 0;
int motorSpeedC = 0;
int motorSpeedD = 0;

void setup() {
  pinMode(enA1, OUTPUT);
  pinMode(enB1, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(enA2, OUTPUT);
  pinMode(enB2, OUTPUT);
  pinMode(in5, OUTPUT);
  pinMode(in6, OUTPUT);
  pinMode(in7, OUTPUT);
  pinMode(in8, OUTPUT);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}

void loop() {
  if (radio.available()) {   
    radio.read(&receivedData, sizeof(receivedData));
    xAxis = atoi(&receivedData[0]); 
    delay(10);
    radio.read(&receivedData, sizeof(receivedData));
    yAxis = atoi(&receivedData[0]);
    delay(10);
    radio.read(&receivedData, sizeof(receivedData));
    button = atoi(&receivedData[0]);
    delay(10);
  }
  
  if (yAxis < 470) {
    // Set Motor A backward
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    // Set Motor B backward
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    // Convert the declining Y-axis readings
    motorSpeedA = map(yAxis, 470, 0, 0, 255);
    motorSpeedB = map(yAxis, 470, 0, 0, 255);
  }
  else if (yAxis > 550) {
    // Set Motor A forward
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    // Set Motor B forward
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    // Convert the increasing Y-axis readings for
    motorSpeedA = map(yAxis, 550, 1023, 0, 255);
    motorSpeedB = map(yAxis, 550, 1023, 0, 255);
  }
  // If joystick stays in middle the motors are not moving
  else {
    motorSpeedA = 0;
    motorSpeedB = 0;
  }
  // X-axis used for left and right control
  if (xAxis < 470) {
    // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
    int xMapped = map(xAxis, 470, 0, 0, 255);

    // Set Motor C left
    analogWrite(in5, HIGH);
    analogWrite(in6, LOW);
    }
    
  if (xAxis > 550) {
    int xMapped = map(xAxis, 550, 1023, 0, 255);

    //Set Motor D right
    analogWrite(in5, LOW);
    analogWrite(in6, HIGH);
  }

  if (button = 0){
    analogWrite(in7, HIGH);
    analogWrite(in8, LOW);
  }
  else{
    analogWrite(in7, LOW);
    analogWrite(in8, HIGH);
  }
  analogWrite(enA1, motorSpeedA);
  analogWrite(enB1, motorSpeedB);
  analogWrite(enA2, motorSpeedC);
  analogWrite(enB2, motorSpeedD);
}

Do they work? If so, then they are ok for your project.

Paul

String xAxis, yAxis, button;
...
...
  xAxis = analogRead(A0); // Read Joysticks X-axis
  yAxis = analogRead(A1); // Read Joysticks Y-axis
  button = digitalRead(4);

Err...

Doesn't look like this is going to work indeed.

The receiver part also: no start/end tags in the data, meaning you have no reference as to what data you're actually receiving.

Is this OK now?

Transmitter:

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

RF24 radio(8, 9);//CE, CSN

const byte address[6] = "792GK3";
char xyData[32] = "";
String xAxis, yAxis, buttonState;
boolean button = 0;

void setup() {
  // put your setup code here, to run once:
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MAX);
  radio.stopListening();
}

void loop() {
  // put your main code here, to run repeatedly:
  xAxis = analogRead(A6);//Read Joystick X-axis
  yAxis = analogRead(A7);//Read Joystick Y-axis
  buttonState = digitalRead(button);//Read Button-state
  //X value
  xAxis.toCharArray(xyData, 5);
  radio.write(&xyData, sizeof(xyData));
  //Y value
  yAxis.toCharArray(xyData, 5);
  radio.write(&xyData, sizeof(xyData));
  //Button value
  buttonState.toCharArray(xyData, 5);
  radio.write(&xyData, sizeof(xyData));
}

Receiver:

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

#define enA1 7
#define in1 6
#define in2 5
#define in3 4
#define in4 3
#define enB1 2

#define enA2 10
#define in5 A0
#define in6 A1
#define in7 A2
#define in8 1
#define enB2 0

RF24 radio(8, 9); //CE, CSN
const byte address[6] = "792GK3";
char recievedData[32] = "";
int xAxis, yAxis, button;
int motorSpeedA = 0;
int motorSpeedB = 0;
int motorSpeedC = 0;
int motorSpeedD = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(enA1, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(enB1, OUTPUT);

  pinMode(enA2, OUTPUT);
  pinMode(in5, OUTPUT);
  pinMode(in6, OUTPUT);
  pinMode(in7, OUTPUT);
  pinMode(in8, OUTPUT);
  pinMode(enB2, OUTPUT);

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

void loop() {
  // put your main code here, to run repeatedly:
  if(radio.available()){
    radio.read(&recievedData, sizeof(recievedData));
    xAxis = atoi(&recievedData[0]);
    delay(10);
    radio.read(&recievedData, sizeof(recievedData));
    yAxis = atoi(&recievedData[0]);
    delay(10);
    radio.read(&recievedData, sizeof(recievedData));
    button = atoi(&recievedData[0]);
    delay(10);
  }
  if(yAxis < 470){
    //Set MotorA forward
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    //Set MotorB forward
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    //Convert declining Y-axis readings
    motorSpeedA = map(yAxis, 470, 0, 200, 255);
    motorSpeedB = map(yAxis, 470, 0, 200, 255);
  }
  else if(yAxis > 520
  ){
    //Set MotorA backward
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    //Set MotorB backward
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    //Convert declining Y-axis readings
    motorSpeedA = map(yAxis, 520, 1023, 200, 255);
    motorSpeedB = map(yAxis, 520, 1023, 200, 255);
  }
  else{
    motorSpeedA = 0;
    motorSpeedB = 0;
  }

  if(xAxis < 470){
    //Set the Direction left
    analogWrite(in5, HIGH);
    analogWrite(in6, LOW);
    //Convert declining X-axis readings
    motorSpeedC = map(xAxis, 470, 0, 0, 255);
  }
  else if(xAxis > 520){
    //Set the Direction right
    analogWrite(in5, LOW);
    analogWrite(in6, HIGH);
    //Convert declining X-axis readings
    motorSpeedC = map(xAxis, 520, 1023, 0, 255);
  }
  else{
    motorSpeedC = 0;
  }
  if(button == 1){
    analogWrite(in7, HIGH);
    digitalWrite(in8, LOW);
    motorSpeedD = button;
  }
  else{
    analogWrite(in7, LOW);
    digitalWrite(in8, HIGH);
    motorSpeedD = button;
  }
  analogWrite(enA1, motorSpeedA);
  analogWrite(enB1, motorSpeedB);
  analogWrite(enA2, motorSpeedC);
  analogWrite(enB2, motorSpeedD);
}

These do get verified in the Arduino IDE.

B1aziken:
Is this OK now?

Transmitter:

#include <SPI.h>

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

RF24 radio(8, 9);//CE, CSN

const byte address[6] = "792GK3";
char xyData[32] = "";
String xAxis, yAxis, buttonState;
boolean button = 0;

void setup() {
  // put your setup code here, to run once:
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MAX);
  radio.stopListening();
}

void loop() {
  // put your main code here, to run repeatedly:
  xAxis = analogRead(A6);//Read Joystick X-axis
  yAxis = analogRead(A7);//Read Joystick Y-axis
  buttonState = digitalRead(button);//Read Button-state
  //X value
  xAxis.toCharArray(xyData, 5);
  radio.write(&xyData, sizeof(xyData));
  //Y value
  yAxis.toCharArray(xyData, 5);
  radio.write(&xyData, sizeof(xyData));
  //Button value
  buttonState.toCharArray(xyData, 5);
  radio.write(&xyData, sizeof(xyData));
}




Receiver:



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

#define enA1 7
#define in1 6
#define in2 5
#define in3 4
#define in4 3
#define enB1 2

#define enA2 10
#define in5 A0
#define in6 A1
#define in7 A2
#define in8 1
#define enB2 0

RF24 radio(8, 9); //CE, CSN
const byte address[6] = "792GK3";
char recievedData[32] = "";
int xAxis, yAxis, button;
int motorSpeedA = 0;
int motorSpeedB = 0;
int motorSpeedC = 0;
int motorSpeedD = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(enA1, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(enB1, OUTPUT);

pinMode(enA2, OUTPUT);
  pinMode(in5, OUTPUT);
  pinMode(in6, OUTPUT);
  pinMode(in7, OUTPUT);
  pinMode(in8, OUTPUT);
  pinMode(enB2, OUTPUT);

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

void loop() {
  // put your main code here, to run repeatedly:
  if(radio.available()){
    radio.read(&recievedData, sizeof(recievedData));
    xAxis = atoi(&recievedData[0]);
    delay(10);
    radio.read(&recievedData, sizeof(recievedData));
    yAxis = atoi(&recievedData[0]);
    delay(10);
    radio.read(&recievedData, sizeof(recievedData));
    button = atoi(&recievedData[0]);
    delay(10);
  }
  if(yAxis < 470){
    //Set MotorA forward
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    //Set MotorB forward
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    //Convert declining Y-axis readings
    motorSpeedA = map(yAxis, 470, 0, 200, 255);
    motorSpeedB = map(yAxis, 470, 0, 200, 255);
  }
  else if(yAxis > 520
  ){
    //Set MotorA backward
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    //Set MotorB backward
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    //Convert declining Y-axis readings
    motorSpeedA = map(yAxis, 520, 1023, 200, 255);
    motorSpeedB = map(yAxis, 520, 1023, 200, 255);
  }
  else{
    motorSpeedA = 0;
    motorSpeedB = 0;
  }

if(xAxis < 470){
    //Set the Direction left
    analogWrite(in5, HIGH);
    analogWrite(in6, LOW);
    //Convert declining X-axis readings
    motorSpeedC = map(xAxis, 470, 0, 0, 255);
  }
  else if(xAxis > 520){
    //Set the Direction right
    analogWrite(in5, LOW);
    analogWrite(in6, HIGH);
    //Convert declining X-axis readings
    motorSpeedC = map(xAxis, 520, 1023, 0, 255);
  }
  else{
    motorSpeedC = 0;
  }
  if(button == 1){
    analogWrite(in7, HIGH);
    digitalWrite(in8, LOW);
    motorSpeedD = button;
  }
  else{
    analogWrite(in7, LOW);
    digitalWrite(in8, HIGH);
    motorSpeedD = button;
  }
  analogWrite(enA1, motorSpeedA);
  analogWrite(enB1, motorSpeedB);
  analogWrite(enA2, motorSpeedC);
  analogWrite(enB2, motorSpeedD);
}




These do get verified in the Arduino IDE.

don't you have the hardware parts to test the code or something ?

B1aziken:

String xAxis, yAxis, buttonState;

[...]

void loop() {
  xAxis = analogRead(A6);//Read Joystick X-axis
  yAxis = analogRead(A7);//Read Joystick Y-axis
  buttonState = digitalRead(button);//Read Button-state

[...]

It may compile, but no idea how this is going to end up.

Just don't use the String class. It's not needed. Everything can be done with c-strings without the memory problems.