Notifications
Clear all

Hi I need help with the code on a project I am working on. I have an IR Remote Car which I have lights on. I have brake lights and backup lights which I have working but I can't figure out the code for the turn signals. Can someone can help me with...

Page 1 / 2

Gee
 Gee
(@gee)
Active Member
Joined: 5 months ago
Posts: 18
Topic starter  
#include <IRremote.h>

#define in1 8
#define in2 9
#define in3 10
#define in4 11
int Wh_Led = 7;
int RedLed = 6;
//int L_Led = 5;
int R_Led = 4;
int IR_Recv = A1; //IR Receiver Pin 7
int IR_Recv1 = A0;
IRrecv irrecv(IR_Recv);
IRrecv irrecv1(IR_Recv1);
decode_results results;

void setup(){
  pinMode(in1 , OUTPUT);
  pinMode(in2 , OUTPUT);
  pinMode(in3 , OUTPUT);
  pinMode(in4 , OUTPUT);
  pinMode(Wh_Led, OUTPUT);
  pinMode(RedLed, OUTPUT);
  //pinMode(L_Led, OUTPUT);
  pinMode(R_Led, OUTPUT);
  Serial.begin(9600); //starts serial communication
  irrecv.enableIRIn(); // Starts the receiver
  irrecv1.enableIRIn();
}

void loop(){
  if (irrecv.decode(&results)){
    long int decCode = results.value;
    Serial.println(decCode);
    if (results.value == 16736925)//Forward
    {
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    }
    if (results.value == 16754775)//Reverse
    {
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    digitalWrite(Wh_Led, HIGH);
    delay(1000);
    }
    else{
    if(results.value != 16754775);
    digitalWrite(Wh_Led, LOW);  
    }
    if (results.value == 16720605)//Turn Right
     {
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    digitalWrite(R_Led, HIGH);
    delay(1000);
     }
    else{
    if(results.value != 16720605);
    digitalWrite(R_Led, LOW);
    }   
    if (results.value == 16761405) //Turn Left
     {
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    delay(1000);
      }
    if (results.value == 16712445)//Stop Motor A + B 
    {
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW);
    digitalWrite(RedLed, HIGH);
    delay(1000);
     }
    else{
    if(results.value != 16712445);
    
    digitalWrite(RedLed, LOW);
     }  
     }
    irrecv.resume();

Quote
codecage
(@codecage)
Member Admin
Joined: 3 years ago
Posts: 1156
 

Gee whiz @gee!

The title should be short and yet descriptive, and all that extra verbiage you've included in your title should be in the body of the post.

SteveG


Inst-Tech liked
ReplyQuote
ron bentley
(@ronbentley1)
Estimable Member
Joined: 6 months ago
Posts: 207
 

Hi Gee,

Your problem lies in the fact that you are using the delay function.

Your individual vehicle functions, eg forward, reverse, indicators, etc need to behave independently and be asynchronous.

You will struggle to come up with a design using delay. Your code currently will ignore IR  commands during each of your delays.

Instead, use an approach based on the millis function if you want each function running for a specific time, but I'm not sure this is sensible as the vehicle should be continually commanded via the IR interface with each function operating until commanded otherwise, ie asynchronous functionality.

If you want to explore running multiple millis timers let me know and I can signpost you to a link that can provide an unlimited number of millis timers.

Ron Bentley
Creativity is an input to innovation and change is the output from innovation. Braden Kelley
A computer is a machine for constructing mappings from input to output. Michael Kirby
Through great input you get great output. RZA
Gauss is great but Euler rocks!!


ReplyQuote
Gee
 Gee
(@gee)
Active Member
Joined: 5 months ago
Posts: 18
Topic starter  

@ronbentley1 Thanks Ron for getting back to me . I will try the millis function and let you know how it works . Gee


Inst-Tech liked
ReplyQuote
Gee
 Gee
(@gee)
Active Member
Joined: 5 months ago
Posts: 18
Topic starter  

@codecage Yes your right . First  time user . Will get it right the next time .  Gee  


ReplyQuote
codecage
(@codecage)
Member Admin
Joined: 3 years ago
Posts: 1156
 

@gee 

And I'm confident you will!

Just couldn't resist getting io use the "Gee Whiz" phrase!  🤣 

SteveG


Inst-Tech liked
ReplyQuote
Will
 Will
(@will)
Noble Member
Joined: 1 year ago
Posts: 1929
 

@gee

It's not obvious to me what the purpose of the delay. It usually seems to follow a write to a LED, but not consistently.

Can you please explain what you expect to happen when you call delay ?

I was kidnapped by mimes.
They did unspeakable things to me.


Inst-Tech liked
ReplyQuote
Will
 Will
(@will)
Noble Member
Joined: 1 year ago
Posts: 1929
 

 @gee

I've modified your sketch to (hopefully) bring it closer to doing what I think you want. I've used #defines to give those awful raw numeric value some more understandable names. I've also separated out the operations for those codes ito subroutines.

The subroutines for stop, left and right turns keep running within the sub reading the next IR command and continuing to operate there until the command changes. For example, the right turn sub configures the motors, then turns on the R_Led signal light and then keeps reading the IR until another VALID non-right-hand-turn command is received. It then turns off the R_Led and returns to the loop to handle the new command.

Note that you seem to have cancelled out the left turn signal L_Led, so in that subroutine, I have assigned L_Led to R_Led so it will use the right hand turn signal LED. If you ever assign a left turn LED (i.e. L_Led) you'll need to remove the assignment to R_Led in doLeft.

Please let me know if this brings you closer to what you needed or if you have any questions about how it works or notify me of any errors (I could compile it but don't have the right parts to run it).

 

I was kidnapped by mimes.
They did unspeakable things to me.


Inst-Tech liked
ReplyQuote
Inst-Tech
(@inst-tech)
Reputable Member
Joined: 9 months ago
Posts: 320
 

@gee, Hi, I've taken a look at your code and have written something similar just for testing purposes.

If you follow along with the code, you'll see how you can not only make the turn signal lites turn on, but will flash on & off until you either stop, go forward, or reverse.. I used a Elegoo IR remote so the codes will be different, but you can change those IR codes in your sketch.. I tested this on a bread board with a Red led for stop lite, white for backup lite, and two yellow leds for left & right turn signals..

The code is in a word document, so all you have to do is copy the code, and paste in in your uno, or what ever you are using to test it.  The hook up drawing for testing the sketch is also included in the attached files..

Stoplite turnSignal

 

#include <IRremote.h>

#define in1 8 
#define in2 9
#define in3 10
#define in4 11
// be sure to change you pins for your application, I used pin 2 for the IR_Recv
int Wh_Led = 7; // backup light
int RedLed = 6; // stop light
int L_Led = 5; // left turn light
int R_Led = 4; // right turn light
int IR_Recv = 2; //IR Receiver Pin 2
int inputInt = 1;  // used for the Switch case routine
//int IR_Recv1 = A0;
IRrecv irrecv(IR_Recv);
//IRrecv irrecv1(IR_Recv1);
decode_results results;

void setup(){
 // pinMode(in1 , OUTPUT);
 // pinMode(in2 , OUTPUT);
 // pinMode(in3 , OUTPUT);
 // pinMode(in4 , OUTPUT);
  pinMode(Wh_Led, OUTPUT);
  pinMode(RedLed, OUTPUT);
  pinMode(L_Led, OUTPUT);
  pinMode(R_Led, OUTPUT);
  Serial.begin(9600); //starts serial communication
  irrecv.enableIRIn(); // Starts the receiver
  //irrecv1.enableIRIn();
}

void loop(){
     flash();  // function to flash turn signal lites
    /* the IR codes used in this sketch are for an Elegoo remote control,   
     you will have to change them for your remote control to work
     */
  if (irrecv.decode(&results)){
    long int decCode = results.value;
    Serial.println(decCode);
    if (decCode == 16748655){  // forward
        inputInt = 1;
            }
    else
    if (decCode== 16769055){   // reverse
        inputInt = 2;
        }
     else
     if (decCode == 16720605){  //right
        inputInt = 3;
        }
     else
      if ( decCode == 16761405){  // left
         inputInt = 4;
         }
      else 
      if (decCode == 16756815) {  // stop
         inputInt = 5;   
      }
                
    switch (inputInt){
     
    //-----------------Foward----------------------------------------------
   case 1:      //Forward
    
   // digitalWrite(in1, HIGH);
   // digitalWrite(in2, LOW);
   // digitalWrite(in3, HIGH);
   // digitalWrite(in4, LOW);
      Serial.println("forward"); 
      digitalWrite(Wh_Led,LOW); //turn off backup white lite
      digitalWrite(RedLed,LOW); // turn off stop red lite
      break;
    
  
    //-------------------Reverse-------------------------------------------
    case 2:    //Reverse
   
   // digitalWrite(in1, LOW);
   // digitalWrite(in2, HIGH);
    //digitalWrite(in3, LOW);
    //digitalWrite(in4, HIGH);
      digitalWrite(Wh_Led, HIGH); // turn on backup white lite
      Serial.println("back up Whitelite on");
    
    
   
    break;
   
    //------------------------turn right---------------------------------------
    case 3:          //Turn Right
     
    //digitalWrite(in1, HIGH);
    //digitalWrite(in2, LOW);
    //digitalWrite(in3, LOW);
    //digitalWrite(in4, HIGH);
    Serial.println("Right turn signal");// flash lite on 
    flash();
    
    break;
    
    //---------------------------- turn left--------------------------------------------------  
    case 4:              //Turn Left
     
    //digitalWrite(in1, LOW);
    //digitalWrite(in2, HIGH);
    //digitalWrite(in3, HIGH); 
    //digitalWrite(in4, LOW);  
    Serial.println("Left turn signal"); 
     flash();
    break;
       
      //-----------------------------------stop----------------------------------------------
  case 5:              //Stop Motor A + B 
   
    //digitalWrite(in1, LOW);
    //digitalWrite(in2, LOW);
    //digitalWrite(in3, LOW);
    //digitalWrite(in4, LOW);
       digitalWrite(RedLed, HIGH); // turn on stop lite
       digitalWrite(Wh_Led, LOW); // turn off backup lite
       digitalWrite(R_Led, LOW); // turn off right turn lite
       digitalWrite(L_Led, LOW); // turn off left turn lite
       
       break;
  }  
} 
    irrecv.resume();
     
}

//----------------- function  void flash().-----------------------------
// flashes leds on and off as long as you are in the turning mode
// pressing forward, reverse, or stop will turn the led off
void flash(){
   if (inputInt == 3) {
    Serial.println("Right turn signal on " + String(inputInt));
    digitalWrite(RedLed, LOW); // turn on stop lite
    digitalWrite(Wh_Led, LOW); // turn off backup lite
    digitalWrite(R_Led,HIGH);
    delay(200);
    digitalWrite(R_Led,LOW);
    delay(500);
   }
   else
     if(inputInt == 4) {
    Serial.println("Left turn signal on "  + String(inputInt));
    digitalWrite(RedLed, LOW); // turn on stop lite
    digitalWrite(Wh_Led, LOW); // turn off backup lite
    digitalWrite(L_Led,HIGH);
    delay(200);
    digitalWrite(L_Led,LOW);
    delay(500);
   }
}

 Regards,

LouisR

LouisR


ReplyQuote
Will
 Will
(@will)
Noble Member
Joined: 1 year ago
Posts: 1929
 

@inst-tech 

I'm curious why you use the input code to assign a separate code which is then used in a switch/case statement? Does the Arduino switch not allow numbers as large as the IR codes directly in the switch test ?

Anyway, nice work.

I was kidnapped by mimes.
They did unspeakable things to me.


Inst-Tech liked
ReplyQuote
Inst-Tech
(@inst-tech)
Reputable Member
Joined: 9 months ago
Posts: 320
 

@will, I'm sure the Arduino can use larger number in the switch/case statement, but do to my poor programming skills, this was just a way That I came up with.. lol

The goal was to make the left and right turn signals flash on & off.. So I just used a method I was more familiar with.. still working on getting a handle on this C++ programming..I liked your sketch better..But it's the best I could do for now..Thanks for the encouragement,

regards,

LouisR

LouisR


Will liked
ReplyQuote
Gee
 Gee
(@gee)
Active Member
Joined: 5 months ago
Posts: 18
Topic starter  

@will Hi Will   Thanks for taking the time to modify my sketch. Unfortunately It didn't work when I uploaded it . No lights , no drive in any direction . I can't see why it shouldn't work . Small bug somewhere , I'll find it . Thanks again.     Gee  


Inst-Tech liked
ReplyQuote
Gee
 Gee
(@gee)
Active Member
Joined: 5 months ago
Posts: 18
Topic starter  

@inst-tech  Thanks inst-tech    I will give it a go . I have a few different projects on the go that I would like to dress up with flashing lights .  Gee


Inst-Tech liked
ReplyQuote
Will
 Will
(@will)
Noble Member
Joined: 1 year ago
Posts: 1929
 
Posted by: @gee

@will Hi Will   Thanks for taking the time to modify my sketch. Unfortunately It didn't work when I uploaded it . No lights , no drive in any direction . I can't see why it shouldn't work . Small bug somewhere , I'll find it . Thanks again.     Gee  

It doesn't work because I got so tangled up in my own footwork rearranging the code and separating the command fetching that I forgot to start the loop by getting the next command - bad programmer - no biscuit !

Try adding the line

getNextCommand();

after the "void loop() {" and before the "  if (command==CMD_STOP) {".

That should give it a command to execute and make something happen.

I was kidnapped by mimes.
They did unspeakable things to me.


Inst-Tech liked
ReplyQuote
Gee
 Gee
(@gee)
Active Member
Joined: 5 months ago
Posts: 18
Topic starter  

@will Awesome Thanks


ReplyQuote
Page 1 / 2