Hello. So, for two days I have been trying to make one project that includes an I2C lcd display, a slide switch and a pushbutton. So basically, every time I press the pushbutton the text on the lcd should change. But when the slide switch is on the left the lcd should turn off. So, I managed to do all of that(90 % of the code on my own) and the code works, but I dont understand how one part of it works. Here is the code:
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int button = 7;
int buttonValue = 0;
int prevButtonValue = 0;
int buttonHits = 1;
int rightSwitchSide = 4;
int rightSwitchSideValue;
void setup() {
pinMode(button, INPUT);
pinMode(rightSwitchSide, INPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(3,0);
lcd.print("CURIOSITY");
delay(2000);
lcd.setCursor(1,1);
lcd.print("NEW ERA BEGINS");
delay(2000);
lcd.clear();
}
void loop() {
buttonValue = digitalRead(button);
rightSwitchSideValue = digitalRead(rightSwitchSide);
lcds();
}
void lcds(){
if(rightSwitchSideValue == HIGH){
lcd.init();
lcd.backlight();
if(buttonValue == HIGH){ /*This is the part that I dont understand */
buttonHits = buttonHits + 1;
delay(20);
}
if(buttonHits == 1){
lcd.setCursor(1,0);
lcd.print("Hello");
}
else if(buttonHits == 2){
lcd.setCursor(1,0);
lcd.print("Friends");
}
else if(buttonHits == 3){
lcd.setCursor(1,0);
lcd.print("Mates");
}
else if(buttonHits > 3){
buttonHits = 1;
}
}
else{
lcd.noDisplay();
lcd.noBacklight();
}
}
How can that part of the code work? Couse, if I release the button, then what should happen is that buttonHits would stop being 2. .... I dont know if you understand what I am saying, but I hope you can help me figure out how why the hell this works... The code is working fine, but I don't know why xD