#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
volatile boolean controle = false;
volatile uint16_t tempo = 0;
void inicioContador(){
controle = true;
}
void fimContador(){
controle = false;
}
// This function will be called every time TIMER2 overflows
// and it is binded to Timer2 Overflow Interrupt Service Routine
ISR(TIMER2_OVF_vect) {
// as Arduino integers are only 16 bits but we will need to
// count the overflows until 312500 to get 5 seconds, so
// we will use two variables for that and control them as
// we need
static uint16_t overflow_count = 0;
static uint16_t segundos = 0;
if (controle){
overflow_count ++;
tempo = overflow_count * 16.384e-3 * 1000;
}
}
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
// // Set pin 13 as output
// pinMode(13, OUTPUT);
// Set pin 2 as input
pinMode(2, INPUT);
pinMode(3, INPUT);
// Attach interrupt to pin 2 through specifc function
// attachInterrupt that can only be used with Arduino
// pins that are interrupt capable (2 and 3 on the Uno)
attachInterrupt(
digitalPinToInterrupt(2), inicioContador, FALLING
);
attachInterrupt(
digitalPinToInterrupt(3), fimContador, FALLING
);
// Set TIMER2 to normal mode
// Arduino UNO has two controller registers for TIMER2: TCCR2A and TCCR2B
TCCR2A = 0b00000000; // normal operation mode
TCCR2B = 0b00000111; // /8 prescaler
// Enable TIMER2 overflow interrupt (not studied yet, but we need to enable
// it in order to see `FOC0` bit behavior). When we enable it it will be
// binded to a ISR function that we need to implement in our code
// As Timer0 can only count to 255 we need to count the times it overflows
// So we will build it in our ISR function
TIMSK2 = TIMSK2 | 0b00000001;
}
void loop() {
// we'll handle the button press outside the ISR in order to
// avoid blocking the ISR
lcd.setCursor(3, 1);
lcd.print(tempo);
}