const int buttonPin = 2; // Tugma pinining raqami
volatile int buttonState = LOW; // Tugma holati (Low - qo'ldirilmagan, High - qo'ldirilgan)
volatile unsigned long lastDebounceTime = 0; // Oldingi vaqt
volatile unsigned long debounceDelay = 50; // Debonse ta'minlash (ms)
void setup() {
Serial.begin(9600); // Serial portni boshlash
pinMode(buttonPin, INPUT_PULLUP); // Tugma pinini kirish (pull-up rezistori bilan)
attachInterrupt(digitalPinToInterrupt(buttonPin), buttonInterrupt, FALLING); // Interruptni bog'lash (buttonPin ni FALLING bo'limida)
Serial.println("Kodingiz ishga tushdi!"); // Serial bilan habar chiqaring
}
void loop() {
// Boshqa vazifalar...
}
void buttonInterrupt() {
// Tugma bosilganda ishga tushadi
if (millis() - lastDebounceTime > debounceDelay) {
buttonState = !buttonState; // Tugmani holatini o'zgartirish
Serial.println(buttonState); // Holatni chiqarish
}
lastDebounceTime = millis(); // O'zgartirishdan so'ng vaqt olish
}