O Level Exam : Practical Question
📝 HTML
🎨 CSS
⚡ Java Script
#include <LiquidCrystal.h> // Initialize the library with the numbers of the interface pins LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Pin for the button const int buttonPin = 7; // Counter variable int counter = 0; // Variable to store the button state int buttonState = 0; int lastButtonState = 0; void setup() { // Set up the LCD's number of columns and rows: lcd.begin(16, 2); // Print the initial counter value lcd.print("Counter: 0"); // Set the button pin as input pinMode(buttonPin, INPUT); // Use internal pull-up resistor digitalWrite(buttonPin, HIGH); } void loop() { // Read the button state buttonState = digitalRead(buttonPin); // Check if the button is pressed if (buttonState != lastButtonState) { if (buttonState == LOW) { // Increase the counter counter++; // Clear the previous value lcd.clear(); // Print the updated counter value lcd.print("Counter: "); lcd.print(counter); } delay(50); // debounce delay } // Save the current button state for comparison in the next loop lastButtonState = buttonState; }
▶ Run Code
🖥 Output: