#include <LiquidCrystal_I2C.h>
#define buttonPin 2
#define buzzerPin 3
#define LIMIT 10
bool buttonState=0;
bool lastButtonState=0;
unsigned long lastDebounceTime=0;
int debounceDelay=50;
LiquidCrystal_I2C lcd(0x27, 16, 2);
uint8_t cas = LIMIT;
unsigned long previousMillis = 0;
bool stav=false;
uint8_t n = 0;
uint8_t k = 1;
byte customChar[8] = {
0b10101,
0b01010,
0b11111,
0b11011,
0b01110,
0b10101,
0b00100,
0b10101
};
void setup() {
pinMode(buttonPin, INPUT);
lcd.init();
lcd.backlight();
lcd.createChar(0, customChar);
zobraz();
}
void loop() {
unsigned long currentMillis = millis();
if(currentMillis-previousMillis>=1000 && stav){
previousMillis=currentMillis;
--cas;
zobraz();
if(cas==0){
tone(buzzerPin,262,200);
stav=false;
}
}
//debounce example
bool reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH && !stav) {
cas=LIMIT;
stav=true;
previousMillis=millis();
zobraz();
}
else if(buttonState == HIGH && stav){
stav=false;
tone(buzzerPin,292,200);
}
}
}
lastButtonState = reading;
}
void zobraz(){
lcd.clear();
lcd.setCursor(7,1);
String text=(cas<LIMIT?' '+String(cas):String(cas));
lcd.print(text);
lcd.setCursor(n,0);
n=n+k;
k=n==0||n==15?-k:k;
lcd.write((byte)0);
}