Go Down

Topic: IR remote sketch respond only after pressing a button three times (Read 486 times) previous topic - next topic

TwoChain

Mar 24, 2013, 06:55 am Last Edit: Mar 24, 2013, 06:57 am by TwoChain Reason: 1
Hello,

I have been working with some code that is in the Arduino Cookbook. The code will turn a LED on/off when any button on a remote is pushed. For (what I thought would be) a simple modification of this code, I have been trying to alter it so that a button has to be pressed three times within a certain amount of time before the led will turn on. For instance, the button would have to be pressed three times within 2 seconds for the led to be turned on. If this criterion is not met, the led should not turn on. I thought this would be simple, but I have been playing around with it now for 5 hours and I have nothing to show for it. If anyone has any suggestions or tips on how to accomplish this goal I would really appreciate it. My attempts have been so useless I won't bother posting what I've tried. Instead, I have posted the original code that simply turns a LED on/off.

Here is the code

Code: [Select]


#include <IRremote.h>                  

const int irReceiverPin = 2;            
const int ledPin = 12;

IRrecv irrecv(irReceiverPin);            
decode_results decodedSignal;            

void setup()
{
 pinMode(ledPin, OUTPUT);
 irrecv.enableIRIn();                  
}

boolean lightState = false;    
unsigned long last = millis();

void loop()
{
 if (irrecv.decode(&decodedSignal) == true)
                                           
 {
   if (millis() - last > 250) {      
     lightState = !lightState;              
     digitalWrite(ledPin, lightState);
   }
   last = millis();
   irrecv.resume();                        
 }
}
     


Looking forward to any suggestions/comments.

Thanks,

Dustin


Nick Gammon

Post what you tried. It will give a clue to your thought patterns.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

LarryD

#2
Mar 24, 2013, 07:09 am Last Edit: Mar 24, 2013, 07:11 am by LarryD Reason: 1
Maybe this will help:

Set up a counter, a switch identifier and a timer.

When a switch is pressed log that switch #, increment the counter and start the timer.
If the switch is pressed again increment the counter, check if the timer is still OK.
If the switch is pressed again increment the counter, check if the timer is still OK,     do something based on that switch #.

If the timer times out cancel the process.
Note: if a different switch is pressed cancel the process and start a new one.


The way you have it in your schematic isn't the same as how you have it wired up!

TwoChain

Hello Nick and Larry,

Thank you for your replies.

Nick: I have tried multiple approaches, but as I have had no luck with them, I have continually been saving over each new approach. I have retyped out one of my approaches for you to see.

Code: [Select]


#include <IRremote.h>                   

const int irReceiverPin = 2;             
const int ledPin = 12;
unsigned long test = millis();
unsigned long testa = millis();



IRrecv irrecv(irReceiverPin);           
decode_results decodedSignal;           

void setup()
{
  pinMode(ledPin, OUTPUT);
  irrecv.enableIRIn();                   
}

boolean lightState = false;     
unsigned long last = millis();



void loop()
{
  if (irrecv.decode(&decodedSignal) == true)
                                             
  {
    if (millis() - last > 250)
    {       
    test = millis();
    }
    last = millis();
                       
  }
  irrecv.resume();
 
  if (irrecv.decode(&decodedSignal) == true)
                                             
  {
    if (millis() - last > 250&& millis()-test < 3000)
    {       
    goto Next;
    }
    else
    {
    goto over;
    }

                         
  }


Next:
   last = millis();
   irrecv.resume();

  if (irrecv.decode(&decodedSignal) == true)
                                             
  {
    if (millis() - last > 250&& millis()-test < 3000)
    {       
      lightState = !lightState;             
      digitalWrite(ledPin, lightState);
    }
    else
    {
      goto over;
    }
   
    last = millis();
    irrecv.resume();                         
  }

over:;

   
    last = millis();
    irrecv.resume();

}
     
   



What I was hoping to do was record the time when the button is first pressed (this is my 'test' variable). I then wanted to check each time the button was pressed if the total time had been less that 3 seconds, while also avoiding 'bounce' issues from my remote, hence the     'if (millis() - last > 250&& millis()-test < 3000)' line. If the criterion is not met, I then jump to the end of the code to start again.

Larry, your suggestion is helpful, and that is in line with what I was trying to accomplish. I will try again to make it work based on your suggestion.

Thank you both,

Dustin

Nick Gammon

Right, well flip the "goto" the birdie, and concentrate on dealing with how to process multiple events over time, like cooking dinner.

http://www.gammon.com.au/blink
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up