Advertisement
Guest User

button test 1

a guest
Jun 18th, 2025
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3.  
  4. LiquidCrystal_I2C lcd(0x27, 16, 2);
  5.  
  6. #define BTN1 7      //macros because the pins arent in order with the buttons, improves readability
  7. #define BTN2 6
  8. #define BTN3 5
  9. #define BTN4 4
  10.  
  11. void setup() {
  12.   pinMode(BTN1, INPUT_PULLUP);    //set pin modes
  13.   pinMode(BTN2, INPUT_PULLUP);
  14.   pinMode(BTN3, INPUT_PULLUP);
  15.   pinMode(BTN4, INPUT_PULLUP);
  16.  
  17.   lcd.init();
  18.   lcd.backlight();
  19. }
  20.  
  21. void loop() {
  22.   while(digitalRead(BTN1) == LOW){          //also tried this with if and if else, etc...
  23.     lcd.setCursor(0,0);
  24.     lcd.print("BUTTON 1 PRESSED");          //when a button is pressed it should display text on the lcd
  25.   }
  26.   while(digitalRead(BTN2) == LOW){
  27.     lcd.setCursor(0,0);
  28.     lcd.print("BUTTON 2 PRESSED");
  29.   }
  30.   while(digitalRead(BTN3) == LOW){
  31.     lcd.setCursor(0,0);
  32.     lcd.print("BUTTON 3 PRESSED");
  33.   }
  34.   while(digitalRead(BTN4) == LOW){
  35.     lcd.setCursor(0,0);
  36.     lcd.print("BUTTON 4 PRESSED");
  37.   }
  38.   lcd.setCursor(0,0);
  39.   lcd.print("NO BUTTON PRESSED");
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement