RF24 with joystick

Hello, I am writing a code to control a stepper with the X axis and a servo with the Y axis on a joystick wirelessly with an RF24. I think I am sending the X and Y at the same time as both motors are moving on one axis. Could someone please help with my code. Thanks.

TRANSMITTER

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CSN, CE
const byte address[6] = "00001";

int x_key = A1;
int y_key = A0;
int x_pos;
int y_pos;

void setup() {
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();

pinMode (x_key, INPUT) ;
pinMode (y_key, INPUT) ;
}

void loop() {
x_pos = analogRead (x_key) ;
y_pos = analogRead (y_key) ;
radio.write(&x_pos, sizeof(x_pos));
radio.write(&y_pos, sizeof(y_pos));

}

RECEIEVER

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
int Index;
int x_pos;
int y_pos;
Servo myservo;
RF24 radio(7, 8); // CSN, CE
const byte address[6] = "00001";
int servo_pin = 3;

void setup()

{
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
myservo.attach(3);
pinMode(6, OUTPUT); //Enable
pinMode(5, OUTPUT); //Step
pinMode(4, OUTPUT); //Direction
digitalWrite(6, LOW);
}

void loop()

{
if (radio.available()) {

int y_pos;
radio.read(&y_pos, sizeof(y_pos));
//Serial.println(x_pos);
Serial.println(y_pos);
y_pos = map(y_pos, 0, 1023, 70, 110);
myservo.write(y_pos);}

if (radio.available()){
int x_pos;
radio.read(&x_pos, sizeof(x_pos));
if (x_pos > 800) {

digitalWrite(4,HIGH);

for(Index = 0; Index < 200; Index++)

{

digitalWrite(5,HIGH);

delayMicroseconds(500);

digitalWrite(5,LOW);

delayMicroseconds(500);

}
}

if (x_pos < 200) {

digitalWrite(4,LOW);

for(Index = 0; Index < 200; Index++)

{

digitalWrite(5,HIGH);

delayMicroseconds(500);

digitalWrite(5,LOW);

delayMicroseconds(500);

Put the data into a struct so that you send with just one write. Then receive the data into a struct. Here is code that I use to measure, send and receive values from 2 LDRs. Quite similar to reading a 2 axis joystick, sending and receiving the values. Set CE and CSN to suit your setup. Uses code from Robin2's simple rf24 tutorial.

Sender code:

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


const byte CE_PIN = 9;
const byte CSN_PIN = 10;

const byte slaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};

RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

struct LdrValues
{
 int ldr_1;
 int ldr_2; 
}ldrValues;

unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second

const byte LDR1 = A0;
const byte LDR2 = A1;

void setup()
{

  Serial.begin(9600);
  Serial.println("SimpleTx Starting");
  pinMode(LDR1, INPUT_PULLUP);
  pinMode(LDR2, INPUT_PULLUP);
  
  radio.begin();
  radio.setChannel(76);  //76 library default
  //RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX
  radio.setPALevel(RF24_PA_HIGH);
  radio.setDataRate( RF24_250KBPS );
  radio.setRetries(3, 5); // delay, count
  radio.openWritingPipe(slaveAddress);
}

void loop()
{
  currentMillis = millis();
  if (currentMillis - prevMillis >= txIntervalMillis)
  {
     send();
     Serial.print("LDR 1 = ");
     Serial.print(ldrValues.ldr_1);
     Serial.print("    LDR 2 = ");
     Serial.println(ldrValues.ldr_2);
     prevMillis = millis();
  }
}

//====================

void send()
{
  ldrValues.ldr_1 = analogRead(LDR1);
  ldrValues.ldr_2 = analogRead(LDR2);
  radio.write( &ldrValues, sizeof(ldrValues) );
}

Receiver code:

// SimpleRx - the slave or the receiver

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

const byte CE_PIN = 9;
const byte CSN_PIN = 10;

const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};

RF24 radio(CE_PIN, CSN_PIN);

struct LdrValues
{
   int ldr_1;
   int ldr_2;
} ldrValues;

bool newData = false;

//===========

void setup()
{

   Serial.begin(9600);

   Serial.println("SimpleRx Starting");

   radio.begin();
   radio.setChannel(76);  //76 library default
   //RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX
   radio.setPALevel(RF24_PA_HIGH);
   radio.setDataRate( RF24_250KBPS );
   radio.openReadingPipe(1, thisSlaveAddress);
   radio.startListening();
}

//=============

void loop()
{
   getData();
   showData();
}

Read the [url=https://forum.arduino.cc/index.php?topic=712199.0]how get the most out of this forum sticky[/url] to see how to properly post code.  Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code in code tags.
//==============

void getData()
{
   if ( radio.available() )
   {
      radio.read( &ldrValues, sizeof(ldrValues) );
      newData = true;
   }
}

void showData()
{
   if (newData == true)
   {
      Serial.print("Data received >> ");
      Serial.print("LDR 1 = ");
      Serial.print(ldrValues.ldr_1);
      Serial.print("    LDR 2 = ");
      Serial.println(ldrValues.ldr_2);
      newData = false;
   }
}

Put the data into a struct so that you send with just one write and receive with just one read. Then receive the data into a struct. Here is code that I use to measure, send and receive values from 2 LDRs. Quite similar to reading a 2 axis joystick, sending and receiving the values. Set CE and CSN to suit your setup. Uses code from Robin2's simple rf24 tutorial.

Sender code:

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

const byte CE_PIN = 9;
const byte CSN_PIN = 10;

const byte slaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};

RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

struct LdrValues
{
 int ldr_1;
 int ldr_2; 
}ldrValues;

unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second

const byte LDR1 = A0;
const byte LDR2 = A1;

void setup()
{
  Serial.begin(9600);
  Serial.println("SimpleTx Starting");
  pinMode(LDR1, INPUT_PULLUP);
  pinMode(LDR2, INPUT_PULLUP);
  
  radio.begin();
  radio.setChannel(76);  //76 library default
  //RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX
  radio.setPALevel(RF24_PA_HIGH);
  radio.setDataRate( RF24_250KBPS );
  radio.setRetries(3, 5); // delay, count
  radio.openWritingPipe(slaveAddress);
}

void loop()
{
  currentMillis = millis();
  if (currentMillis - prevMillis >= txIntervalMillis)
  {
     send();
     Serial.print("LDR 1 = ");
     Serial.print(ldrValues.ldr_1);
     Serial.print("    LDR 2 = ");
     Serial.println(ldrValues.ldr_2);
     prevMillis = millis();
  }
}
//====================
void send()
{
  ldrValues.ldr_1 = analogRead(LDR1);
  ldrValues.ldr_2 = analogRead(LDR2);
  radio.write( &ldrValues, sizeof(ldrValues) );
}

Receiver code:

// SimpleRx - the slave or the receiver

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

const byte CE_PIN = 9;
const byte CSN_PIN = 10;

const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};

RF24 radio(CE_PIN, CSN_PIN);

struct LdrValues
{
   int ldr_1;
   int ldr_2;
} ldrValues;

bool newData = false;

//===========

void setup()
{
   Serial.begin(9600);
   Serial.println("SimpleRx Starting");

   radio.begin();
   radio.setChannel(76);  //76 library default
   //RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX
   radio.setPALevel(RF24_PA_HIGH);
   radio.setDataRate( RF24_250KBPS );
   radio.openReadingPipe(1, thisSlaveAddress);
   radio.startListening();
}

//=============

void loop()
{
   getData();
   showData();
}

Read the [url=https://forum.arduino.cc/index.php?topic=712199.0]how get the most out of this forum sticky[/url] to see how to properly post code.  Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code in code tags.
//==============

void getData()
{
   if ( radio.available() )
   {
      radio.read( &ldrValues, sizeof(ldrValues) );
      newData = true;
   }
}

void showData()
{
   if (newData == true)
   {
      Serial.print("Data received >> ");
      Serial.print("LDR 1 = ");
      Serial.print(ldrValues.ldr_1);
      Serial.print("    LDR 2 = ");
      Serial.println(ldrValues.ldr_2);
      newData = false;
   }
}

Read the how get the most out of this forum sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code in code tags.

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