nrf24l01 switch with led

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

Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

I agree with Reply #1

The code you posted does not seem to be sending data for the switch.

...R

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

Title says switch, but it looks like you are reading a pot.

I have 2 Unos with RF24 radios. I loaded the RX and TX code from the OP after inserting some print statements. When I run the code the 4 channels data is received fine so the problem is not related to the radio's code.

I suggest that you insert serial prints to follow the program execution and variable values.

Another suggestion, put a delay in the transmitter program to slow sending the data.

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 1000;  // change later when code works
   if (millis() - timer >= interval)
   {
      timer = millis();
      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));
      Serial.println("sending");
   }
}
ch1_value = map(received_data.ch1 , 0 , 255 , 0 , 255);

What the heck is that? Is

ch1_value = received_data.ch1);

too simple?

Modified receive function:

void receive_the_data()
{
if (radio. available()) // use if
{
Serial.print("ch1 "); Serial.println(received_data.ch1);
Serial.print("ch2 "); Serial.println(received_data.ch2);
Serial.print("ch3 "); Serial.println(received_data.ch3);
Serial.print("ch4 "); Serial.println(received_data.ch4);
Serial.println();
radio.read(&received_data, sizeof(Received_data));
lastRecvTime = millis();
}
}

Are you actually receiving the data?