I'm using an OLED display connected to a push button. I am trying to get my display to show two different values independently by using the push button to switch between them. Here is a snap of my code from the loop function:
if (buttonState == HIGH) {
x=x+1;
}
if (x == 1) {
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.setTextSize(2);
display.print(T);
display.print(" degree");
display.display();
display.print(x);
}
else if (x == 2) {
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.setTextSize(2);
display.print(perventage);
display.print(" %");
display.display();
display.print(x);
}
else {
x=1;
}
delay(10);
}
x is defined as 1 initially out side the loop. What happens is that when the circuit is turned on, the screen displays the value of T then what should happen is when I push the button it switches to the second value which is percentage, and I push again to go back to T. My issue is that the Arduino doesn't response to the push button all of the time sometimes it takes 1 click and other times it takes 6 or 8 fast clicks to switch between them and it doesn't stick to a specific number of pushes to switch. What would you suggest me to change to fix it.