I finally managed to communicate two arduinos using nrf24l01 modules. Now i want to write my own code looking at the codes i used before.
TX CODE
/* TRANSMITTER CODE FOR OVERLOKMAKINESII PROTOv0,01 */
/* WARNING LED INDICATIONS */
////////////////////////////
/*
fast blinking three times then staying on for a second = problem with nrf module
Blinking led = problem with transmission
*/
/*OKLED indications*/
/////////////////////
/*
fast blinking 3 times then staying on for a second = hardware is OK
Blinking = LED blinks with every few successful payload transmission
*/
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
#define OKLED 5 // connect a red colore LED to this pin for convenince
#define warningLED 6 // connect green LED to this pin
#define aAnalogPinX A0
#define aAnalogPinY A1
#define bAnalogPinX A2
#define bAnalogPinY A3
RF24 radio(7,8); // CE --> 7 | CSN --> 8
const uint32_t address = 0x1DA1F15LL;
struct payLoad // max 32 bytes so it doesnt exceed nrf24l01's 32bytes buffer limit
{
byte aX;
byte aY;
byte bX;
byte bY;
//insert toggle switch command here to control two of the flight modes
//insert toggle switch command here to control two of the flight modes
//insert potmeter command here to trim x axis (is this a good idea?)
//insert potmeter command here to trim y axis (this is really not a good idea)
};
payLoad payload;
void readValuesFromJoysticks()
{
payload.aX = map(analogRead(aAnalogPinX), 0 , 1023 , 0 , 255);
payload.aY = map(analogRead(aAnalogPinY), 0 , 1023 , 0 , 255);
payload.bX = map(analogRead(bAnalogPinX), 0 , 1023 , 0 , 255);
payload.bY = map(analogRead(bAnalogPinY), 0 , 1023 , 0 , 255);
}
void transmissionSuccessful()
{
digitalWrite(OKLED, HIGH);
delay(1);
digitalWrite(OKLED,LOW);
}
void transmissionFailed()
{
digitalWrite(warningLED, HIGH);
delay(1);
digitalWrite(warningLED, LOW);
}
void setup()
{
pinMode(OKLED, OUTPUT);
pinMode(warningLED, OUTPUT);
//Serial.begin(9600); // NOTE TO SELF: Comment this after debugging and upload again!!!
if(!radio.begin()) // check to see if radio hardware is responding.
{
bool isRadioAvailable = false;
while(!isRadioAvailable)
{
for(int counter = 0; counter < 3; counter++) // blink the red led to indicate there is problem with hardware
{
digitalWrite(warningLED, HIGH);
delay(100);
digitalWrite(warningLED, LOW);
delay(100);
}
digitalWrite(warningLED, HIGH);
delay(1000);
isRadioAvailable = radio.begin(); // trying to connect to radio again
}
}
/*///////////////////////////////////
HARDWARE IS OK
///////////////////////////////////*/
digitalWrite(warningLED, LOW);
for(int counter = 0; counter < 3; counter++) // blink the green led three time to indicate hardware is ok
{
digitalWrite(OKLED, HIGH);
delay(100);
digitalWrite(OKLED, LOW);
delay(100);
}
digitalWrite(OKLED, HIGH);
delay(1000);
digitalWrite(OKLED, LOW);
radio.setPALevel(RF24_PA_MAX); // set this low if you are going to use your receiver close to the transmitter
radio.setPayloadSize(sizeof(payLoad)); // save on transmission time by setting the radio to only transmit the
// number of bytes we need to transmit a payload
radio.setDataRate(RF24_250KBPS);
radio.openWritingPipe(address);
radio.stopListening(); // put radio in TX mode
}
void loop()
{
readValuesFromJoysticks();
bool report = radio.write(&payload, sizeof(payLoad));
if(report)
{
transmissionSuccessful();
}
else
{
transmissionFailed();
}
}
RX CODE
/* RECEIVER CODE FOR OVERLOKMAKINESII PROTOv0.01 */
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#define OKLED 5 // connect a red colore LED to this pin for convenince
#define warningLED 6 // connect green LED to this pin
RF24 radio(7,8); // CE --> 7 | CSN --> 8
const uint32_t address = 0x1DA1F15LL;
struct ReceivedPayLoad // max 32 bytes so it doesnt exceed nrf24l01's 32bytes buffer limit
{
byte aX;
byte aY;
byte bX;
byte bY;
//insert toggle switch command here to control two of the flight modes
//insert toggle switch command here to control two of the flight modes
//insert potmeter command here to trim x axis (is this a good idea?)
//insert potmeter command here to trim y axis (this is really not a good idea)
};
ReceivedPayLoad receivedPayload;
void printReceivedData()
{
Serial.println(receivedPayload.aX);
Serial.println(receivedPayload.aY);
Serial.println(receivedPayload.bX);
Serial.println(receivedPayload.bY);
}
void setup()
{
if(!radio.begin()) // check to see if radio hardware is responding.
{
bool isRadioAvailable = false;
while(!isRadioAvailable)
{
for(int counter = 0; counter < 3; counter++) // blink the red led to indicate there is problem with hardware
{
digitalWrite(warningLED, HIGH);
delay(100);
digitalWrite(warningLED, LOW);
delay(100);
}
digitalWrite(warningLED, HIGH);
delay(1000);
isRadioAvailable = radio.begin(); // trying to connect to radio again
}
}
/*///////////////////////////////////
HARDWARE IS OK
///////////////////////////////////*/
digitalWrite(warningLED, LOW);
for(int counter = 0; counter < 3; counter++) // blink the green led three time to indicate hardware is ok
{
digitalWrite(OKLED, HIGH);
delay(100);
digitalWrite(OKLED, LOW);
delay(100);
}
digitalWrite(OKLED, HIGH);
delay(1000);
digitalWrite(OKLED, LOW);
Serial.begin(9600);
radio.setPALevel(RF24_PA_MAX); // set this low if you are going to use your receiver close to the transmitter
radio.setDataRate(RF24_250KBPS);
radio.openReadingPipe(1, address);
radio.startListening();
}
void loop()
{
if (radio.available())
{
radio.read(&receivedPayload, sizeof(ReceivedPayLoad));
printReceivedData(); //requires Serial port to be initialized thorugh Serial.begin(9600) command
}
}
two modules wont communicate while using this code. What is wrong with the code?