Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Arduino learner - Help with IR Code / loops please

Status
Not open for further replies.
I am playing around with an IR sensor on an Arduino and made a simple program to light some LEDs from my TV remote. I have a Red, Green and yellow LED connected and a red, green and yellow button on the remote to operate each one, this works fine.

What I want to be able to do firstly is to be able to press the red button and instead of the red LED just turning on, it would start flashing and stop again when the button is pressed again, with the other (green and yellow) buttons also working as normal. I am very new to this and would appreciate any pointers so I can carry on playing!

Thanks

Code:

/*
Working Code for remote control

red
yellow
green LEDs

with different functions
C:
*/

#include <IRremote.h>

int IRpin = 11;  // pin for the IR sensor
int LEDYellow = 13;    // Yellow LED pin
int LEDRed = 9;      // Red LED pin
int LEDGreen = 5;    //Green LED pin
IRrecv irrecv(IRpin);
decode_results results;

boolean LEDon = true; // initializing LEDon as true

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  pinMode(LEDYellow, OUTPUT); //Pinmode set
  pinMode(LEDRed, OUTPUT);
  pinMode(LEDGreen, OUTPUT);
}

void loop()
{
 
  if (irrecv.decode(&results))
    {
   
      irrecv.resume();   // Receive the next value
    }
   if (results.value == 546983)  // remote control code from serial monitor
     {
       if (LEDon == true)   // is LEDon equal to true?
         {
           LEDon = false;
           digitalWrite(LEDYellow, HIGH);
           delay(100);      // keeps the transistion smooth
           
         }
       
        else
          {
            LEDon = true;
            digitalWrite(LEDYellow, LOW);
            delay(100);
         
          }
       
     }
if (results.value == 571463)
     {
       if (LEDon == true)   // is LEDon equal to true?
         {
           LEDon = false;
           digitalWrite(LEDRed, HIGH);
           delay(100);      // keeps the transistion smooth
         
         }
       
        else
          {
            LEDon = true;
            digitalWrite(LEDRed, LOW);
            delay(100);
         
          }
       
     }
     if (results.value == 538823)
     {
if (LEDon == true)   // is LEDon equal to true?
         {
           LEDon = false;
           digitalWrite(LEDGreen, HIGH);
           delay(100);      // keeps the transistion smooth
         
         }
       
        else
          {
            LEDon = true;
            digitalWrite(LEDGreen, LOW);
            delay(100);
         
          }
       
     }

}

Mod edit.... Use code tags when posting code... It makes it easier to read and will get more replies....
 

Attachments

  • remote_control_yellow_red_green.ino
    2.1 KB · Views: 132
Last edited by a moderator:

Wp100

Well-Known Member
Hi,

Cannot help with your code direct as I'm only at the same stage as you, however have you looked at the many online tutorial series which can help .. here are just 2 popular ones, and don't forget there are masses of info and examples on Arduinos own site .

 
Hey Wp100,

Thanks for that, I have watched a lot of tutorials but I haven't figured out how to task this particular problem of being able to start a process with a button press and still be able to monitor and start another process from another button.....I am trying to do it with loops which is obviously not the correct way!

Thanks
 

Wp100

Well-Known Member
Hi,

Cannot help you with the detailed code but I would use this kind of logic path, though like most programming there is always another way to do the same thing.
Set up a variable FLASH=0
loop
Red key pressed ,yes
Is FLASH ON if yes Turn OFF or 0
If FLASH is OFF turn ON or 1

test yellow and green keys as before

test FLASH = ON then -
test what is state of red led /port
if ON turn off
if OFF turn on
FLASH =OFF
turn off LED
delay might be needed here of about 250ms to produce a visible flash effect
goto loop
 

KeepItSimpleStupid

Well-Known Member
Most Helpful Member
I have an Arduino, but have only done the "hello world" with it. There is polling and interrupt programming. Polling is when you test bits every once in while.
Interrupt programming is when on a change, you do something. n some respects it depends on what Arduino you have.

I'll point you here: https://playground.arduino.cc/Code/Interrupts first.

And I'll send you here: https://playground.arduino.cc/Code/TimerScheduler


In simple terms you can have a routine that runs every 10 mS or so AND you can attach Interupts on a pin change.

You might have a variable that says GREEN=ON/OFF. FLASHING_GREEN=T/F and TICS_GREEN_ON TICS_GREEN_OFF_CNT=integer, TICS_GREEN_OFF_CNT integer and TICS_GREEN_OFF = integer

So, when you get an interrupt on say the rising edge of a button you just set FLASHING_GREEN=TRUE and nothing move.

Then in the interrupt service routine, you actually start counting the 10 mS intervals

e.g.
(Pseudocode)

if FLASHING_GREEN=FALSE then (return from interrupt)
Now if it's true, you have to figure out if it's ON or OFF
If GREEN_ON (read port), you increment TICS_GREEN_ON_CNT until it is equal to TICS_GREEN_ON
....When it's equal you set TICS_GREEN_ON_CNT to zero and GREEN=OFF (Set the port)
....return from interrupt

If GREEN_OFF (read port), you increment TICS_GREEN_OFF_CNT until it is equal to TICS_GREEN_OFF
....When it's equal you set TICS_GREEN_OFF_CNT to zero and GREEN=ON(Set the port)
....return from interrupt

You use your port changes to set FLASHING_GREEN_ON to TRUE or FALSE. You may or may not have to software debounce the switch.

==

So if TICS_GREEN_ON = 10 and TICS_GREEN_OFF = 8, then the blink would be 10*10 mS ON and 8*10 mS off

You can modify the code such that it can blink or be steady. To keep things simple I just showed flashing. You could turn the LED on elsewhere and the ISR does nothing if the LED is in ON_OFF mode. So you can have a variable say GREEN_MODE with the enumerated values of ON_OFF or BLINK and use those as well.
In the ON_OFF mode, the ISR just returns from interrupt. You can set the state elsewhere.

Certain functions may mess up the interrupts. Remember to initialize the variable correctly.
Some could be enumerated. e.g. Defined before the program starts. Defines, I believe.

CODE tags look like [CODE]your code goes here[/CODE]; I purposely circumvented their function.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Top