The Transmitter
// SimpleTx - the master or the transmitter
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <LiquidCrystal_I2C.h>
#define CE_PIN 9
#define CSN_PIN 10
LiquidCrystal_I2C lcd(0x3f, 16, 2);
const byte slaveAddress[5] = {'R','x','A','A','A'};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
int dataToSend[3];
char txNum = '0';
int GazE;
int SagSolE;
int YukariAsagiE;
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1; // send once per second
void setup() {
lcd.begin();
lcd.backlight();
lcd.setCursor(0,0);
Serial.begin(9600);
Serial.println("SimpleTx Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.setRetries(3,5); // delay, count
radio.openWritingPipe(slaveAddress);
}
//====================
void loop() {
GazE = map(analogRead(0),23,200,0,255);
SagSolE = map(analogRead(2),37,260,70,120);
YukariAsagiE = map(analogRead(1),35,230,65,125);
if (GazE > 255) // My pot is too unbalanced. ( Sometimes it can show up to "200" value)
GazE = 255;
if (GazE < 0)
GazE = 0; //( "Gaz" Means Throttle. "GazE" Means Throttle Edited)
if (SagSolE > 120) // "Sag Sol" Means Left Right. "SagSolE" Means Left Right Edited)
SagSolE = 120;
if (SagSolE < 70)
SagSolE = 70;
if (YukariAsagiE > 125) // "Yukari Asagi" Means Up Down. "YukariAsagiE" Means Up Down Edited)
YukariAsagiE = 125;
if (YukariAsagiE < 65)
YukariAsagiE = 65;
dataToSend[0] = GazE;
dataToSend[1] = YukariAsagiE;
dataToSend[2] = SagSolE;
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis) { // I dont understand anything here
send();
prevMillis = millis();
}
}
//====================
void send() {
bool rslt;
rslt = radio.write( &dataToSend, sizeof(dataToSend) );
if (rslt) {
lcd.print("Connected"); // If its connected , it prints to lcd connected :p
lcd.setCursor(0,0);
}
else {
lcd.print("Connection Lost"); // If connection lost , it prints connection lost and clear it because if it connect again it writes "Connectedn Lost"
delay(1000);
lcd.clear();
}
}
//================
And the Reciever
// SimpleRx - the slave or the receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
#define CE_PIN 9
#define CSN_PIN 10
const byte thisSlaveAddress[5] = {'R','x','A','A','A'};
RF24 radio(CE_PIN, CSN_PIN);
int dataReceived[3]; // this must match dataToSend in the TX
bool newData = false;
int int1;
int int2;
int int3;
Servo UpDown;
Servo LeftRight;
int Throttle;
void setup() {
UpDown.attach(6);
LeftRight.attach(5);
Serial.begin(9600);
Serial.println("SimpleRx Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.openReadingPipe(1, thisSlaveAddress);
radio.startListening();
}
//=============
void loop() {
getData();
showData();
UpDown.write(int2);
LeftRight.write(int3);
digitalWrite(3,int1);
}
//==============
void getData() {
if ( radio.available() ) {
radio.read( &dataReceived, sizeof(dataReceived) );
newData = true;
int1 = dataReceived[0];
int2 = dataReceived[1];
int3 = dataReceived[2];
}
}
void showData() {
if (newData == true) {
Serial.println(int1);
Serial.println(int2);
Serial.println(int3);
newData = false;
}
}