improved firmware for transmitter and receiver.
Transmitter:(date 15th August 2022)
//nRF24L01 communication 2 ways Arduino A
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN
const byte addresses [][6] = {"00001", "00002"}; //Setting the two addresses. One for transmitting and one for receiving
int MASTER_DATA[7];
int SWITCH1 = 6;
int SWITCH2 = 7;
int SWITCH3 = 8;
int POTENTIOMETER1 = A2;
int POTENTIOMETER2 = A2;
int JOYSTICK_X = A0;
int JOYSTICK_Y = A1;
int RESPONCE_LED = 4;
int connectionStatus = 1;//sending 1 to "ON"
int connectionStatusResponce;
void setup() {
Serial.begin(115200);
pinMode(JOYSTICK_X, INPUT);
pinMode(JOYSTICK_Y, INPUT);
pinMode(SWITCH1, INPUT_PULLUP);
pinMode(SWITCH2, INPUT_PULLUP);
pinMode(SWITCH3, INPUT_PULLUP);
pinMode(POTENTIOMETER1, INPUT);
pinMode(POTENTIOMETER2, INPUT);
pinMode(RESPONCE_LED, OUTPUT);
radio.begin(); //Starting the radio communication
radio.openWritingPipe(addresses[1]); //Setting the address at which we will send the data
radio.openReadingPipe(1, addresses[0]); //Setting the address at which we will receive the data
radio.setPALevel(RF24_PA_MIN); //You can set it as minimum or maximum depending on the distance between the transmitter and receiver.
}
void loop()
{
delay(5);
sending_Master_Data();
delay(5);
receive_Slave_Data();
}
void sending_Master_Data(){
radio.stopListening(); //This sets the module as transmitter
MASTER_DATA[0] = analogRead(JOYSTICK_X);
MASTER_DATA[1] = analogRead(JOYSTICK_Y);
MASTER_DATA[2] = digitalRead(SWITCH1);
MASTER_DATA[3] = digitalRead(SWITCH2);
MASTER_DATA[4] = digitalRead(SWITCH3);
MASTER_DATA[5] = analogRead(POTENTIOMETER1);
MASTER_DATA[6] = analogRead(POTENTIOMETER2);
radio.write(&MASTER_DATA, sizeof(MASTER_DATA)); //Sending the data
radio.write(&connectionStatus, sizeof(connectionStatus));
}
void receive_Slave_Data(){
digitalWrite(RESPONCE_LED, LOW);
radio.startListening(); //This sets the module as receiver
while(!radio.available()); //Looking for incoming data
radio.read(&connectionStatusResponce, sizeof(connectionStatusResponce)); //Reading the data
if(connectionStatusResponce == 1){
digitalWrite(RESPONCE_LED, HIGH);
// Serial.println("Slave Device Connection Success.");
}
}
Receiver:(date 15th August 2022)
//nRF24L01 communication 2 ways Arduino B
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN
const byte addresses [][6] = {"00001", "00002"}; //Setting the two addresses. One for transmitting and one for receiving
int MASTER_DATA[7];
int LED1 = A0;
int LED2 = A1;
int LED3 = A2;
int connectionStatus;
int connectionStatusResponce;
void setup() {
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
radio.begin(); //Starting the radio communication
radio.openWritingPipe(addresses[0]); //Setting the address at which we will send the data
radio.openReadingPipe(1, addresses[1]); //Setting the address at which we will receive the data
radio.setPALevel(RF24_PA_MIN); //You can set it as minimum or maximum depending on the distance between the transmitter and receiver.
}
void loop(){
delay(5);
radio.startListening(); //This sets the module as receiver
if (radio.available()) //Looking for incoming data
{
radio.read(&MASTER_DATA, sizeof(MASTER_DATA));
radio.read(&connectionStatus, sizeof(connectionStatus));
//connection status of the transmitter
connectionStatusResponce = (connectionStatus == 1) ? 1 : 0;
int JOYSTICK_X = MASTER_DATA[0];
int JOYSTICK_Y = MASTER_DATA[1];
int LED1_FLAG = (MASTER_DATA[2] == 1) ? 0 : 1;
int LED2_FLAG = (MASTER_DATA[3] == 1) ? 0 : 1;
int LED3_FLAG = (MASTER_DATA[4] == 1) ? 0 : 1;
int POT1_VALUE = map(MASTER_DATA[5], 0 , 1024, 0, 180);
int POT2_VALUE = map(MASTER_DATA[6], 0 , 1024, 0, 180);
digitalWrite(LED1, LED1_FLAG);
digitalWrite(LED2, LED2_FLAG);
digitalWrite(LED3, LED3_FLAG);
}
delay(5);
radio.stopListening(); //This sets the module as transmitter
// button_stateB = digitalRead(button_pinB);
radio.write(&connectionStatusResponce, sizeof(connectionStatusResponce)); //Sending the data
}
*****************************************************************
TRANSMITTER CODE:-
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define JOYX A0
#define JOYY A1
#define POTA A2
#define POTB A3
#define SWITCHA 6
#define SWITCHB 7
#define SWITCHC 8
RF24 radio(9, 10); // CE, CSN
const byte address[6] = "MKINVENTIONS100";
void setup() {
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
pinMode(JOYX, INPUT);
pinMode(JOYY, INPUT);
pinMode(POTA, INPUT);
pinMode(POTB, INPUT);
pinMode(SWITCHA, INPUT_PULLUP);
pinMode(SWITCHB, INPUT_PULLUP);
pinMode(SWITCHC, INPUT_PULLUP);
}
void loop() {
int ROBOT[7];
ROBOT[0] = analogRead(JOYX);
ROBOT[1] = analogRead(JOYY);
ROBOT[2] = analogRead(POTA);
ROBOT[3] = analogRead(POTB);
ROBOT[4] = digitalRead(SWITCHA);
ROBOT[5] = digitalRead(SWITCHB);
ROBOT[6] = digitalRead(SWITCHC);
radio.write(&ROBOT, sizeof(ROBOT));
delay(5);
}
RECEIVER CODE:-
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
RF24 radio(9, 10); // CE, CSN
const byte address[6] = "MKINVENTIONS100";
int LMT1 = 5;
int LMT2 = 6;
int RMT1 = 7;
int RMT2 = 8;
int LEDA = A0;
int LEDB = A1;
int LEDC = A2;
Servo servo1;
Servo servo2;
void setup() {
Serial.begin(9600);
pinMode(LMT1, OUTPUT);
pinMode(LMT2, OUTPUT);
pinMode(RMT1, OUTPUT);
pinMode(RMT2, OUTPUT);
pinMode(LEDA, OUTPUT);
pinMode(LEDB, OUTPUT);
pinMode(LEDC, OUTPUT);
servo1.attach(3);
servo2.attach(4);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
digitalWrite(LMT1, LOW);
digitalWrite(LMT2, LOW);
digitalWrite(RMT1, LOW);
digitalWrite(RMT2, LOW);
servo1.write(90);
servo2.write(90);
}
void loop() {
delay(5);
if ( radio.available()) {
ROBOT();
}
}
void ROBOT(){
Serial.println("STOP :");
STOPP();
while (radio.available()) {
int ROBOT[7];
radio.read(&ROBOT, sizeof(ROBOT));
int POTA= map(ROBOT[2], 0, 1023, 0, 180);
int POTB= map(ROBOT[3], 0, 1023, 180, 0);
//Serial.println("POT A:");
//Serial.println(POTA);
//Serial.println("POT B:");
//Serial.println(POTB);
servo1.write(POTA);
servo2.write(POTB);
//*******JOYSTICK********
if(ROBOT[0] > 550){
Serial.println("FORWARD :");
Serial.println(ROBOT[0]);
FORWARD();
}
if(ROBOT[0] < 450){
Serial.println("BACKWARD :");
Serial.println(ROBOT[0]);
BACKWARD();
}
if(ROBOT[1] > 600){
Serial.println("LEFT :");
Serial.println(ROBOT[1]);
LEFT();
}
if(ROBOT[1] < 450){
Serial.println("RIGHT :");
Serial.println(ROBOT[1]);
RIGHT();
}
//************LIGHTS****************
if(ROBOT[4] == 0){
digitalWrite(LEDA, HIGH);
Serial.println("LEDA ON");
}
if(ROBOT[4] == 1){
digitalWrite(LEDA, LOW);
}
if(ROBOT[5] == 0){
digitalWrite(LEDB, HIGH);
Serial.println("LEDB ON");
}
if(ROBOT[5] == 1){
digitalWrite(LEDB, LOW);
}
if(ROBOT[6] == 0){
digitalWrite(LEDC, HIGH);
Serial.println("LEDC ON");
}
if(ROBOT[6] == 1){
digitalWrite(LEDC, LOW);
}
}
}
void RIGHT(){
digitalWrite(LMT1, LOW);
digitalWrite(LMT2, HIGH);
digitalWrite(RMT1, HIGH);
digitalWrite(RMT2, LOW);
}
void LEFT(){
digitalWrite(LMT1, HIGH);
digitalWrite(LMT2, LOW);
digitalWrite(RMT1, LOW);
digitalWrite(RMT2, HIGH);
}
void FORWARD(){
digitalWrite(LMT1, HIGH);
digitalWrite(LMT2, LOW);
digitalWrite(RMT1, HIGH);
digitalWrite(RMT2, LOW);
}
void BACKWARD(){
digitalWrite(LMT1, LOW);
digitalWrite(LMT2, HIGH);
digitalWrite(RMT1, LOW);
digitalWrite(RMT2, HIGH);
}
void STOPP(){
digitalWrite(LMT1, LOW);
digitalWrite(LMT2, LOW);
digitalWrite(RMT1, LOW);
digitalWrite(RMT2, LOW);
}
0 Comments
Please do not Enter any Spam Link in the Comment Box
Emoji