Combining Motor Sketch with nRF24L01s and Joystick

I have a sketch (first one) that runs two motors with a L298N and a joystick. This works on a breadboard perfectly like I want it.

However, I want to separate the joystick from the motors (the vehicle) with nRF24L01s.

I have two more sketches, which work, transmit and receive, that run a servo back and forth with a joystick. But being a newb to programming, I cannot figure out how to make the DC motor sketch with the wireless joystick of the 2nd one.

I can get the joystick readings on a receiver sketch, but I guess I'm not sure what to do with them.

Thank you, steve

/*  L298N Motor Control Demonstration with Joystick
  L298N-Motor-Control-Demo-Joystick.ino
  Demonstrates use of Joystick control with Arduino and L298N Motor Controller

  DroneBot Workshop 2017
  http://dronebotworkshop.com
*/

// Motor A

int enA = 6;
int in1 = 8;
int in2 = 7;

// Motor B

int enB = 3;
int in3 = 5;
int in4 = 4;

// Joystick Input

int joyVert = A0; // Vertical
int joyHorz = A1; // Horizontal

// Motor Speed Values - Start at zero

int MotorSpeed1 = 0;
int MotorSpeed2 = 0;

// Joystick Values - Start at 512 (middle position)

int joyposVert = 512;
int joyposHorz = 512;


void setup()

{
  Serial.begin(9600);


  // Set all the motor control pins to outputs

  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  // Start with motors disabled and direction forward

  // Motor A

  digitalWrite(enA, LOW);
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);

  // Motor B

  digitalWrite(enB, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);

}

void loop() {

  // Read the Joystick X and Y positions

  joyposVert = analogRead(joyVert);
  joyposHorz = analogRead(joyHorz);


  Serial.print("X = ");
  Serial.print(joyposVert);

  Serial.print(" Y = ");
  Serial.println(joyposHorz);

  
  // Determine if this is a forward or backward motion
  // Do this by reading the Verticle Value
  // Apply results to MotorSpeed and to Direction

  if (joyposVert < 460)
  {
    // This is Backward

    // Set Motor A backward

    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);

    // Set Motor B backward

    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);

    //Determine Motor Speeds

    // As we are going backwards we need to reverse readings

    joyposVert = joyposVert - 460; // This produces a negative number
    joyposVert = joyposVert * -1;  // Make the number positive

    MotorSpeed1 = map(joyposVert, 0, 460, 0, 100);
    MotorSpeed2 = map(joyposVert, 0, 460, 0, 100);

  }
  else if (joyposVert > 564)
  {
    // This is Forward

    // Set Motor A forward

    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);

    // Set Motor B forward

    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);

    //Determine Motor Speeds

    MotorSpeed1 = map(joyposVert, 564, 1023, 0, 100);
    MotorSpeed2 = map(joyposVert, 564, 1023, 0, 100);

  }
  else
  {
    // This is Stopped

    MotorSpeed1 = 0;
    MotorSpeed2 = 0;

  }

  // Now do the steering
  // The Horizontal position will "weigh" the motor speed
  // Values for each motor

  if (joyposHorz < 460)
  {
    // Move Left

    // As we are going left we need to reverse readings

    joyposHorz = joyposHorz - 460; // This produces a negative number
    joyposHorz = joyposHorz * -1;  // Make the number positive

    // Map the number to a value of 255 maximum

    joyposHorz = map(joyposHorz, 0, 460, 0, 100);


    MotorSpeed1 = MotorSpeed1 - joyposHorz;
    MotorSpeed2 = MotorSpeed2 + joyposHorz;

    // Don't exceed range of 0-255 for motor speeds

    if (MotorSpeed1 < 0)MotorSpeed1 = 0;
    if (MotorSpeed2 > 255)MotorSpeed2 = 255;

  }
  else if (joyposHorz > 564)
  {
    // Move Right

    // Map the number to a value of 255 maximum

    joyposHorz = map(joyposHorz, 564, 1023, 0, 100);


    MotorSpeed1 = MotorSpeed1 + joyposHorz;
    MotorSpeed2 = MotorSpeed2 - joyposHorz;

    // Don't exceed range of 0-255 for motor speeds

    if (MotorSpeed1 > 255)MotorSpeed1 = 255;
    if (MotorSpeed2 < 0)MotorSpeed2 = 0;

  }


  // Adjust to prevent "buzzing" at very low speed

  if (MotorSpeed1 < 20)MotorSpeed1 = 0;
  if (MotorSpeed2 < 20)MotorSpeed2 = 0;

  // Set the motor speeds

  analogWrite(enA, MotorSpeed1);
  analogWrite(enB, MotorSpeed2);

}

TRANSMIT

/*-----TRANSMIT-----*/

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN   9
#define CSN_PIN 10

#define POT_X A0
#define POT_Y A1
//#define POT_A A2
//#define POT_B A3


// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe


/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
int pot[2];


void setup()
{
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(pipe);
}


void loop()
{
  pot[0] = analogRead(POT_X);
  pot[1] = analogRead(POT_Y);
  //pot[2] = analogRead(POT_A);
  //pot[3] = analogRead(POT_B);

  radio.write( pot, sizeof(pot) );


  Serial.print("X = ");
      Serial.print(pot[0]);

      Serial.print(" Y = ");
      Serial.println(pot[1]);


}]

RECEIVER

/*-----RECEIVE-----*/

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN   9
#define CSN_PIN 10

// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe


/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

/*-----( Declare Variables )-----*/
int pot[2];  // 2 element array holding Joystick readings

#include <Servo.h>      // include the servo library

Servo servo1;      
Servo servo2;      

unsigned long prevMillis = 0;
long interval = 500;

int switchPin = 7;

int pos;
int pos2;

void setup()
{
  Serial.begin(9600);
  Serial.println("Nrf24L01 Receiver Starting");
  radio.begin();
  radio.openReadingPipe(1,pipe);
  radio.startListening();
  ;

 // servo1.attach(5);  
 // servo2.attach(6);  

  pinMode(switchPin, INPUT_PULLUP);
}

void loop()
{
  //if (digitalRead(switchPin) == HIGH)
 // {
    // Read the data payload until we've received everything
    bool done = false;
    if (!done)
    {
      int analogValue = pot[0]; // read the analog input
      int analogValue2 = pot[1]; // read the analog input

      int servoAngle = map(analogValue, 0, 1023, 50, 130);
      int servoAngle2 = map(analogValue2, 0, 1023, 50, 130);

      // Fetch the data payload
    radio.read( pot, sizeof(pot) );
      Serial.print("Xo = ");
      Serial.print(pot[0]);

      Serial.print(" Y = ");
      Serial.println(pot[1]);

     // servo1.write(servoAngle);
      delay (20);     
     // servo2.write(servoAngle2);
      delay (20);
    }
  }
 /* else
  {
    unsigned long newMillis = millis();
    if (newMillis - prevMillis > interval)
    {
      prevMillis = newMillis;

      int pos = random(80, 100);
      int pos2 = random(50, 130);

      servo1.write(pos);
      servo2.write(pos2);
    }
  }
}*/

Try moving the code in line 64 in the Receive program so it is before what is now line 57.

...R
Simple nRF24L01+ Tutorial

These sketches are working. What I want to do is create a new sketch from the first one, with the wireless joystick control of the second two sketches. I don't know how to make the nRF24s to communicate properly so the sketch will work.

stevex:
These sketches are working. What I want to do is create a new sketch from the first one, with the wireless joystick control of the second two sketches. I don't know how to make the nRF24s to communicate properly so the sketch will work.

Did you try the change I suggested?

Have you tried the first example in my Tutorial?

...R

You can try this code, I know it all works my end has I've tested it on my set up that I have running on my test bench with my modules I already had running.

I've left out the servo's part has I don't have any or the library installed plus it's just about getting the correct data received then add the servos may be one at time . I've added a fail safe kind of thing so if the data is lost/ wired in correct then it will display in the serial monitor.

So this should get you going and easy to understand but remember bit the RX & TX code must have the same amount of data and same variable like an INT both sides in the struct part and must not exceed 32 bytes.

TX Code:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
//########################################
// Define all the input/output pins      #
//########################################
#define CE_PIN   9
#define CSN_PIN 10
#define POT_X A0
#define POT_Y A1
//#define POT_A A2
//#define POT_B A3

//########################################
// Set the transreiciver module          #
//########################################
RF24 radio(CE_PIN, CSN_PIN); // select  CSN  pin
const uint64_t pipeOut = 0xE8E8F0F0E1LL; // Define the transmit pipe

// The sizeof this struct should not exceed 32 bytes
// This gives us up to 32 8 bits channals
struct MyData {
  int Pot_X_channel;
  int Pot_Y_channel;
  // int Pot_A_channel;
  // int Pot_B_channel;

};

MyData data;

void resetData()
{
  //This are the start values of each channal
  // Throttle is 0 in order to stop the motors
  data.Pot_X_channel = 0;
  data.Pot_Y_channel = 0;
  // data.Pot_A_channel = 0; //Steer left signal
  // data.Pot_B_channel = 0;

}

void setup()
{
  Serial.begin(9600);
  radio.begin();
  radio.setAutoAck(false);
  radio.setDataRate(RF24_250KBPS);
  radio.openWritingPipe(pipeOut);
  resetData();
  delay(200);
}


void loop()
{
  data.Pot_X_channel = analogRead(POT_X);
  data.Pot_Y_channel = analogRead(POT_Y);
  //data.Pot_A_channel = analogRead(POT_A);
  //data.Pot_B_channel = analogRead(POT_B);
  radio.write(&data, sizeof(MyData));
}

RX Code:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
//#include <Servo.h>      // include the servo library But I dont have installed
//########################################
// Define all the input/output pins      #
//########################################
#define CE_PIN   9
#define CSN_PIN 10
const uint64_t pipeIn =  0xE8E8F0F0E1LL;//0xE8E8F0F0E1LL;
RF24 radio(CE_PIN, CSN_PIN);
unsigned long previousMillis = 0;        // will store last time LED was updated
const long interval = 1000;           // interval at which to blink (milliseconds)

//Servo servo1;
//Servo servo2;
unsigned long lastRecvTime = 0;
struct MyData {
  int Pot_X_channel_in;
  int Pot_Y_channel_in;
  // int Pot_A_channel_in;
  // int Pot_B_channel_in;
};

MyData data;

void resetData()
{
  Serial.println("LOST DATA/CONNECTION");

}
/**************************************************/
void setup()
{
  Serial.begin(9600);
  Serial.println("Nrf24L01 Receiver Starting");
  radio.begin();
  radio.setDataRate(RF24_250KBPS); // Both endpoints must have this set the same
  radio.setAutoAck(false);
  radio.openReadingPipe(1, pipeIn);
  radio.startListening();
}
/**************************************************/


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 of second
    // signal lost? tunr it off
    resetData();
  }

  int servoAngle1 = map(data.Pot_X_channel_in, 1, 1023, 50, 130);
  int servoAngle2 = map(data.Pot_Y_channel_in, 1, 1023, 50, 130);
  //  int servoAngle31 = map(data.Pot_A_channel_in, 0, 1023, 50, 130);
  //  int servoAngle42 = map(data.Pot_BX_channel_in, 0, 1023, 50, 130);
  //#######################################
  //# Print the data every second can be more)
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    //Print the Incoming data
    Serial.print("Pot_X_channel_in = ");
    Serial.print(data.Pot_X_channel_in);
    Serial.println("   ");
    Serial.print("Pot_Y_channel_in = ");
    Serial.print(data.Pot_Y_channel_in);
    Serial.println("   ");
    Serial.print("servoAngle1 Mapped = ");
    Serial.print(servoAngle1);
    Serial.println("   ");
    Serial.print("servoAngle2 Mapped = ");
    Serial.print(servoAngle2);
    Serial.println("   ");
  }
  //  servo1.write(servoAngle1);
  // delay (20);
  //  servo2.write(servoAngle2);
  // delay (20);

}

/**************************************************/