#include <TimerOne.h>
#define trigger 5
#define TRIAC 13
int toggle = 0;
void setup()
{
Serial.begin(9600);
pinMode(2, INPUT); // external interrupt pin
pinMode(5, OUTPUT); // test program uses to interrupt pin 2
pinMode(13, OUTPUT); // test program indicator light
attachInterrupt(0, ISR_Triac, CHANGE); // external interrupt on pin 2
}
void ISR_Triac()
{
cli();//stop interrupts
TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B
TCNT1 = 0; //initialize counter value to 0
// Timer1.setPeriod(zak_isr); this doesn't seem to work like you said , so, using line below
OCR1A = 60000; // set time period = (16*10^6) / (1*1024) - 1 (must be <65536)
TCCR1B |= (1 << WGM12); // turn on CTC mode
TCCR1B |= (1 << CS12); // set timer1 prescaller
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
sei(); //allow interrupts
digitalWrite(13, HIGH); // turn ed on to signal start of timer1 delay
}
// #define TIMER1_COMPA_vect _VECTOR(11) /* Timer/Counter1 Compare Match A */
// Timer1 interrupt called by timer one:
// void Triac_ON()
ISR(TIMER1_COMPA_vect) // Timer1 interrupt service routine
{
// digitalWrite(TRIAC,HIGH);
// digitalWrite(TRIAC,LOW);
digitalWrite(13,LOW); // after timer1 delay, turn led off
Timer1.stop();
TIMSK1 &= (_BV( OCIE1A)); // disable timer compare interrupt
}
void loop() // toggles pin 5 when key is pressed
{
if(Serial.available() > 0)
{
Serial.read();
if(toggle == 0)
{
digitalWrite(5, HIGH);
toggle = 1;
}
else
{
digitalWrite(5, LOW);
toggle = 0;
}
}
}