I hope I'm posting this to the right place, apologies if this is in the wrong part of the forum. Im looking for some help with a project. I have created an arduino / nrf24l01 based controller using a nano, 4 buttons, 2 xbox analog sticks and an nrf24l01. The code I have works well when using another Arduino eg another nano, uno or mega but when trying to use the same code on a Seeeduino Xiao the output goes nuts. To explain these are the values that the controller outputs and an arduino based receiver displays in the serial monitor:
Left y :510
Left x :505
Right y :497
Right x :535
Left Joystick Button: 0
Right Joystick Button: 0
Left button A: 0
Left button B: 0
Right button A: 0
Right button B: 0
But when using the Xiao I get this:
33096190
35062257
0
0
0
0
0
0
0
0
The transmitter code I am using is this:
/* Controller software version 1.01
*
- Version 1.01 changelog:
-Ammended annotations-Scrapped version 1.1 as PWM conversion to be handled by receiver*/
// Libraries:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>// NRF24L01:
RF24 radio(7, 8); // CE and CSN pins
const byte addresses[6] = "Free1"; // Define address for the radio
int dataWrite[10]; // Define data array// Pins:
const int joyLeftXPin = A0, // Define left X axis
joyLeftYPin = A1, // Define left y axis
joyRightXPin = A2, // Define right X axis
joyRightYPin = A3, // Define right y axis
joyButton_Left = 3, // Define left analog stick button
joyButton_Right = 2, // Define right analog stick button
joyButtonLeftA = 10, // Define left "A" button
joyButtonLeftB = 9, // Define left "B" button
joyButtonRightA = 5, // Define right "A" button
joyButtonRightB = 6; // Define right "B" buttonvoid setup() {
// Enable pins:
pinMode(joyButton_Left, OUTPUT); // Enable left joystick button as an output
pinMode(joyButton_Right, OUTPUT ); // Enable right joystick button as an output
pinMode(joyButtonLeftA, OUTPUT); // Enable left "A" button as an output
pinMode(joyButtonLeftB, OUTPUT); // Enable left "B" button as an output
pinMode(joyButtonRightA, OUTPUT); // Enable right "A" button as an output
pinMode(joyButtonRightB, OUTPUT); // Enable right "B" button as an output
// Serial setup:
Serial.begin(9600); // Enable serial @ 9600 baud
// NRF24L01 setup:
radio.begin(); // Initialise NRF24L01
radio.setPALevel(RF24_PA_LOW); // Set PA (power) level to low
radio.setDataRate(RF24_1MBPS); // Set data rate to 1 megabit per second (mpbs)
radio.setRetries(0, 15); // Set the number and delay of retries
radio.openWritingPipe(addresses); // Open writing pipe
radio.openReadingPipe(1, addresses);// Open reading pipe
radio.stopListening(); // Stop listening for incoming messages
}// Main code:
void loop()
{
/* Add the values collected from the analog sticks and buttons into the array defined earlier.
Also added is the result of each value printed to the serial output and a 1 second delay
to analyse the data being sent. Comment out the delay or reduce to 10 for use*/
dataWrite[0] = analogRead(joyLeftYPin);
Serial.print("Left y :" );
Serial.println(dataWrite[0]);
dataWrite[1] = analogRead(joyLeftXPin);
Serial.print("Left x :" );
Serial.println(dataWrite[1]);
dataWrite[2] = analogRead(joyRightYPin);
Serial.print("Right y :" );
Serial.println(dataWrite[2]);
dataWrite[3] = analogRead(joyRightXPin);
Serial.print("Right x :" );
Serial.println(dataWrite[3]);
dataWrite[4] = digitalRead(joyButton_Left);
Serial.print("Left Joystick Button: " );
Serial.println(dataWrite[4]);
dataWrite[5] = digitalRead(joyButton_Right);
Serial.print("Right Joystick Button: " );
Serial.println(dataWrite[5]);
dataWrite[6] = digitalRead(joyButtonLeftA);
Serial.print("Left button A: " );
Serial.println(dataWrite[6]);
dataWrite[7] = digitalRead(joyButtonLeftB);
Serial.print("Left button B: " );
Serial.println(dataWrite[7]);
dataWrite[8] = digitalRead(joyButtonRightA);
Serial.print("Right button A: " );
Serial.println(dataWrite[8]);
dataWrite[9] = digitalRead(joyButtonRightB);
Serial.print("Right button B: " );
Serial.println(dataWrite[9]);
radio.write(dataWrite, sizeof(dataWrite));
delay(1000);
}
and the receiver is:
// Libraries:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>// NRF24L01:
RF24 radio(6, 7); // CE and CSN pins
//const uint64_t pipe = 0xE6E6E6E6E6E6;
const byte addresses[6] = "Free1"; // Define address for the radio
int nrfDataRead[10]; // Define data arrayvoid setup() {
Serial.begin(9600);
// NRF24L01 setup
radio.begin(); // initialize RF24
radio.setPALevel(RF24_PA_MAX); // set power amplifier (PA) level
radio.setDataRate(RF24_1MBPS); // set data rate through the air
radio.setRetries(0, 15); // set the number and delay of retries
radio.openWritingPipe(addresses); // open a pipe for writing
radio.openReadingPipe(1, addresses);// open a pipe for reading
radio.startListening();
}void loop() {
// put your main code here, to run repeatedly:
if ( radio.available() ) {
radio.read( &nrfDataRead, sizeof(nrfDataRead) );// Controls left or right movement. Any value lower than 505 indicates a move to the right whilst anything higher is a move to the left int a = nrfDataRead[0]; Serial.println(a); // Controls movement forwards or backwards. Any value lower than 500 indicates reverse whilst anything higher is forwards int b = nrfDataRead[1]; Serial.println(b); int c = nrfDataRead[2]; Serial.println(c); int d = nrfDataRead[3]; Serial.println(d); int e = nrfDataRead[4]; Serial.println(e); int f = nrfDataRead[5]; Serial.println(f); int g = nrfDataRead[6]; Serial.println(g); int h = nrfDataRead[7]; Serial.println(h); int i = nrfDataRead[8]; Serial.println(i); int j = nrfDataRead[9]; Serial.println(j);
As stated this code works on a nano based setup but when I swap the receiver for a Xiao it all goes haywire as shown above. I'd be grateful if anyone can suggest anything to help. Many thanks.