Pages: [1]   Go Down
Author Topic: "Typematic rate" for interrupt  (Read 435 times)
0 Members and 1 Guest are viewing this topic.
Riga, Latvia
Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

 I am trying to create something like "typematic rate" setting for infrared remote, to do a delay before the second trigger gets processed, but my delay for the second trigger gets ignored... If I press a button on the remote, I get lights turned on and off a lot of times smiley-sad
This is my interrupt routine:
Code:
void getIR() {
detachInterrupt(0);                                                 // Not sure if I need to do this, but detaching, just to be sure, we don't get interrupted while in this routine
interrupt_time = millis();
 int tempKey = getIRKey();                                      // Here we get the key number
  if ((interrupt_time - last_interrupt_time) < 1000){
 presscount++;                                                       // counting number of times we get the button press
  } else {
    presscount = 0;                                                  // previous button press was more than a second age, so resetting the counter
  }
 if (presscount == 1) delay(3000);                             // this should be done if we get the second command within one second

 ... different things for every button here ..

  last_interrupt_time = interrupt_time;                      // saving the last interrupt time
  attachInterrupt(0, getIR, LOW);                             // re-attach interrupt 0
}
The problem is, I can't debug it via serial port, because this controller is on custom built pcb and in remote place, where I can only swap MCus... so I hope, someone will point me to the right direction...
Logged

Offline Offline
God Member
*****
Karma: 17
Posts: 721
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is an ISR?  Get that delay out of there.  Delay doesn't work in an ISR.
Logged

Riga, Latvia
Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you. Hmm.. I didn't know that. So I can simply detach the interupt outside the ISR for some time, after it gets processed first time?
Logged

Offline Offline
God Member
*****
Karma: 17
Posts: 721
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't really understand how this is supposed to work.

IF I were you and wanted to do what you sound like you want to do I'd only look for button transitions.  I'd have a simple state machine with two states, a button is pressed and no button is pressed.  In the button is pressed state, I'd make note of the time when I got into that state and then have a set of IF's to check against that time and see if it is time to act.

I'm not sure why you are using an interrupt, but even so the most it should be doing is detecting the button transitions and setting a flag to let the loop know to change the state.  Really a nice tight loop could handle that fine without the interrupt.  I doubt your hands are fast enough to push a button quicker than a tight loop can turn over. 
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Don't do delays in an ISR. And don't cross-post.

http://arduino.cc/forum/index.php/topic,150586.msg1132338.html#msg1132338
Logged

Riga, Latvia
Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't really understand how this is supposed to work.

IF I were you and wanted to do what you sound like you want to do I'd only look for button transitions.  I'd have a simple state machine with two states, a button is pressed and no button is pressed.  In the button is pressed state, I'd make note of the time when I got into that state and then have a set of IF's to check against that time and see if it is time to act.

I'm not sure why you are using an interrupt, but even so the most it should be doing is detecting the button transitions and setting a flag to let the loop know to change the state.  Really a nice tight loop could handle that fine without the interrupt.  I doubt your hands are fast enough to push a button quicker than a tight loop can turn over. 

Well, I do need an interrupt, since it is infrared remote detector. It also works without an interrupt, but sometimes misses the "start bit" from a remote. Anyway, got it working, thank you for your help.
Logged

Offline Offline
God Member
*****
Karma: 17
Posts: 721
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh OK.  That was the confusion I had.  I thought this was the IR transmitter instead of receiver. 
Logged

Pages: [1]   Go Up
Jump to: