I just try to control a car wirelessly using nRF24L01 module with Arduino Uno. But i don't have a joystick module now so I use two potentiometers , one potmeter for forward and backward control and another potmeter for right and left control. I stored the potentiometer values in structure form and try to send it to the receiver .but it does not send and receive anything.
I did not complete the entire program , just now i started to write the program . using the program i attached below, i try to make sure that the transmitter transmits whole structure correctly and receiver receives whole structure correctly .
I don't no where the problem really occurs .Nothing is printed on serial port .
Transmitter code:
#include<RF24.h>
#include<SPI.h>
#include<nRF24L01.h>
const int CEpin=9;
const int CSNpin=10;
RF24 radio(CEpin,CSNpin);
const byte address[]={"00001"};
const int Potpin1=A0;
const int Potpin2=A1;
int Readval1=0;
int Readval2=0;
struct motorWriting
{
int Right=0;
int Left=0;
int Forward=0;
int Backward=0;
}dataTosend;
void setup() {
pinMode(Potpin1,INPUT);
pinMode(Potpin2,INPUT);
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(&address);
radio.startListening();
}
void loop() {
Readval1=analogRead(Potpin1);
Readval2=analogRead(Potpin2);
if(0<=Readval1 && Readval1<=450 )
dataTosend.Backward=map(Readval1,0,450,0,255);
if(574<=Readval1 && Readval1<=1023)
dataTosend.Forward=map(Readval1,574,1023,0,255);
if(0<=Readval2 && Readval2<=450)
dataTosend.Left=map(Readval2,0,450,0,255);
if(574<=Readval2 && Readval2<=1023)
dataTosend.Right=map(Readval2,574,1023,0,255);
if(radio.write(&dataTosend,sizeof(dataTosend)))
{
Serial.print("\nTransmitted Data is : ");
Serial.print("\nRight = ");Serial.print(dataTosend.Right);
Serial.print("\nLeft = ");Serial.print(dataTosend.Left);
Serial.print("\nForward = ");Serial.print(dataTosend.Forward);
Serial.print("\nBackward = ");Serial.print(dataTosend.Backward);
Serial.print("\n\n");
}
}
Receiver code:
#include<RF24.h>
#include<SPI.h>
#include<nRF24L01.h>
const int CEpin=9;
const int CSNpin=10;
RF24 radio(CEpin,CSNpin);
const byte address[]={"00001"};
struct motorReading
{
int Right=0;
int Left=0;
int Forward=0;
int Backward=0;
}dataToread;
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(1,&address);
radio.startListening();
}
void loop() {
if(radio.available())
{
radio.read(&dataToread,sizeof(dataToread));
Serial.print("\nReceived Data is : ");
Serial.print("\nRight = ");Serial.print(dataToread.Right);
Serial.print("\nLeft = ");Serial.print(dataToread.Left);
Serial.print("\nForward = ");Serial.print(dataToread.Forward);
Serial.print("\nBackward = ");Serial.print(dataToread.Backward);
Serial.print("\n\n");
}
}
what I have to correct in this program to transmit and receive a structure. I don't bother about the other part of the program