Created
May 24, 2025 02:33
-
-
Save CravApp/acbf53d238fc961a93e8af9b54b3fb0d to your computer and use it in GitHub Desktop.
LCD1602i2c_Dreammy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#include <LiquidCrystal_I2C.h> | |
// Define the LCD object with its I2C address, columns, and rows | |
LiquidCrystal_I2C lcd(0x27, 16, 2); | |
// Define the button pin | |
const int buttonPin = 8; // Connect your button to digital pin D8 | |
// Setup function: runs once when the Arduino starts | |
void setup() { | |
lcd.init(); // Initialize the LCD | |
lcd.backlight(); // Turn on the backlight | |
// Set the button pin as an input with an internal pull-up resistor | |
// This means the pin will be HIGH by default, and LOW when the button is pressed | |
pinMode(buttonPin, INPUT_PULLUP); | |
lcd.print("Hola Diego"); // Display "Hola Diego" | |
delay(2000); // Keep it on for 2 seconds | |
lcd.clear(); // Clear the screen | |
} | |
// Loop function: runs repeatedly after setup | |
void loop() { | |
int buttonPressCount = 0; // Variable to count button presses | |
bool lastButtonState = HIGH; // Track the previous state of the button (HIGH means not pressed) | |
lcd.print("Presiona 2 veces"); // Prompt the user to press the button | |
// Loop until two button presses are detected | |
while (buttonPressCount < 2) { | |
bool currentButtonState = digitalRead(buttonPin); // Read the current state of the button | |
// Check for a state change from HIGH to LOW (button pressed) | |
if (lastButtonState == HIGH && currentButtonState == LOW) { | |
buttonPressCount++; // Increment the counter | |
lcd.setCursor(0, 1); // Move cursor to the second line | |
lcd.print("Conteo: "); // Display "Conteo: " | |
lcd.print(buttonPressCount); // Display the current count | |
delay(50); // Small debounce delay | |
} | |
lastButtonState = currentButtonState; // Update the last button state | |
} | |
// After two presses, clear the screen and display "Buenas noches" | |
lcd.clear(); | |
lcd.print("Buenas noches"); | |
delay(5000); // Display "Buenas noches" for 5 seconds | |
lcd.clear(); // Clear the screen before repeating the loop | |
// The loop will then restart, prompting for two more presses. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment