I put 1k resistors between buttons and the pins. I fixed the LOW/HIGH confusion. After these changes the screen showed the "Press a button" text but nothing happened when I pressed the buttons neither on the screen nor the led. So I deleted the text on "else" case. Now the screen displays "2 is pressed" although I am not pressing. The led is on. But text changes to "1 is pressed" when I click the 1 button. The code is below
#include <TinyWireM.h>
#include <LiquidCrystal_I2C.h>
#define GPIO_ADDR 0x3f
LiquidCrystal_I2C lcd(GPIO_ADDR,16,2);
const int button1 = 3;
const int button2 = 4;
const int ledPin = 1;
int button1State = 0;
int button2State = 0;
void setup(){
pinMode(ledPin, OUTPUT);
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
TinyWireM.begin();
lcd.init();
lcd.backlight();
}
void loop(){
delay(50);
button1State = digitalRead(button1);
button2State = digitalRead(button2);
if(button1State==LOW){
digitalWrite(ledPin, HIGH);
lcd.clear();
lcd.print("1 is pressed");
}
else if(button2State==LOW){
digitalWrite(ledPin, HIGH);
lcd.clear();
lcd.print("2 is pressed");
}
else
digitalWrite(ledPin, LOW);
}
I was thinking that something is wrong about the screen and while digging the forum I found this post which is written by you exactly 2 years ago:) Troubles getting lcd display & i2c working with Digispark Attiny85
I already tried the library but got an error:
In file included from C:\Users....ino:2:0:
C:...\Arduino\libraries\hd44780/hd44780.h:152:77: error: '__FlashStringHelper' does not name a type
size_t attribute ((error("println() is not supported"))) println(const __FlashStringHelper *);
^
C:...\Arduino\libraries\hd44780/hd44780.h:152:97: error: ISO C++ forbids declaration of 'parameter' with no type [-fpermissive]
size_t attribute ((error("println() is not supported"))) println(const __FlashStringHelper *);
^
exit status 1
Error compiling for board Digispark (Default - 16.5mhz).