Hello everyone,
I am new to this forum so if anything is wrong, please let me know.
The project:
Currently I am creating a remote controlled car. I am using 2 potmeters (one for steering and one for power). For this project I am using two arduino Nano's with one nRF24L01 each. Using these nRF24L01 the two Arduino's are communicating.
The Issue:
On Arduino Nano 1 (Tx) I have connected the two potmeters. I am trying to send the value of those two potmeters to the other Arduino Nano (Rx). I am trying to use an Array to send both values over one channel. With both codes uploaded (see codes bellow) the value of Array[0] (pot_power_pin) is send and received, I see it on the Serial Monitor (Rx). I also see a value of '0' after the value of Array[0] (Rx). However when I turn the potmeter of Array[1] (pot_steer_pin), the value is not changing on the receiver side (Rx). On the Serial Monitor of Tx the value is changing.
It seems to me that the Tx has the correct values. The problem could be one of the following:
- Only Array[0] is send and Array[1] is not.
- Only Array[0] is received and Array[1] is not.
Code Tx
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
int pin_RF_CE = 9; //RF-CE pin
int pin_RF_CSN = 10; //RF-CSN pin
RF24 radio(pin_RF_CE, pin_RF_CSN);
const byte address[6] = "00001";
int pot_power_pin = A0; //Potmeter gas pin
int pot_power_min = 0; //Minimum gas
int pot_power_max = 255; //Maximum gas
int pot_steer_pin = A1; //Potmeter stuur pin
int pot_steer_min = 0; //Minimum stuur
int pot_steer_max = 255; //Maximum stuur
const int Arraynum = 2;
int Array[Arraynum];
void setup() {
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN); //RF PALevel
radio.stopListening(); //RF Transmitter
pinMode (pot_power_pin, INPUT);
pinMode (pot_steer_pin, INPUT);
Serial.begin(9600); //Teste, verwijderen als code klopt
delay(2000);
}
void loop() {
Array[0] = analogRead(pot_power_pin);
Array[0] = map(Array[0], 0, 1023, pot_power_min, pot_power_max);
Array[1] = analogRead(pot_steer_pin);
Array[1] = map(Array[1], 0, 1023, pot_steer_min, pot_steer_max);
Serial.print(Array[0]); //Testen, verwijderen als code klopt
Serial.println(Array[1]); //Testen, verwijderen als code klopt
radio.write(&Array, sizeof(Array));
}
Code Rx
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9,10); // CE, CSN
const byte address[6] = "00001";
const int Arraynum = 2;
int Array[Arraynum];
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
}
void loop() {
if (radio.available()) {
byte Array[2];
radio.read(&Array, sizeof(Array));
Serial.print(Array[0]);
Serial.println(Array[1]);
}
}
My Question:
I have put many hours in trying to figure out why not both values are send. Can someone please help me to find the error in my code.
As I said I am new to this forum and still learning to work with Arduino so any advice is very much appreciated.
Thanks to you all for helping!