Created
November 25, 2013 14:49
-
-
Save jenschr/7642341 to your computer and use it in GitHub Desktop.
Interrupt example - The AVR way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Interrupt example - The AVR 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/ | |
**/ | |
#include <avr/io.h> | |
#include <avr/interrupt.h> | |
byte ledPin = 9; | |
void setup(void) | |
{ | |
Serial.begin( 9600 ); | |
pinMode(2, INPUT); | |
digitalWrite(2, HIGH); // Enable pullup resistor | |
pinMode(13, OUTPUT); | |
digitalWrite(13, LOW); | |
EICRA |= (1 << ISC01); // Trigger on falling edge | |
EIMSK |= (1 << INT0); // Enable external interrupt INT0 | |
sei(); // Enable global interrupts | |
} | |
// Interrupt Service Routine attached to INT0 vector | |
ISR(INT0_vect) | |
{ | |
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