Konfig: LCD (I2C), Led (8,9,10), Pushbutton (7).
Cara kerja: Awal akan muncul "welcome", short press akan masuk ke pilihan led (1-3) dengan status nya, untuk ubah Status gunakan long press.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize the LCD with the address 0x27 (the I2C address of the LCD)
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int BUTTON_PIN = 7; // pushbutton pin
const int LED_PINS[] = {8, 9, 10}; // array of LED pins
const int NUM_LEDS = sizeof(LED_PINS) / sizeof(LED_PINS[0]); // number of LEDs
int selectedLedIndex = 0; // index of selected LED
bool ledStates[NUM_LEDS] = {false}; // states of LEDs (false: off, true: on)
unsigned long lastButtonPressTime = 0; // last time button was pressed
bool buttonPressed = false; // flag to indicate if button is pressed
const unsigned long LONG_PRESS_DURATION = 1000; // duration for a long press (milliseconds)
const unsigned long DEBOUNCE_DELAY = 50; // debounce delay (milliseconds)
void setup() {
lcd.init(); // initialize the LCD
lcd.backlight(); // turn on the backlight
// Set up pushbutton pin as input with pull-up resistor
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Set up LED pins as output
for (int i = 0; i < NUM_LEDS; ++i) {
pinMode(LED_PINS[i], OUTPUT);
}
// Display "Selamat Datang" on LCD
lcd.setCursor(0, 0);
lcd.print("Selamat Datang");
}
void loop() {
int buttonState = digitalRead(BUTTON_PIN); // read button state
// Check for button press
if (buttonState == LOW && !buttonPressed) {
lastButtonPressTime = millis();
buttonPressed = true;
}
// Check for button release
if (buttonState == HIGH && buttonPressed) {
// Check if it's a short press or long press
if (millis() - lastButtonPressTime > LONG_PRESS_DURATION) {
// Long press: select LED and turn it on/off
toggleLED(selectedLedIndex);
} else {
// Short press: move to next LED
selectedLedIndex = (selectedLedIndex + 1) % NUM_LEDS;
// Display selected LED on LCD
updateLCD();
}
buttonPressed = false; // reset button pressed flag
delay(DEBOUNCE_DELAY); // debounce delay
}
}
void toggleLED(int index) {
ledStates[index] = !ledStates[index]; // toggle LED state
digitalWrite(LED_PINS[index], ledStates[index] ? HIGH : LOW); // turn LED on or off
updateLCD(); // update LCD to show LED status
}
void updateLCD() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Pilih LED:");
lcd.setCursor(0, 1);
lcd.print("LED ");
lcd.print(selectedLedIndex + 1);
lcd.print(" ");
lcd.print(ledStates[selectedLedIndex] ? "ON" : "OFF");
}
Menggunakan EEPROM
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
// Initialize the LCD with the address 0x27 (the I2C address of the LCD)
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int BUTTON_PIN = 7; // pushbutton pin
const int LED_PINS[] = {8, 9, 10}; // array of LED pins
const int NUM_LEDS = sizeof(LED_PINS) / sizeof(LED_PINS[0]); // number of LEDs
int selectedLedIndex = 0; // index of selected LED
bool ledStates[NUM_LEDS] = {false}; // states of LEDs (false: off, true: on)
unsigned long lastButtonPressTime = 0; // last time button was pressed
bool buttonPressed = false; // flag to indicate if button is pressed
const unsigned long LONG_PRESS_DURATION = 1000; // duration for a long press (milliseconds)
const unsigned long DEBOUNCE_DELAY = 50; // debounce delay (milliseconds)
void setup() {
lcd.init(); // initialize the LCD
lcd.backlight(); // turn on the backlight
// Set up pushbutton pin as input with pull-up resistor
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Set up LED pins as output
for (int i = 0; i < NUM_LEDS; ++i) {
pinMode(LED_PINS[i], OUTPUT);
// Read LED state from EEPROM
ledStates[i] = EEPROM.read(i);
digitalWrite(LED_PINS[i], ledStates[i] ? HIGH : LOW); // update LED state
}
// Display "Selamat Datang" on LCD
lcd.setCursor(0, 0);
lcd.print("Selamat Datang");
}
void loop() {
int buttonState = digitalRead(BUTTON_PIN); // read button state
// Check for button press
if (buttonState == LOW && !buttonPressed) {
lastButtonPressTime = millis();
buttonPressed = true;
}
// Check for button release
if (buttonState == HIGH && buttonPressed) {
// Check if it's a short press or long press
if (millis() - lastButtonPressTime > LONG_PRESS_DURATION) {
// Long press: select LED and turn it on/off
toggleLED(selectedLedIndex);
} else {
// Short press: move to next LED
selectedLedIndex = (selectedLedIndex + 1) % NUM_LEDS;
// Display selected LED on LCD
updateLCD();
}
buttonPressed = false; // reset button pressed flag
delay(DEBOUNCE_DELAY); // debounce delay
}
}
void toggleLED(int index) {
ledStates[index] = !ledStates[index]; // toggle LED state
digitalWrite(LED_PINS[index], ledStates[index] ? HIGH : LOW); // turn LED on or off
// Save LED state to EEPROM
EEPROM.write(index, ledStates[index]);
updateLCD(); // update LCD to show LED status
}
void updateLCD() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Pilih LED:");
lcd.setCursor(0, 1);
lcd.print("LED ");
lcd.print(selectedLedIndex + 1);
lcd.print(" ");
lcd.print(ledStates[selectedLedIndex] ? "ON" : "OFF");
}
0 Response to "Program Arduino dengan LCD dan Pushbutton "
Posting Komentar