// Circuito: cambia el estado de un LED conectado al pin46 por medio de un pulsador con antirrebote que activa la interrupcion #0 por un flanco de bajada
//Board Digital - Pins Usable For Interrupts
//Mega, Mega2560, MegaADK
#define INTERRUPTION0_PIN 2 // pin para la interrupcion #0
#define INTERRUPTION1_PIN 3
#define INTERRUPTION2_PIN 21
#define INTERRUPTION3_PIN 20
#define INTERRUPTION4_PIN 19
#define INTERRUPTION5_PIN 18 // pin para la interrupcion #5
//defines when the interrupt should be triggered. (activada)
#define low_to_high_trig RISING // to trigger when the pin goes from low to high (increasing)
#define high_to_low_trig FALLING // to trigger when the pin goes high to low (descendente)
#define change_trig CHANGE // activa la interrupción cada vez que el pin cambia de valor de 0-1 or de 1-0
#define in_low_trig LOW // to trigger the interrupt whenever the pin is low,
#define in_HIGHT_trig HIGH //to trigger the interrupt whenever the pin is high. (Arduino Due, Zero only)
// Las variables ha modificar de la funcion de interrupcion, se deben definir como VOLATILES
// La palabra clave volatile es un modificador que añadido a una variable, advierte al compilador que esta puede ser modificada por una rutina en segundo plano (background), por una rutina de interrupción, o por un dispositivo de entrada salida. Es decir, que puede sufrir modificaciones fuera del control del programa.
// La declaración de un objeto con este atributo, previene al compilador de hacer ninguna asunción sobre el valor del objeto mientras se ejecutan instrucciones en las que esté involucrado, advirtiéndolo que dicho valor puede cambiar en cualquier momento. También previene al compilador de que no debe hacer de dicho objeto una variable de registro.
// fuente http://www.zator.com/Cpp/E3_2_1d.htm
volatile int led_pin_state = HIGH;
#define LED_PIN 46
const byte PUSH_BUTTON_SWITCH = INTERRUPTION0_PIN;
void setup() {
pinMode(INTERRUPTION0_PIN, INPUT); // No hay necesidad de definirlo como entrada
attachInterrupt(digitalPinToInterrupt(INTERRUPTION0_PIN), blink_LED_ISR, high_to_low_trig);
pinMode(INTERRUPTION1_PIN, INPUT); // No hay necesidad de definirlo como entrada
attachInterrupt(digitalPinToInterrupt(INTERRUPTION1_PIN), Action2_ISR, high_to_low_trig);
pinMode(LED_PIN, OUTPUT);
// open the serial port at 9600 bps
Serial.begin(9600);
}// end setup
void loop() {
digitalWrite(LED_PIN, led_pin_state);
}// end loop
// Interrupt #0 Service Routine
void blink_LED_ISR() { // invierte el valor entre HIGH y LOW y viceversa
Serial.println("Inicia Funcion para la interrupcion #0");
led_pin_state = !led_pin_state;
Serial.println("Cambio del estado a: "+String(led_pin_state)+"\n");
}
// Interrupt #1 Service Routine
void Action2_ISR() {
Serial.println("Inicia Funcion para la interrupcion #1");
}
/*
/
PIN46: O--/\/\/\--*--|>|--*---¬
|
_|__
/ / /
1K LED GND
* +5v
|
|
>
< 1k
> pushButtom
| __|__
|
PIN2: O-- *---*---| |--*----¬
| | |
|---| (---- |
+ - |
10uF _|__
/ / /
http://www.retrojunkie.com/asciiart/electron/circuits.htm
*/