This reads two characters into a one-character buffer. You should declare 'data' the same way in both sketches.
A: You didn't get the value out of the buffer ('data') and put it in valX.
B: The range of 'valX' is 0 to 255 so it is always under 460.
Transmitter:
// RF24 - Version: Latest
#include "RF24.h"
#include <SPI.h>
// This is just the way the RF24 library works:
// Hardware configuration: Set up nRF24L01 radio on SPI bus (pins 10, 11, 12, 13) plus pins 7 & 8
RF24 radio(7, 8);
byte addresses[][6] = {"1Node", "2Node"};
// Define the Joystick Connections
int joyYPin = A0;
int joyXPin = A1;
void setup()
{
Serial.begin(9600);
Serial.println("THIS IS THE TRANSMITTER CODE - YOU NEED THE OTHER ARDIUNO TO SEND BACK A RESPONSE");
radio.begin(); //start radio communication
radio.setPALevel(RF24_PA_LOW); //power level
radio.setDataRate(RF24_2MBPS); //make it quick
radio.setChannel(124); // Use a channel unlikely to be used by Wifi
radio.openWritingPipe(addresses[1]); //send
radio.openReadingPipe(1, addresses[0]); //recieve
}
void loop()
{
byte valX = analogRead(joyXPin) / 4;
byte valY = analogRead(joyYPin) / 4;
uint8_t data[2]; // defines size of array
data[0] = valX; // puts the value from the variable valX into the first element
// note elements numbered from 0
// also assumes that valX is defined as uint8_t or byte
data[1] = valY;
radio.write(data, sizeof data);
// Show user what we sent
Serial.print("Sent: ");
Serial.print(valX);
Serial.print(' ');
Serial.println(valY);
}
Receiver:
// RF24 - Version: Latest
#include "RF24.h"
#include <SPI.h>
// Servo - Version: Latest
#include <Servo.h>
// This is just the way the RF24 library works:
// Hardware configuration: Set up nRF24L01 radio on SPI bus (pins 10, 11, 12, 13) plus pins 7 & 8
RF24 radio(7, 8);
byte addresses[][6] = {"1Node", "2Node"};
Servo LeftWheel;
int LeftWheelPin = 3;
Servo RightWheel;
int RightWheelPin = 4;
void setup()
{
Serial.begin(9600);
Serial.println("THIS IS THE RECEIVER CODE - YOU NEED THE OTHER ARDUINO TO TRANSMIT");
LeftWheel.attach(LeftWheelPin);
RightWheel.attach(RightWheelPin);
radio.begin(); //start radio communication
radio.setPALevel(RF24_PA_LOW); //power level
radio.setDataRate(RF24_2MBPS); //make it quick
radio.setChannel(124); // Use a channel unlikely to be used by Wifi
radio.openReadingPipe(1, addresses[1]); //recieve
radio.startListening(); //start listening for radio
}
void loop()
{
// This is what we receive from the other device (the transmitter)
unsigned char data[2];
// Is there any data for us to get?
if (radio.available())
{
radio.read(data, sizeof data);
int valX = data[0];
int valY = data[1];
Serial.print("Recieved: ");
Serial.print(valX);
Serial.print(' ');
Serial.println(valY);
int speed = map(valY, 0, 225, 0, 180);
int steering = map(valX, 0, 225, -30, 30);
int leftSpeed = constrain(speed - steering, 0, 180);
int rightSpeed = constrain((180 - speed) - steering, 0, 180);
LeftWheel.write(leftSpeed);
RightWheel.write(rightSpeed);
}
}