#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int btnPlusPin = PA6;
const int btnMinusPin = PA7;
const int ledPin = PA5;
LiquidCrystal_I2C lcd(0x27, 16, 2);
int counter = 1;
const int maxCounter = 10;
const int minCounter = 0;
bool lastPlusState = HIGH;
bool lastMinusState = HIGH;
unsigned long lastDebouncePlus = 0;
unsigned long lastDebounceMinus = 0;
const int debounceDelay = 200;
unsigned long previousLedMillis = 0;
unsigned long previousLcdMillis = 0;
bool ledState = LOW;
void setup() {
pinMode(btnPlusPin, INPUT_PULLUP);
pinMode(btnMinusPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
}
void loop() {
unsigned long currentMillis = millis();
bool currentPlusState = digitalRead(btnPlusPin);
bool currentMinusState = digitalRead(btnMinusPin);
if (currentPlusState == LOW && lastPlusState == HIGH) {
if (currentMillis - lastDebouncePlus > debounceDelay) {
if (counter < maxCounter) counter++;
lastDebouncePlus = currentMillis;
}
} else if (currentPlusState == HIGH && lastPlusState == LOW) {
lastDebouncePlus = currentMillis;
}
lastPlusState = currentPlusState;
if (currentMinusState == LOW && lastMinusState == HIGH) {
if (currentMillis - lastDebounceMinus > debounceDelay) {
if (counter > minCounter) counter--;
lastDebounceMinus = currentMillis;
}
} else if (currentMinusState == HIGH && lastMinusState == LOW) {
lastDebounceMinus = currentMillis;
}
lastMinusState = currentMinusState;
if (counter > 0) {
unsigned long interval = 500 / counter;
if (currentMillis - previousLedMillis >= interval) {
previousLedMillis = currentMillis;
ledState = !ledState;
digitalWrite(ledPin, ledState);
}
} else {
digitalWrite(ledPin, LOW);
}
if (currentMillis - previousLcdMillis >= 200) {
previousLcdMillis = currentMillis;
lcd.setCursor(0, 0);
lcd.print("Cnt:");
lcd.print(counter);
if(counter < 10) lcd.print(" ");
lcd.print(" Frq:");
lcd.print(counter);
lcd.print("Hz ");
lcd.setCursor(0, 1);
lcd.print("Btn+:");
lcd.print(currentPlusState == LOW ? 1 : 0);
lcd.print(" Btn-:");
lcd.print(currentMinusState == LOW ? 1 : 0);
}
}