Hi,
I'm using the nRF24L01 transceiver to write and receive data from a controller to a car.
The controller reads many potentiometers, and I'm trying to put all the inputs into one variable which can then be sent through the transceiver and decoded once it gets over.
I'm able to get sample code working with the transceivers and I have successfully sent "Hello World" from one to the other, however, I cannot get a solution working that sends the variable that contains all the potentiometer outputs.
Transmitter Code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
// This is the code for the transmitter, which goes on a nano and into my controller.
RF24 radio(2, 3); // CE, CSN
const byte address[6] = "00001";
String s = ", ";
void setup() {
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MAX);
radio.stopListening();
Serial.begin(9600);
pinMode(A0, INPUT_PULLUP);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT_PULLUP);
pinMode(A4, INPUT);
pinMode(A5, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(A6, INPUT);
pinMode(A7, INPUT);
}
//void loop() {
// const char text[] = "Hello World";
// radio.write(&text, sizeof(text));
// delay(1000);
//}
void loop() {
int joysticka1 = analogRead(A0);
int joysticka2 = map(analogRead(A1), 0, 1023, 99, 0);
int joysticka3 = map(analogRead(A2), 0, 1023, 99, 0);
int joystickb1 = analogRead(A3);
int joystickb2 = map(analogRead(A4), 0, 1023, 99, 0);
int joystickb3 = map(analogRead(A5), 0, 1023, 99, 0);
int switcha = digitalRead(4);
int switchb = digitalRead(5);
int pota = map(analogRead(A6), 0, 1023, 99, 0);
int potb = map(analogRead(A7), 0, 1023, 99, 0);
if (joysticka1 > 50){
joysticka1 = 0;
}
else
{
joysticka1 = 1;
}
if (joystickb1 > 50){
joystickb1 = 0;
}
else
{
joystickb1 = 1;
}
String test = "";
test += joysticka1;
if (joysticka2 <= 9){
test += "0";
}
test += joysticka2;
if (joysticka3 <= 9){
test += "0";
}
test += joysticka3;
test += joystickb1;
if (joystickb2 <= 9){
test += "0";
}
test += joystickb2;
if (joystickb3 <= 9){
test += "0";
}
test += joystickb3;
test += switcha;
test += switchb;
if (pota <= 9){
test += "0";
}
test += pota;
if (potb <= 9){
test += "0";
}
test += potb;
const char charbuf[32];
char text[] = test.toCharArray(charbuf, 32);
radio.write(&text, sizeof(text));
// Serial.println(test);
// radio.write(&test, sizeof(test));
delay(50);
}
Receiver Code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(8, 7); // CE, CSN
const byte address[6] = "00001";
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MAX);
radio.startListening();
}
void loop() {
if (radio.available()) {
char text[32] = "";
radio.read(&text, sizeof(text));
Serial.println(text);
}
delay(50);
}
I am fairly sure the issue lies within how the transmitter is 'packaging up' and sending the data. The issue almost certainly lies within the last 4 lines of the loop().
Hoping someone can take a look and tell me what I'm doing wrong
Thanks in advance