How to add LED's to this sketch?

Hello all,
I finally got my RC tank working, thanks to some advice from others. My code works great, I just need to add some lights to it. I know this sounds very simple, and it may be, I am just having a hard time integrating the LED code to my current sketch. It seems every time I try to something goes wrong somewhere else. I think what makes it difficult is I am using the NRF24L01 radio transmitter, so telling the transmitter to do one thing and the receiver to do another always trips me up. I have spent a long time on this and thought someone else could more than likely just tell me in two or three lines and it would be easy for them. I just need the LED to be a button, but when you press the button it turns it ON then press again for OFF. Hopefully that makes sense, I mean like a toggle.

I am pretty new to Arduino, though working hard on it, I still struggle with adding new things to a sketch. Would anyone mind throwing down a simple sketch of how to do so on the transmitter and receiver side? This is the last thing I need to complete my first big project! Thank you all in advance.

Transmitter

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN
const byte address[][6] = {"00001", "00002"};
char receivedData[32] = "";
int joystick[5];
int button_pin = A2;
boolean button_state = 0;


void setup() {
  pinMode(button_pin, INPUT); //////////////////////
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(address[1]);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();


}
void loop() {

  joystick[0] = analogRead(A4);
  joystick[1] = analogRead(A3);
  joystick[2] = analogRead(A0), joystick[2] = map(joystick[2], 0, 1023, 0, 255);
  joystick[3] = analogRead(A1), joystick[3] = map(joystick[3], 0, 1023, 128, 255);
  joystick[4] = analogRead(A2);
 
  radio.write( joystick, sizeof(joystick) );
 
  {

}
}

Receiver

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
Servo Tilt;
Servo Pan;
#define enA 6
#define in1 7
#define in2 5
#define enB 3
#define in3 4
#define in4 2

RF24 radio(9, 10); // CE, CSN
const byte address[][6] = {"00001", "00002"};
char receivedData[32] = "";          /////this may be a factor
int  xAxis, yAxis;
int motorSpeedA = 0;
int motorSpeedB = 0;
int joystick[5];
int servoAngleA = 0;
int servoAngleB = 0;
int servo_pin = 8;
int servo_pin2 = A1;

boolean button_state = 0;  ////////////////////////////////////
int led_pin = A2;        /////////////////////////////////////////

void setup()
{
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(A2, OUTPUT); ///////////////////////////////////////////
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address[1]);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
  Tilt.attach(servo_pin);
  Pan.attach(servo_pin2);
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);


}
void loop()
{
  if (radio.available()) {

    radio.read( joystick, sizeof(joystick) );
   
    
    //// radio.read(&receivedData, sizeof(receivedData));
    yAxis = joystick[0];
    xAxis = joystick[1];
    servoAngleA = joystick[2];
    servoAngleB = joystick[3];
    button_state = joystick[4];
    Serial.println(yAxis);
    Serial.println(xAxis);
    Serial.println(servoAngleA);
    Serial.println(servoAngleB);
    Serial.println(button_state);
  }

  if (yAxis < 470) {

    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);

    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);

    motorSpeedA = map(yAxis, 470, 0, 0, 255);
    motorSpeedB = map(yAxis, 470, 0, 0, 255);
  }
  else if (yAxis > 550) {

    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);

    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);

    motorSpeedA = map(yAxis, 550, 1023, 0, 255);
    motorSpeedB = map(yAxis, 550, 1023, 0, 255);
  }

  else {
    motorSpeedA = 0;
    motorSpeedB = 0;
  }

  if (xAxis < 470) {

    int xMapped = map(xAxis, 470, 0, 0, 255);

    motorSpeedA = motorSpeedA - xMapped;
    motorSpeedB = motorSpeedB + xMapped;
    // Confine the range from 0 to 255
    if (motorSpeedA < 0) {
      motorSpeedA = 0;
    }
    if (motorSpeedB > 255) {
      motorSpeedB = 255;
    }
  }
  if (xAxis > 550) {

    int xMapped = map(xAxis, 550, 1023, 0, 255);

    motorSpeedA = motorSpeedA + xMapped;
    motorSpeedB = motorSpeedB - xMapped;

    if (motorSpeedA > 255) {
      motorSpeedA = 255;
    }
    if (motorSpeedB < 0) {
      motorSpeedB = 0;
    }
  }

  if (motorSpeedA < 70) {
    motorSpeedA = 0;
  }
  if (motorSpeedB < 70) {
    motorSpeedB = 0;
  }
  analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
  analogWrite(enB, motorSpeedB); // Send PWM signal to motor B

  Tilt.write(servoAngleA);
  Pan.write(servoAngleB);
if(button_state == 1)
{
analogWrite(A2, HIGH);
Serial.println(button_state);
}
else
{
analogWrite(A2, LOW);
Serial.println(button_state);}
}


Then please post your attempts. Well, your best one.

Do you want this led on the transmitter or receiver side ? Also which button press should trigger it ?

Ah pardon me, I should have been clearer there.
The receiver would have the LED, the transmitter would have the button.
I am not sure what you mean when you say which button press should trigger it, I am sure its my ignorance showing. The button could be on any pin, I assume I would use an analog, but I was worried it might mess with my array being sent with the joysticks. Of course, I just am missing some experience and knowledge here unfortunately.

Thanks so much for your time!

I will post them in the morning, I thought about posting them initially, but decided against it. I thought it may confuse the people trying to help if they saw the mess I had made of my working code. I thought knowing for sure what works, apart from what does not, would be best. Though I will definitely do that first chance tomorrow, just wanted to make it as pleasant on you guys as possible.
Thank you very much!

Ah, dont worry, That makes two of us. :slight_smile:

I mean, the Microcontroller on the transmitter side triggers a transmittion based on some button press, yes ?This is what i would do in your place. The algorithm goes like this : When you transmitt the code you want you can include some kind of special character that will trigger the state change of the led on the receiver. Now the microcontroller on the receiver side can be looking for that control character and change the state of the led whenever it finds one Or you could use some specific part of rour transmitted code as an indication that when the receiver sees this will change the state of the pin.

Generally speaking i think that it would help you alot to first figure out the algorithm of the project (the "arcitecture" of your code, the way you want it to work) and then proceed with how you can realise it. I understand it's hard for a beginner (since i am one too) but still it's important to learn working projects like that :slight_smile:

1 Like

I think this is the point. Yes if you just send an extra piece of data that defines the light being on or off, then it will get mixed up with other data, and you will not be able to separate them at the receive end.

You need some sort of protocol to make sure you can differentiate the data.

I think the simplest way is to add an extra row into your joystick matrix, and access that separately at the receive end.

2 Likes

Ok I updated the code in my original post, sorry for the delay, I am gradating University tomorrow so I have had exams all week like crazy.

I updated the code again, I feel just lost now.
I am getting 0's coming across to the REC from Transmitter, though when I press the button nothing happens. I have been messing with this to no avail all day. I would really appreciate some wisdom! ha

Please do not edit your original code. If you change the original code, thread continuity is lost. If you change the code, put it in a new post.

Oh shoot, I did not know that. I may just make a new thread, I did make a mess of that. I wasn't sure the best way to do that.

I posted something similarly before, though I edited the original code, I did not know that would mess the thread up. I thought I would repost for clarity, and to show my most recent attempt at adding LED's to this RC car project.

The sketch I have is working just fine (finally), which is monumental for me. I have an RC car, with a pan/tilt camera set up on top, controlled with two servos. All I need to do to finish the project is add a momentary switch (button or on/off switch) on the transmitter, to control led lights on the receiver.

An important note, I have used an array for the motor control and servos (joystick[4]). I think I should just add the switch to this array, though I have never been successful with this. When I try this sketch it throws off everything, the servos go haywire and then everything stops. As someone in the last forum recommended I need to distinguish this button array from the joystick array. The code will compile as is, though it is not right.

I am certain most of you could look at this and spot the issue quickly, I have been obsessively working on this non stop for days. I was supposed to get this project done by this weekend, but I just finished finals and graduate University tomorrow, so of course it has been a challenge.

Thank you all, this community is the only reason I am still doing this. I would have been stuck way way back without you guys.

Receiver

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
Servo Tilt;
Servo Pan;
#define enA 6
#define in1 7
#define in2 5
#define enB 3
#define in3 4
#define in4 2

RF24 radio(9, 10); // CE, CSN
const byte address[][6] = {"00001", "00002"};
char receivedData[32] = "";   //////// I usually comment this out, just left it becasue I am not sure
int  xAxis, yAxis;
int motorSpeedA = 0;
int motorSpeedB = 0;
int joystick[5];///////////// this was joystick[4] I went to five to try the LED's
int servoAngleA = 0;
int servoAngleB = 0;
int servo_pin = 8;
int servo_pin2 = A1;

boolean button_state = 0;  ////////////////////////////////////
int led_pin = A2;        //////////////////////// A2 is used becasue I do not have any more digital pin space 
                                                    ///////////////(NRF module and motor controller)

void setup()
{
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(A2, OUTPUT); ///////////////////////////////////////////
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address[1]);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
  Tilt.attach(servo_pin);
  Pan.attach(servo_pin2);
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);


}
void loop()
{
  if (radio.available()) {

    radio.read( joystick, sizeof(joystick) );
   
    
    //// radio.read(&receivedData, sizeof(receivedData));//// again this has not been used
    yAxis = joystick[0];
    xAxis = joystick[1];
    servoAngleA = joystick[2];
    servoAngleB = joystick[3];
    button_state = joystick[4];       ////////////////////////////////////////
    Serial.println(yAxis);
    Serial.println(xAxis);
    Serial.println(servoAngleA);
    Serial.println(servoAngleB);
    Serial.println(button_state); /////////////////////////////////////
  }

  if (yAxis < 470) {

    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);

    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);

    motorSpeedA = map(yAxis, 470, 0, 0, 255);
    motorSpeedB = map(yAxis, 470, 0, 0, 255);
  }
  else if (yAxis > 550) {

    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);

    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);

    motorSpeedA = map(yAxis, 550, 1023, 0, 255);
    motorSpeedB = map(yAxis, 550, 1023, 0, 255);
  }

  else {
    motorSpeedA = 0;
    motorSpeedB = 0;
  }

  if (xAxis < 470) {

    int xMapped = map(xAxis, 470, 0, 0, 255);

    motorSpeedA = motorSpeedA - xMapped;
    motorSpeedB = motorSpeedB + xMapped;
    // Confine the range from 0 to 255
    if (motorSpeedA < 0) {
      motorSpeedA = 0;
    }
    if (motorSpeedB > 255) {
      motorSpeedB = 255;
    }
  }
  if (xAxis > 550) {

    int xMapped = map(xAxis, 550, 1023, 0, 255);

    motorSpeedA = motorSpeedA + xMapped;
    motorSpeedB = motorSpeedB - xMapped;

    if (motorSpeedA > 255) {
      motorSpeedA = 255;
    }
    if (motorSpeedB < 0) {
      motorSpeedB = 0;
    }
  }

  if (motorSpeedA < 70) {
    motorSpeedA = 0;
  }
  if (motorSpeedB < 70) {
    motorSpeedB = 0;
  }
  analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
  analogWrite(enB, motorSpeedB); // Send PWM signal to motor B

  Tilt.write(servoAngleA);
  Pan.write(servoAngleB);
///////////////////////// EVERYTHING BELOW IS THE ATTEMPTED ADD-ON////////////////
  
if(button_state == 1)
{
analogWrite(A2, HIGH);
Serial.println(button_state);
}
else
{
analogWrite(A2, LOW);
Serial.println(button_state);}
}

TRANSMITTER

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN
const byte address[][6] = {"00001", "00002"};
char receivedData[32] = "";////// this is not usually used, not sure If i need it
int joystick[5];  /////////was joystick[4]
int button_pin = A2; //////////////////
boolean button_state = 0; ////////////////////////


void setup() {
  pinMode(button_pin, INPUT); //////////////////////
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(address[1]);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();


}
void loop() {

  joystick[0] = analogRead(A4);
  joystick[1] = analogRead(A3);
  joystick[2] = analogRead(A0), joystick[2] = map(joystick[2], 0, 1023, 0, 255);
  joystick[3] = analogRead(A1), joystick[3] = map(joystick[3], 0, 1023, 128, 255);
  joystick[4] = analogRead(A2); //////////////////////////////

  radio.write( joystick, sizeof(joystick) );

  {
 //////////////////EVERYTHING BELOW IS THE ATTEMPTED ADD-ON (MORE THAN LIKELY ALL WRONG)

    button_state = digitalRead(button_pin);
    if (button_state == HIGH) {

      const char receivedData[] = "Your Button State is HIGH";
      radio.write(&receivedData, sizeof(receivedData));
    }
    else {

      const char receivedData[] = "Your Button State is LOW";
      radio.write(&receivedData, sizeof(receivedData));


      radio.write(&button_state, sizeof(button_state));
    }
  }
}

I have merged your topics @tjjohnston33. Please only create one topic for each distinct subject matter. You can always add additional information via replies in the thread as the discussion progresses.

Whoops! sorry

1 Like

What are you trying to do here? Why the comma? Did you possibly mean

  joystick[2] = map(analogRead(A0), 0, 1024, 0, 256);

or the more efficient

  joystick[2] = analogRead(A0) / 4;

?

Things like this can't be left to chance. You can't randomly include or not include something in a data packet...

When I said add an extra element to the joystick array, you have to make this array bigger, so you can add the button and send the whole array.
That way you don't get mixed up and you know where to find it.

You have some odd bits of code in there.

What is this all about? I think you need to remove it.

You need to add an extra element in the array to give a place to put the button value. Then you send it all.

Why have you got anything to do with received data in the transmitter section of code? This code never receives anything. You can not transmit and receive in the same piece of code without getting a lot more complex than you can cope with at the moment.

Get the transmitter side working first before you try anything fancy with the receive code. Just have that print out what it receives to see if it is what you want first.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.