Hello,
I am currently working on a connection between two nrf24l01 modules. So far I am able to controll servos, dc motors (via a H-bridge) and led's with a joystick / potentiometer, but i also want to use a switch as a digital Input to turn on an led.
When I tried adding a switch and a led to the code, the other functions were disabled, for example the servo motor was constantly moving around and the other led was randomly blinking.
Here is the code:
Tx:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
const uint64_t my_radio_pipe = 0xE8E8F0F0E1LL;
RF24 radio(9, 10);
struct Data_to_be_sent{
byte ch1;
byte ch2;
byte ch3;
byte ch4;};
Data_to_be_sent sent_data;
void setup(){
radio. begin ();
radio. setAutoAck(false);
radio. setDataRate(RF24_250KBPS);
radio. openWritingPipe(my_radio_pipe);
sent_data. ch1 = 127;
sent_data. ch2 = 127;
sent_data. ch3 = 127;
sent_data. ch4 = 127;}
void loop(){
sent_data.ch1 = map(analogRead(A0), 0 , 1024 , 0 , 255);
sent_data.ch2 = map(analogRead(A1), 0 , 1024 , 0 , 255);
sent_data.ch3 = map(analogRead(A2), 0 , 1024 , 0 , 255);
sent_data.ch4 = map(analogRead(A3), 0 , 1024 , 0 , 255);
radio.write(&sent_data, sizeof(Data_to_be_sent));}
Rx:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
const uint64_t pipeIn = 0xE8E8F0F0E1LL;
RF24 radio(9, 10);
Servo myservo;
Servo motor;
#define in1 3
#define in2 4
#define enA 5
int led = 6;
int motorspeed = 0;
struct Received_data{
byte ch1;
byte ch2;
byte ch3;
byte ch4;};
Received_data received_data;
int ch1_value = 0;
int ch2_value = 0;
int ch3_value = 0;
int ch4_value = 0;
void reset_the_Data(){
received_data.ch1 = 128;
received_data.ch2 = 0;
received_data.ch3 = 0;
received_data.ch4 = 0;
}
void setup(){
pinMode(in1 , OUTPUT);
pinMode(in2 , OUTPUT);
pinMode(enA , OUTPUT);
pinMode(led , OUTPUT);
analogWrite(enA , motorspeed);
myservo.attach(2);
motor.attach(7);
reset_the_Data();
Serial.begin(9600);
radio. begin ();
radio. setAutoAck(false);
radio. setDataRate(RF24_250KBPS);
radio. openReadingPipe(1, pipeIn);
radio.startListening();}
unsigned long lastRecvTime = 0;
void receive_the_data(){
while(radio. available()){
radio.read(&received_data, sizeof(Received_data));
lastRecvTime = millis();}}
void loop(){
receive_the_data();
unsigned long now = millis();
if (now - lastRecvTime > 1000){
reset_the_Data();
}
ch1_value = map(received_data.ch1 , 0 , 255 , 0 , 255);
ch2_value = map(received_data.ch2 , 0 , 255 , 0 , 180);
ch3_value = map(received_data.ch3 , 0 , 255 , 0 , 180);
ch4_value = map(received_data.ch4 , 0 , 255 , 0 , 255);
myservo.write(ch2_value);
motor.write(ch3_value);
analogWrite(led, ch4_value);
if(ch1_value<126){
digitalWrite(in1 , HIGH);
digitalWrite(in2 , LOW);
motorspeed = map(ch1_value , 127 , 0 , 0 , 255);
analogWrite(enA , motorspeed);}
else if(ch1_value>130){
digitalWrite(in1 , LOW);
digitalWrite(in2 , HIGH);
motorspeed = map(ch1_value , 127 , 255 , 0 , 255);
analogWrite(enA , motorspeed);}
else{
motorspeed = 0;
analogWrite(enA , motorspeed);}
Serial.print(ch1_value);
Serial.println(ch2_value);
delay(10);
}
I would appreciate if someone could help me figure out how to add this feature to the code.
Thanks and best Regards