Kod:
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int lcd_key = 0;
int adc_key_in = 0;
int hrs = 0;
int mins = 0;
int set_mins = 0;
int set_hrs = 1;
int secs = 60;
int cursor_pos = 1;
int buzzer_pin = 28;
int btnarmed = 53;
bool startTimer = false;
bool setTimer = true;
bool get_time = false;
bool stopTimer = true;
unsigned long interval=1000;
unsigned long previousMillis=0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnNONE 5
// read the buttons
int read_LCD_buttons()
{
adc_key_in = analogRead(0);
// checking which button is pressed
if (adc_key_in > 1000) return btnNONE;
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 250) return btnUP;
if (adc_key_in < 450) return btnDOWN;
if (adc_key_in < 650) return btnLEFT;
}
void setup()
{
Serial.begin(115200);
pinMode(buzzer_pin, OUTPUT);
pinMode (btnarmed, INPUT);
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print("Arduino airsoft");
lcd.setCursor(0, 1);
lcd.print(" BOMB");
delay(3000);
}
void loop(){
if(startTimer == true){
start_timer();
}
else if (setTimer == true){
set_timer();
}
}
void start_timer(){
if(hrs == 0 && mins == 0 && secs == 0){
lcd.setCursor(0, 0);
lcd.print(" Time is UP");
lcd.setCursor(0, 1);
lcd.print(" Game over");
digitalWrite(buzzer_pin, HIGH);
delay(1000);
digitalWrite(buzzer_pin, LOW);
delay(1000);
}
else if(secs < 0){
secs = 59;
mins = mins - 1;
}
else if(mins < 0){
mins = 59;
hrs = hrs - 1;
}
else
{
get_time = true;
counter();
lcd.setCursor(0, 0);
lcd.print("BOMB ARMED");
lcd.setCursor(0, 1);
lcd.print(hrs);
lcd.print(":");
lcd.setCursor(4, 1);
lcd.print(mins);
lcd.print(":");
lcd.setCursor(8, 1);
lcd.print(secs);
}
lcd_key = read_LCD_buttons(); // read the buttons
switch (lcd_key) // depending on which button was pushed, we perform an action
{
}
}
// This function will set the time
void set_timer(){
counter();
lcd.setCursor(0, 0);
lcd.print("Set Time");
lcd.setCursor(0, 1);
lcd.print("Hrs:");
lcd.print(hrs);
lcd.setCursor(8, 1);
lcd.print("Mins:");
lcd.print(mins);
lcd.setCursor(0,1);
lcd_key = read_LCD_buttons(); // read the buttons
switch (lcd_key) // depending on which button was pushed, we perform an action
{
// if right button is pressed, then move the cursor to minutes
case btnRIGHT:
{
cursor_pos = set_mins;
break;
}
// if left button is pressed, start the timer
case btnLEFT:
{
startTimer = true;
setTimer = false;
mins = mins - 1;
delay(300);
break;
}
// if up button is pressed, add 1 to the minutes or hours
case btnUP:
{
delay(300);
if(cursor_pos == set_mins){
mins++;
if(mins > 59){
mins = 0;
}
}
else if(cursor_pos == set_hrs){
hrs++;
if(hrs > 24){
hrs = 0;
}
}
break;
}
// if down button is pressed, minus 1 from the minutes or hours
case btnDOWN:
{
delay(300);
if(cursor_pos == set_mins){
mins--;
if(mins < 0){
mins = 60;
}
}
else if(cursor_pos == set_hrs){
hrs--;
if(hrs < 0){
hrs = 24;
}
}
break;
}
}
}
void counter() {
unsigned long currentMillis = millis(); // grab current time
// check if "interval" time has passed (1000 milliseconds)
if ((unsigned long)(currentMillis - previousMillis) >= interval) {
lcd.clear();
if(get_time == true){
secs--;
get_time = false;
}
previousMillis = millis();
}
}
óbowałem poprzez przykłady przerwania zewnętrznego ale mi to jakoś nie działa.