//https://github.com/makertut/counter-lcd-push-btn/blob/master/code1.ino for button press count code
//https://forum.arduino.cc/t/push-button-to-run-program-once/495542 For button flag code
//Compatible with the Arduino IDE 1.0
//Library version:1.1
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define ENCODER_CLK 2
#define ENCODER_DT 3
#define ENCODER_SW 4
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
// this constant won't change:
const int Up_buttonPin = 10; // the pin that the pushbutton is attached to
const int Down_buttonPin = 11;
const int Enter_buttonPin = 9;
const int FillPin = 8;
//const int ExhaustPin = 7;
const int BuzzPin = 7;
// Variables will change:
int buttonPushCounter = 35; // counter for the number of button presses
int up_buttonState = 0; // current state of the up button
int up_lastButtonState = 0; // previous state of the up button
int FlagEnter_button = 4 ; //Set initial value of EnterFlag to >3
int down_buttonState = 0; // current state of the up button
int down_lastButtonState = 0; // previous state of the up button
int SENSOR = A0;
int adcVal = 0;
float psi = map(adcVal, 0, 1023, 0, 200);
bool bPress = false;
void fillTire();
void setup()
{
lcd.init(); // initialize the lcd
lcd.backlight();
// Initialize encoder pins
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
pinMode(ENCODER_SW, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(ENCODER_CLK), readEncoder, FALLING);
Serial.begin(9600);
pinMode( Up_buttonPin , INPUT_PULLUP);
pinMode( Down_buttonPin , INPUT_PULLUP);
pinMode( Enter_buttonPin , INPUT_PULLUP);
pinMode( FillPin , OUTPUT);
//pinMode( ExhaustPin, OUTPUT);
pinMode( BuzzPin, OUTPUT);
adcVal = analogRead(SENSOR);
float psi = map(adcVal, 0, 1023, 0, 200);
Serial.println(adcVal);
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Set Pressure:");
lcd.setCursor(14,0);
lcd.print(buttonPushCounter);
lcd.setCursor(17,0);
lcd.print("PSI");
//Current Pressure LCD readout
lcd.setCursor(0,1);
lcd.print("Current Pressure:");
lcd.setCursor(0,2);
lcd.print(adcVal);
lcd.setCursor(3,2);
lcd.print("PSI");
}
void readEncoder() {
int dtValue = digitalRead(ENCODER_DT);
if (dtValue == HIGH) {
buttonPushCounter++; // Clockwise
}
if (dtValue == LOW) {
buttonPushCounter--; // Counterclockwise
}
}
// Get the counter value, disabling interrupts.
// This make sure readEncoder() doesn't change the value
// while we're reading it.
int getCounter() {
int result;
noInterrupts();
result = buttonPushCounter;
interrupts();
return result;
}
void resetCounter() {
noInterrupts();
buttonPushCounter = 35;
interrupts();
}
void loop(){
checkUp();
checkDown();
/*adcVal = analogRead(SENSOR);
float psi = map(adcVal, 0, 1023, 0, 200);
Serial.println(adcVal);
Serial.println(psi);*/
if (digitalRead(9) == LOW) {
FlagEnter_button = 0 ; // sets the value to 0
}
if (FlagEnter_button <= 3) {
//Serial.println("Enter Button Pushed");
fillTire(); //Execute operation
delay(250);
FlagEnter_button = 4 ;
//Serial.println("Enter Button Not Pushed");
}
if( bPress){
bPress = false;
//adcVal = analogRead(SENSOR);
//Serial.println(adcVal);
//Set Pressure LCD readout
lcd.setCursor(0,0);
lcd.print("Set Pressure:");
lcd.setCursor(14,0);
lcd.print(buttonPushCounter);
lcd.setCursor(17,0);
lcd.print("PSI");
//Current Pressure LCD readout
lcd.setCursor(0,1);
lcd.print("Current Pressure:");
lcd.setCursor(0,2);
lcd.print(psi);
lcd.setCursor(3,2);
lcd.print("PSI");
/*
lcd.setCursor(2,1);
lcd.print(" ");
lcd.setCursor(2,1);
lcd.print(buttonPushCounter);
*/
}
if (digitalRead(ENCODER_SW) == LOW) {
resetCounter();
}
}
void fillTire() {
adcVal = analogRead(SENSOR);
float psi = map(adcVal, 0, 1023, 0, 200);
//Set Pressure LCD readout
lcd.setCursor(0,0);
lcd.print("Set Pressure:");
lcd.setCursor(14,0);
lcd.print(buttonPushCounter);
lcd.setCursor(17,0);
lcd.print("PSI");
//Current Pressure LCD readout
lcd.setCursor(0,1);
lcd.print("Current Pressure:");
lcd.setCursor(0,2);
lcd.print(psi);
lcd.setCursor(3,2);
lcd.print("PSI");
// Setting Tire Pressure
lcd.setCursor(0,3);
lcd.print("Setting Pressure");
digitalWrite(13, HIGH);
Serial.println("Setting Pressure");
delay(5000);
digitalWrite(13, LOW);
Serial.println("Pressure Set");
tone(7, 1000, 1000);
//Pressure Set
lcd.clear();
//Set Pressure LCD readout
lcd.setCursor(0,0);
lcd.print("Set Pressure:");
lcd.setCursor(14,0);
lcd.print(buttonPushCounter);
lcd.setCursor(17,0);
lcd.print("PSI");
//Current Pressure LCD readout
lcd.setCursor(0,1);
lcd.print("Current Pressure:");
lcd.setCursor(0,2);
lcd.print(psi);
lcd.setCursor(3,2);
lcd.print("PSI");
lcd.setCursor(0,3);
lcd.print("Pressure Set");
delay(2000);
lcd.clear();
//Set Pressure LCD readout
lcd.setCursor(0,0);
lcd.print("Set Pressure:");
lcd.setCursor(14,0);
lcd.print(buttonPushCounter);
lcd.setCursor(17,0);
lcd.print("PSI");
//Current Pressure LCD readout
lcd.setCursor(0,1);
lcd.print("Current Pressure:");
lcd.setCursor(0,2);
lcd.print(psi);
lcd.setCursor(3,2);
lcd.print("PSI");
}
void checkUp(){
adcVal = analogRead(SENSOR);
up_buttonState = digitalRead(Up_buttonPin);
int SetPressure = buttonPushCounter+1;
// compare the buttonState to its previous state
if (up_buttonState != up_lastButtonState) {
// if the state has changed, increment the counter
if (up_buttonState == LOW) {
bPress = true;
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.print("Set Pressure: ");
Serial.print(SetPressure);
Serial.println("PSI");
}
else {
adcVal = analogRead(SENSOR);
float psi = map(adcVal, 0, 1023, 0, 200);
// if the current state is LOW then the button went from on to off:
//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
up_lastButtonState = up_buttonState;
}
void checkDown()
{
adcVal = analogRead(SENSOR);
float psi = map(adcVal, 0, 1023, 0, 200);
down_buttonState = digitalRead(Down_buttonPin);
int SetPressure = buttonPushCounter-1;
// compare the buttonState to its previous state
if (down_buttonState != down_lastButtonState) {
// if the state has changed, increment the counter
if (down_buttonState == LOW) {
bPress = true;
// if the current state is HIGH then the button went from off to on:
buttonPushCounter--;
Serial.print("Set Pressure: ");
Serial.print(SetPressure);
Serial.println("PSI");
} else {
adcVal = analogRead(SENSOR);
psi = map(adcVal, 0, 1023, 0, 200);
// if the current state is LOW then the button went from on to off:
//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
down_lastButtonState = down_buttonState;
adcVal = analogRead(SENSOR);
psi = map(adcVal, 0, 1023, 0, 200);
}