Skip to content

Instantly share code, notes, and snippets.

@greed9
Last active July 7, 2025 18:50
Show Gist options
  • Save greed9/be1920feb58e18f9c37bb5b420362e30 to your computer and use it in GitHub Desktop.
Save greed9/be1920feb58e18f9c37bb5b420362e30 to your computer and use it in GitHub Desktop.
Incomplete starter sketch for interrupt/debounce demo
// *****************************************
// Fill in the missing parts of this sketch
// Does not currently compile
// *****************************************
#define BUTTON_PIN /* ? */
#define LED_PIN /* ?*/
// This must be declared volatile since it is accessed from
// the ISR and from loop()
//https://en.wikipedia.org/wiki/Volatile_(computer_programming)
static volatile int isr_counter = 0 ;
// ISR - execution saves state and jumps here when switch closed
void countBounces( void )
{
isr_counter ++ ;
}
void setup() {
Serial.begin( 9600 ) ;
Serial.println(F("START " __FILE__ " from " __DATE__ "\r\n" ));
pinMode( BUTTON_PIN, /* ? */ ) ;
pinMode( LED_PIN, /* ? */ ) ;
// Arduino function that sets up the ISR
// Note the use of trhe RISING keyword - why?
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), countBounces, RISING ) ;
Serial.println( "starting") ;
digitalWrite( LED_PIN, HIGH ) ;
}
void loop() {
// copy isr variable into local -- this requirement may be a bug
int count = isr_counter ;
isr_counter = 0 ;
// Loop flashing once for each detected rising edge (>1 is a bounce)
for( /* Need a completed for loop here */ )
{
// Insert code to turn LED on and off
}
delay( 500 ) ;
Serial.println( count ) ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment