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);