Skip to content

Instantly share code, notes, and snippets.

@jenschr
Created November 25, 2013 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jenschr/7642386 to your computer and use it in GitHub Desktop.
Save jenschr/7642386 to your computer and use it in GitHub Desktop.
Interrupt example - The Arduino way
/**
Interrupt example - The Arduino way
For use with Arduino UNO and other 328 Arduino's
- Connect a button to pin 2 (with 10k pulldown)
- Connect a LED to pin 9 (with 220/330 Ohm resistor)
Nov 2013, Jens Brynildsen http://flashgamer.com/arduino/
**/
byte ledPin = 9;
void setup(void)
{
Serial.begin( 9600 );
pinMode(2, INPUT);
digitalWrite(2, HIGH); // Enable pullup resistor
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
attachInterrupt(0, pin2ISR, FALLING);
}
// Normale Interrupt Service Routine in Arduino
void pin2ISR()
{
digitalWrite(13, !digitalRead(13)); // Toggle LED on pin 13
Serial.println("Interrupted!");
}
void loop(void)
{
// fade in from min to max in increments of 5 points:
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5){
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment