#include <LiquidCrystal.h>
// Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // RS, E, D4, D5, D6, D7
// This constant won't change:
const int Up1_buttonPin = 2; // the pin that the pushbutton is attached to
const int Down1_buttonPin = 3;
const int Up10_buttonPin = 4;
const int Reset_buttonPin = 5;
// Variables that will change:
int Counter = 0; // counter for the number of button presses
int Up1_buttonState = 0; // current state of the up button +1
int Up1_lastButtonState = 0; // previous state of the up button +1
int Down1_buttonState = 0; // current state of the down button -1
int Down1_lastButtonState = 0; // previous state of the down button -1
int Up10_buttonState = 0; // current state of the up button +10
int Up10_lastButtonState = 0; // previous state of the up button +10
int Reset_buttonState = 0; // current state of the reset button
int Reset_lastButtonState = 0; // previous state of the reset button
bool ButtonPress = false;
void setup() {
Serial.begin(9600);
pinMode(Up1_buttonPin, INPUT_PULLUP);
pinMode(Down1_buttonPin, INPUT_PULLUP);
pinMode(Up10_buttonPin, INPUT_PULLUP);
pinMode(Reset_buttonPin, INPUT_PULLUP);
lcd.begin(16, 2); // initialize the lcd for 16 chars 2 lines
lcd.setCursor(0, 0);
lcd.print("Please Select:");
lcd.setCursor(2, 1); // position the cursor
lcd.print(Counter);
}
void loop() {
CheckUp1();
CheckDown1();
CheckUp10();
CheckReset();
if (ButtonPress) {
ButtonPress = false;
lcd.setCursor(2, 1);
lcd.print(" ");
lcd.setCursor(2, 1);
lcd.print(Counter);
}
}
void CheckUp1() {
Up1_buttonState = digitalRead(Up1_buttonPin);
// compare the buttonState to its previous state
if (Up1_buttonState != Up1_lastButtonState) {
// if the state has changed, increment the counter
if (Up1_buttonState == LOW) {
ButtonPress = true;
Counter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(Counter);
} else {
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
Up1_lastButtonState = Up1_buttonState;
}
void CheckDown1() {
Down1_buttonState = digitalRead(Down1_buttonPin);
// Compare the button state to its previous state
if (Down1_buttonState != Down1_lastButtonState) {
// if the state has changed, increment the counter
if (Down1_buttonState == LOW) {
ButtonPress = true;
Counter--;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(Counter);
} else {
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
Down1_lastButtonState = Down1_buttonState;
}
void CheckUp10() {
Up10_buttonState = digitalRead(Up10_buttonPin);
// Compare the button state to its previous state
if (Up10_buttonState != Up10_lastButtonState) {
// if the state has changed, increment the counter
if (Up10_buttonState == LOW) {
ButtonPress = true;
Counter += 10;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(Counter);
} else {
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
Up10_lastButtonState = Up10_buttonState;
}
void CheckReset() {
Reset_buttonState = digitalRead(Reset_buttonPin);
// Compare the button state to its previous state
if (Reset_buttonState != Reset_lastButtonState) {
// if the state has changed, reset the counter
if (Reset_buttonState == LOW) {
ButtonPress = true;
Counter = 0;
Serial.println("on");
Serial.print("Counter reset: ");
Serial.println(Counter);
} else {
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
Reset_lastButtonState = Reset_buttonState;
}