I cant get intterupts to dettach. Im using a pro micro as a HID device to trigger hotkeys for a computer program. I have two buttons, upon pressing button 1 a keystroke is sent, and pressing button 2 a different key stroke is sent.
button 1 is physically attached to pin 3, and button 2 is attached to pin 7
here is my code:
//defining key strokes
#define F5 0xC6
#define F6 0xC7
//setting digital pins
const int button1 = 3;
const int button2 = 7;
void setup()
{
attachInterrupt(digitalPinToInterrupt(button1), taskone, LOW);
attachInterrupt(digitalPinToInterrupt(button2), tasktwo, LOW);
}
void loop()
{
}
void taskone() //run when button1 is pressed
{
disarm_buttons; //detach interrupts while running the following code
Keyboard.write(F5); //send key stroke
delay(1000); //prevents multiple "presses"
arm_buttons; // reattach interrupts
}
void tasktwo() //run when button2 is pressed
{
disarm_buttons; //detach interrupts while running the following code
Keyboard.write(F6); //send key stroke
delay(1000); //prevents multiple "presses"
arm_buttons; //reattach interupts
}
void arm_buttons()
{
attachInterrupt(digitalPinToInterrupt(button1), domark, LOW);
attachInterrupt(digitalPinToInterrupt(button2), dotrack, LOW);
}
void disarm_buttons()
{
detachInterrupt(button1);
detachInterrupt(button2);
}
My problem is; with the delay in the ISRs the interrupt should only trigger once every 1000ms (one second), but when i press the button it fires rapidly no delay. I manipulated to code to see if the interrupts were detaching by removing the "arm_buttons" at the end of the ISRs, they don't seem to be.
Don't use interrupts to detect button presses unless you have extra special circumstances. Buttons pressed by humans are slow and can be processed by polling. If you are determined to use interrupts, then read Nick Gammon's excellent hints.
disarm_buttons; //detach interrupts while running the following code
arduarn:
Don't use interrupts to detect button presses unless you have extra special circumstances. Buttons pressed by humans are slow and can be processed by polling. If you are determined to use interrupts, then read Nick Gammon's excellent hints.
disarm_buttons; //detach interrupts while running the following code
delay() won't work in an ISR, even if it was advisable, since delay() relies on millis() to return the proper time, and millis() relies on the click tick interrupt handler firing regularly. While your ISR is running, the clock tick handler can't, so millis() always returns the same time, and delay() never ends, because time never passes.
Are you sure you can attach to an external interrupt on pin 7? I'm not familiar with Pro Micros, but the pinout diagrams I have seen don't have that as an option.
Jimmus:
Are you sure you can attach to an external interrupt on pin 7? I'm not familiar with Pro Micros, but the pinout diagrams I have seen don't have that as an option.
From the schematic here, pin 7 corresponds with PE6 (port E 6) which, according to the ATMega32U4 datasheet, has the external interrupt capability:
INT6/AIN0 – Port E, Bit 6
INT6, External Interrupt source 6: The PE6 pin can serve as an external interrupt source.