hello everyone, I'm new on the forum and I'm already terrified of making some indentation error, so I beg for forgiveness since the beginning. I'm also quite new in the arduino programming (or to programming in general). In particular, I struggle to understand data types.
I was trying to make two boards communicate and serial print some readings of some scrappy analog joysticks found at home. I've read on the comments section of amazon that the nRF modules I've bought only work with Mirf library, so I tried to stick to it. (But if you come up with a solution involving more conventional libraries PLEASE tell me, for me it's only important that the code works).
my code is the following:
TRANSMITTER:
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
int j1PotX; //will become data[0]
int j1PotY; //will become data[1]
int j2PotX; //will become data[2]
int j2PotY; //will become data[3]
void setup() {
Serial.begin(9600);
Mirf.cePin = 9; //Set the CE pin to D9
Mirf.csnPin = 10; //Set the Csn pin to D10
Mirf.spi = &MirfHardwareSpi;
Mirf.init(); //initialization nRF24L01
//Set the receiving identifier "Sen01"
Mirf.setTADDR((byte *)"Sen01");
//Set the number of bytes sent and received at a time
Mirf.payload = sizeof(int)*4;
//Sending channel, can fill 0~128, send and receive must be consistent.
Mirf.channel = 3;
Mirf.config();
//Note that one Arduino writes Sender.ino and the other writes Receiver.ino.
//The identifier here is written to Sender.ino
Serial.println("I'm Sender...");
// Set initial default values
j1PotX = 127; // Values from 0 to 255. When Joystick is in resting position, the value is in the middle, or 127. We actually map the pot value from 0 to 1023 to 0 to 255 because that's one BYTE value
j1PotY = 127;
j2PotX = 127;
j2PotY = 127;
}
void loop() {
byte data[4];
// Read all analog inputs and map them to one Byte value
j1PotX = map(analogRead(A1), 0, 1023, 0, 255); // Convert the analog read value from 0 to 1023 into a BYTE value from 0 to 255
j1PotY = map(analogRead(A0), 0, 1023, 0, 255);
j2PotX = map(analogRead(A2), 0, 1023, 0, 255);
j2PotY = map(analogRead(A3), 0, 1023, 0, 255);
data[0] = j1PotX;
data[1] = j1PotY;
data[2] = j2PotX;
data[3] = j2PotY;
Serial.println(j1PotX);
Serial.println(j1PotY);
Serial.println(j2PotX);
Serial.println(j2PotY);
Mirf.send((byte *) &data); //send the packet
//The next step can only be performed after the while loop function transmission is completed.
while(Mirf.isSending()) {}
delay(50);
}
RECEIVER:
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
int j1PotX; //will become data[0]
int j1PotY; //will become data[1]
int j2PotX; //will become data[2]
int j2PotY; //will become data[3]
void setup() {
Serial.begin(9600);
//---------Initial part, can't be modified at any time--------
Mirf.cePin = 9; //Set CE Pin to D9
Mirf.csnPin = 10; //Set Csn Pin to D10
Mirf.spi = &MirfHardwareSpi; //default for Nano
Mirf.init(); //initialization nRF24L01
//---------Configuration part, can be modified at any time--------
//Set the receiving identifier "Rec01"
Mirf.setRADDR((byte *)"Rec01");
//Set the number of bytes sent and received at a time
Mirf.payload = sizeof(int) * 4;
// Sending channel, can fill 0~128, send and receive must be consistent.
Mirf.channel = 3;
Mirf.config();
//Note that one Arduino writes Sender.ino and the other writes Receiver.ino.
//To identify the programwritten in Receiver.ino.
Serial.println("I'm Receiver...");
// Set initial default values
j1PotX = 127; // Values from 0 to 255. When Joystick is in resting position, the value is in the middle, or 127. We actually map the pot value from 0 to 1023 to 0 to 255 because that's one BYTE value
j1PotY = 127;
j2PotX = 127;
j2PotY = 127;
}
void loop() {
//creating the buffer to store data
byte data[4];
//Waiting the prepared receive data:
if (Mirf.dataReady()) {
Mirf.getData((byte *) &data); //Receive data to data array.
}
//serial monitoring data packet received
Serial.println(data[0]); //j1PotX
Serial.println(data[1]); //j1PotY
Serial.println(data[2]); //j2PotX
Serial.println(data[3]); //j2PotY
}
Now, the setup and connections all seem to work, when asking the transmitter to serial print the analog readings, the numbers match what i do on the joysticks. Also, the modules all work fine because trying the example code provided by the manifacturer, I managed to send "123" (literally) on the serial monitor of the receiver.
The problem is that, in my code, the receiver outputs on the serial monitor a set of fixed values, nonsense, like 241-17-242-5 in constant repetition.
I don't know where to look anymore. Even copy-pasting other codes that use Mirf don't work (I cannot guarantee the validity of the sources). Trying to adapt other examples with libraries like RF24.h didn't work neither, because they sent data using structs, while I've read that Mirf library only uses byte arrays for communication. Anyway, given the simplicity of my inputs, I don't understand why my code isn't working.
Thanks a lot in advance. As I said in the beginning, being this my first post, I'm sorry for eventual mistakes.
P.S.
this is the link for the devices I'm using. https://www.amazon.it/gp/product/B07ZCJPJ8B/ref=ppx_yo_dt_b_asin_title_o00_s01?ie=UTF8&psc=1
In the comment section someone pointed out the problem. But I also checked myself, testing a very simple transmit-receive example code (from a website) that used RF24.h
This the manifacturer explicative code, if that might be useful:
//TRANSMITTER
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
void setup() {
Serial.begin(9600);
Mirf.cePin = 9; //Set the CE pin to D9 4
Mirf.csnPin = 10; //Set the Csn pin to D10
Mirf.spi = &MirfHardwareSpi;
Mirf.init(); //initialization nRF24L01
//Set the receiving identifier "Sen01"
Mirf.setRADDR((byte *)"Sen01");
//Set the number of bytes sent and received at a time, here send an integer, write sizeof (unsigned int), actually equal to 2 bytes
Mirf.payload = sizeof(unsigned int);
//Sending channel, can fill 0~128, send and receive must be consistent.
Mirf.channel = 3;
Mirf.config();
//Note that one Arduino writes Sender.ino and the other writes Receiver.ino.
//The identifier here is written to Sender.ino
Serial.println("I'm Sender...");
}
unsigned int adata = 0;
void loop() {
adata = 123;
//Since nRF24L01 can only send Mirf.payload data in a byte single byte array, //So all the data that needs to be transferred must be split into bytes.
//Define a byte array to store pending data, because Mirf.payload = sizeof(unsigned int);
//Actually the following is equal to byte data[2];
byte data[Mirf.payload];
//adata is unsigned int double-byte data and must be split.
//Split the adata high and low eight:
data[0] = adata & 0xFF; //low eight bits to data[0],
data[1] = adata >> 8; //high eight bits to data[1]。
//Settings send data to "serv1"
Mirf.setTADDR((byte *)"Rec01");
Mirf.send(data);
//The next step can only be performed after the while loop function transmission is completed.
while (Mirf.isSending()) {}
delay(20);
}
//RECEIVER
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
//Define a variable adata to store the final result.
unsigned int adata = 0;
void setup() {
Serial.begin(9600);
//---------Initial part, can't be modified at any time--------
Mirf.cePin = 9; //Set CE Pin to D9
Mirf.csnPin = 10; //Set CE Pin to D10
Mirf.spi = &MirfHardwareSpi;
Mirf.init(); //initialization nRF24L01
//---------Configuration part, can be modified it at any time--------
//Set the receiving identifier "Rev01"
Mirf.setRADDR((byte *)"Rec01");
//Set the number of bytes sent and received at a time, here sent an integer.
//Write sizeof(unsigned int), which is actually equal to 2 bytes.
Mirf.payload = sizeof(unsigned int);
// Sending channel, can fill 0~128, send and receive must be consistent.
Mirf.channel = 3; Mirf.config();
//Note that one Arduino writes Sender.ino and the other writes Receiver.ino.
//To identify the programwritten in Receiver.ino.
Serial.println("I'm Receiver...");
}
void loop() {
//Define a scratchpad array with a size of Mirf.payload.
byte data[Mirf.payload];
if (Mirf.dataReady()) //Waiting the prepared receive data.
{
Mirf.getData(data); //Receive data to data array.
//data[1]< move left 8 bits and data[0] merge, reorganize data.
adata = (unsigned int)((data[1] << 8) | data[0]);
Serial.print("pin=");
Serial.println(adata);
//Can also output double-byte data.
//Serial.write(data[1]);
//Serial.write(data[0]);
}
}