element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Need help for IR remote control
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 5 replies
  • Subscribers 393 subscribers
  • Views 940 views
  • Users 0 members are here
Related

Need help for IR remote control

Former Member
Former Member over 11 years ago

for eg. this is a code for switching leds on off with ir remote

 

 

in this when i press Forward button led light up

when i press Reverse button led1 light up

and when any thing else is pressed all go off

it act as a on off switch

i have to press some thing to turn led off

but i want it to act as a push button when Reverse button is pressed it light up and as soon as i release the reverse button it should go off

i tried to use while statment but using that nothing happen

on youtube there are many who have made ir cars but they press one buton to move it and again some other button to stop it

 

#include <IRremote.h>

 

 

int IR_Recv = 3;   //IR Receiver Pin 3

int led = 9;

int led1 = 10; 

int led2 = 11;

int led3 = 12;

 

 

IRrecv irrecv(IR_Recv);

decode_results results;

 

 

void setup(){

  Serial.begin(9600);  //starts serial communication

  irrecv.enableIRIn(); // Starts the receiver

  pinMode(led, OUTPUT);      // sets the digital pin as output

  pinMode(led1, OUTPUT);      // sets the digital pin as output

  pinMode(led2, OUTPUT);      // sets the digital pin as output

  pinMode(led3, OUTPUT);      // sets the digital pin as output

}

 

 

void loop(){

  //decodes the infrared input

  if (irrecv.decode(&results)){

    long int decCode = results.value;

    Serial.println(decCode);

   //switch case to use the selected remote control button

   switch (results.value){

      case 16722135: //when you press the Forward button

    

       digitalWrite(led,HIGH);

       digitalWrite(led1,LOW);

       digitalWrite(led2,LOW);

       digitalWrite(led3,LOW);

       break;

      

      case 16754775: //when you press the Reverse button

        digitalWrite(led1,HIGH);

        digitalWrite(led2,LOW);

        digitalWrite(led3,LOW);

        digitalWrite(led,LOW);

        break;

      

      case 16730295: //when you press the Mute button

           digitalWrite(led2,HIGH);

           digitalWrite(led3,LOW);

           digitalWrite(led1,LOW);

           digitalWrite(led,LOW);

        break;

      

      case 16724175: //when you press the Power button

          digitalWrite(led3,HIGH);  

          digitalWrite(led2,LOW);

          digitalWrite(led1,LOW);

          digitalWrite(led,LOW); 

      default:

      {

        digitalWrite(led,LOW);

        digitalWrite(led1,LOW);

        digitalWrite(led2,LOW);

        digitalWrite(led3,LOW);

      } 

  }

    irrecv.resume(); // Receives the next value from the button you press

  }

 

}

  • Sign in to reply
  • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 11 years ago

    What you need is a time-out for any command, IR only sends when you press a button and some but not all will repeat the command or send a repeat command that is not the same as the original command

     

    so you may need an additional case statement to account for a repeat cmmand (This is easily detected as you have a debug Serial.Println already for every command you receive.

     

    As you will not want the LED to go out immediately after you receive the command you need a delay

     

    this is best achieved using a statement like this

     

    const delayTime 1000 // command delay before LEDs are rest to off

    in timeoutLED = millis() + delayTime // setup the first timeout to ensure the LEDs will extinguish

    ....

    in the loop whenever you receive a valid command

     

    timeoutLED = millis() + delayTime // reset the delay as we just got another valid command

     

    now somewhere outside the case structure have this

     

    if (timeoutLED <= millis())

    {

    // turn off all the LEDs

    }

     

    as long as you keep getting a command the selected LED will remain on

    if no transmit is received for the time-out period then all the LEDs go off.

     

    You can shorten the delay by adjusting the cont value above. You want it to be a little longer than the repeat time of the IR remote so the command seems continuous, you should be able to get away with close to 100mS (My sample above has it at 1000mS (1Sec) right now)

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to Robert Peter Oakes

    iif I don't want to use switch case in program is there a ankther way to do this

    like insted of switch can I use while loop for ir control

    if yes

    can any one show me how will it work

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 11 years ago in reply to Former Member

    the "Switch, Case" method is actually the easiest but only works with integers or single characters. You could use nested "If(){} else if(){}" constructs but that gets messy after a while

     

    Look at my tutorials here for a background on processing commands. the only difference between my code and what your doing is the source of the command (Serial or Ethernet in my case, IR in yours)

     

    http://www.element14.com/community/groups/arduino/blog/2014/06/09/fast-track-to-arduino-programming

     

    This should help you understand what you can do with the right approach

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • nschreiber0813
    0 nschreiber0813 over 11 years ago

    Dear: Rajat

    If you are looking for how to use an Ir sensor look here because the code is pretty good so why don't you try it https://learn.adafruit.com/tmp36-temperature-sensor/overview.

    From: Noah

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 11 years ago in reply to nschreiber0813

    Actually Noah, while good advise re temperature sensors, in this case slightly off topic, what Rajat is asking about is an Infrared remote Control like you use on your TV, not an infrared thermometer or temperature sensor. A good link if that where so but does not answer his immediate question.

     

    Regards

     

    Peter

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube