#include <LiquidCrystal.h>
const int RS = 12;
const int EN = 11;
const int D4 = 10;
const int D5 = 8;
const int D6 = 7;
const int D7 = 6;
LiquidCrystal lcd(RS, EN, D4, D5, D6, D7);
bool isPlaying = 0; /* 0: Stop; 1: Play */
bool countDirection = 0; /* 0: Count Down; 1: Count Up */
float countValue = 0.0;
void setup() {
lcd.begin(16, 2);
if (!isPlaying) {
lcd.print("STOP");
} else {
if (countDirection) {
lcd.print("COUNT_UP");
} else {
lcd.print("COUNT_DOWN");
}
}
// Set up Timer1
noInterrupts(); // Disable all interrupts
TCCR1A = 0; // Reset TCCR1A register
TCCR1B = 0; // Reset TCCR1B register
// Set the prescaler to 1024
TCCR1B |= 0x05;
// Set the compare match value for 1s
OCR1A = 15625; //(15625 ≒ 1s / (1 / (16MHz / 1024)))
// Set the CTC mode
TCCR1B |= (1 << WGM12);
// Enable Timer1 compare match A interrupt
TIMSK1 |= (1 << OCIE1A);
interrupts(); // Enable all interrupts
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(countValue);
lcd.print("s");
}
// Timer1 compare match A interrupt service routine
ISR(TIMER1_COMPA_vect) {
countValue = 0.1;
// Your custom code here
// This code will be executed every 100ms
}
/*
Exercise 3. Stop Watch
Learning Objective
• Timer interrupt, state transition, LCD control
Task Overview
• Create a stopwatch system that uses an Arduino UNO, four buttons, and an LCD to perform count-up, countdown, and stop state transitions.
Input Specifications:
1. Push button 1 (pin 5): Count up (enabled only when stopped)
2. Push button 2 (pin 4): Countdown (enabled only while stopped)
3. Push button 3 (pin 3): stop/count reset (Accepted at any time. Count reset enabled only while stopped)
4. Push button 4 (pin 2): Reserve for next exercise
Output Specifications:
1. LCD (Pins 12, 11, 10, 8, 7, 6): the first line shows the current state, the second line shows the count value (Seconds, up to one decimal place)
Required Features:
1. When the power is turned ON, the following states and values are set.
a. Status: STOP
b. Count: 0.0 s
2. The first line of the LCD always displays the current status (STOP/COUNT_UP/COUNT_DOWN), and the second line always displays the count value (**.* s).
Sample
C O U N T _ D O W N
0 9 . 5 s
3. When push button 1 is pressed, count up is started. When the count reaches 20 seconds, the count stops (STOP).
4. When the push button 2 is pressed, the countdown is started. When the count value reaches 0 seconds, the count stops (STOP).
5. When the push button 3 is pressed during count up or count down, the count stops (STOP).
6. When the push button 3 is pressed while the count is stopped, the count is reset.
Restrictions:
• The following libraries shall be prohibited:
・ PinMode
・ DigitalRead
・ DigitalWrite
・ millis()
・ micros()
(Use port manipulations.)
Reference Applications:
• HelloWorld (under File->Examples->LiquidCrystal in Arduino IDE)
• Timer Interrupt Example
The processing to perform the interrupt accurately every 1 second is described below. You can also change the interrupt cycle by reviewing each setting.
void setup() {
// Set up Timer1
noInterrupts(); // Disable all interrupts
TCCR1A = 0; // Reset TCCR1A register
TCCR1B = 0; // Reset TCCR1B register
// Set the prescaler to 1024
TCCR1B |= 0x05;
// Set the compare match value for 1s
OCR1A = 15625; //(15625 ≒ 1s / (1 / (16MHz / 1024)))
// Set the CTC mode
TCCR1B |= (1 << WGM12);
// Enable Timer1 compare match A interrupt
TIMSK1 |= (1 << OCIE1A);
interrupts(); // Enable all interrupts
}
// Timer1 compare match A interrupt service routine
ISR(TIMER1_COMPA_vect) {
// Your custom code here
// This code will be executed every 100ms
}
*/