Please see page #2. Hardware works with test sketches provided by Robin2.
Below is history using different code
Hi All,
Still very new to Arduino and coding. I have setup the following code for a receiver and transmitter
both use nRF24L01 modules, and an Arduino Nano board each.
I have a joystick attached to the Transmitter that reads the X, Y and switch states.
Viewing this in the monitor shows the values as expected, these change as I move the stick / press switch.
Position X: 500
Position Y: 500
Switch: 0
I can see the following on the Receiver monitor:
Position X: 14
Position Y: 14
Switch: 0
Only the switch responds and acts as expected, as you can see the X and Y get different numbers, however they do not change when the joystick is moved. I assume theres a different way to parse integers that maybe I don't yet know how to code?
Any suggestions / help is greatly appreciated. Thank you
Note: I did take out the checks to confirm the radio was connecting as the variables wouldn't parse fullstop. I had used a demo set of code (link below) that worked and the only thing I could see dfifferent was that it did not have the radio connecting checks.
I will need to revist this at a later stage, but for now I just want to see data from one going to the other.
Transmitter
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <FastLED.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
#define JoyX A0 // Joystick X pin connected to A0 on the UNO
#define JoyY A2 // Joystick Y pin connected to A2 on the UNO
int Switchpin = 3; // Digital pin connected to switch output
int buttonState;
#define LED_PIN 5
#define NUM_LEDS 1
#define NUM_STRIPS 1
CRGB Leds[NUM_STRIPS][NUM_LEDS];
void setup()
{
pinMode(JoyX, INPUT);
pinMode(JoyY, INPUT);
pinMode(Switchpin, INPUT_PULLUP);
digitalWrite(Switchpin, HIGH);
Serial.begin(9600);
FastLED.addLeds<WS2812, LED_PIN, GRB>(Leds[0], NUM_LEDS);
Leds[0][0] = CRGB(140,255,0); // Orange
FastLED.show();
delay(100);
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MAX);
radio.stopListening();
}
void loop()
{
int buttonState = (digitalRead(Switchpin));
radio.write(&JoyX, sizeof(JoyX));
radio.write(&JoyY, sizeof(JoyY));
radio.write(&buttonState, sizeof(buttonState));
Serial.print("JoyX : ");
Serial.println(analogRead(JoyX));
Serial.print("JoyY : ");
Serial.println(analogRead(JoyY));
Serial.print("buttonState : ");
Serial.println(buttonState);
Serial.println("=============");
delay(500);
Leds[0][0] = CRGB(255,0,0); // GREEN
FastLED.show();
delay(1000);
}
Receiver
#include "HCPCA9685.h" // Include the HCPCA9685 library created by Andrew Davies
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <FastLED.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
#define LED_PIN 5
#define NUM_LEDS 1
#define NUM_STRIPS 1
CRGB Leds[NUM_STRIPS][NUM_LEDS];
#define I2CAdd 0x40 // Default address of the PCA9685 Module (Servo array) // this comes later
int JoyX;
int JoyY;
int buttonState;
uint32_t start=0;
uint32_t timeout=0;
//const int Switch_Pin = 3; // digital pin connected to switch output
//int buttonState = 0;
// Used to store the mapping of the Joystick X and Y values
int LefteyeY;
int LefteyeX;
int RighteyeY;
int RighteyeX;
HCPCA9685 HCPCA9685(I2CAdd); // Define Library to use I2C communication
void setup()
{
HCPCA9685.Init(SERVO_MODE); // Set to Servo Mode
HCPCA9685.Sleep(false); // Wake up PCA9685 module
Serial.begin(9600);
FastLED.addLeds<WS2812, LED_PIN, GRB>(Leds[0], NUM_LEDS);
Leds[0][0] = CRGB(140,255,0); // Orange
FastLED.show();
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MAX);
radio.startListening();
delay(100);
//set all servos to 90
HCPCA9685.Servo(0, 90); // Move Servo 0
HCPCA9685.Servo(1, 90); // Move Servo 1
HCPCA9685.Servo(2, 90); // Move Servo 5
HCPCA9685.Servo(3, 90); // Move Servo 6
HCPCA9685.Servo(4, 90); // Move Servo 0 // Right Eye Up and down
HCPCA9685.Servo(5, 90); // Move Servo 1 // Right Eye left and right
HCPCA9685.Servo(6, 90); // Move Servo 5
HCPCA9685.Servo(7, 90); // Move Servo 6
delay(1000);
}
void loop()
{
Leds[0][0] = CRGB(255,0,0); // GREEN
FastLED.show();
radio.read(&JoyX, sizeof(JoyX));
radio.read(&JoyY, sizeof(JoyY));
radio.read(&buttonState, sizeof(buttonState));
//monitoring
Serial.print("Joy X : ");
Serial.println(JoyX);
Serial.print("Joy Y : ");
Serial.println(JoyY);
Serial.print("Button : ");
Serial.println(buttonState);
Serial.println("========");
delay(1000);
// end monitoring
//blink();
int val1X = JoyX; // Read current value of Joystick 1 X axis
int val1Y = JoyY; // Read current value of Joystick 1 Y axis
// Map Joystick Axis values to servo Min and Max position
RighteyeY = map(val1X, 4, 1023, 0, 110 ); // Used to move Servo 4 (Right Eye up and down)
RighteyeX = map(val1Y, 5, 1023, 80, 110); // Used to move Servo 3 (Left Eye up and down)
LefteyeY = map(val1Y, 3, 1023, 0, 110); // Used to move Servo 3 (Left Eye up and down)
LefteyeX = map(val1Y, 2, 1023, 800, 110); // Used to move Servo 3 (Left Eye up and down)
// Move Servos to read postion from Joystick
HCPCA9685.Servo(2, LefteyeX); // Move Servo 2
HCPCA9685.Servo(3, LefteyeY); // Move Servo 3
HCPCA9685.Servo(4, RighteyeY); // Move Servo 6
HCPCA9685.Servo(5, RighteyeX); // Move Servo 7
delay(200);
}
//blink code
void blink() {
if(millis() - start >= timeout) {
start = millis();
timeout = random(5000,15000); // between 5 - 15 seconds;
HCPCA9685.Servo(0, 0); // Move Servo 0
HCPCA9685.Servo(1,180); // Move Servo 1
HCPCA9685.Servo(6, 0); // Move Servo 6
HCPCA9685.Servo(7, 0); // Move Servo 7
delay(200);
}
}