Programming Examples

Write a program to display button press count on an LCD.


O level July 2025 practical Exam question

Write a program to display button press count on an LCD.

Solution

#include <LiquidCrystal.h>

// RS, E, D4, D5, D6, D7 pins connected to Arduino
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

#define BUTTON_PIN 7

int count = 0;
int lastButtonState = HIGH;

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  lcd.begin(16, 2);
  lcd.print("Button Count:");
}

void loop() {
  int buttonState = digitalRead(BUTTON_PIN);

  // Detect button press (HIGH to LOW transition)
  if (buttonState == LOW && lastButtonState == HIGH) {
    count++;
    lcd.setCursor(0, 1);
    lcd.print("Count: ");
    lcd.print(count);
    delay(200); // Simple debounce
  }

  lastButtonState = buttonState;
}
▶ RUN

Output/ Explanation:


Latest Current Affairs 2026 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online MBA 2 years Online MCA Online BCA Best Website and Software Company in Allahabad