Pages: [1]   Go Down
Author Topic: IR remote sketch respond only after pressing a button three times  (Read 369 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

#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

« Last Edit: March 24, 2013, 12:57:03 am by TwoChain » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Post what you tried. It will give a clue to your thought patterns.
Logged

Offline Offline
Edison Member
*
Karma: 56
Posts: 2174
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


« Last Edit: March 24, 2013, 01:11:23 am by LarryD » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

#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
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: