#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
int startPauseButton = 4;
int stopButton = 5;
int relay = 6;
int levelOne = 8;
int levelTwo = 9;
int levelThree = 10;
int levelFour = 11;
int amountInput = 0;
int passInput = 0;
bool prevstartPauseButton = 0;
bool prevstopButton = 0;
double prevMillis = 0;
int rate = 1000;
bool startFlow = 0;
bool threshholdState = 1;
bool startPauseButtonState = 0;
bool stopButtonState = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
lcd.init();
lcd.backlight();
pinMode(startPauseButton, INPUT_PULLUP);
pinMode(stopButton, INPUT_PULLUP);
pinMode(levelOne, INPUT_PULLUP);
pinMode(levelTwo, INPUT_PULLUP);
pinMode(levelThree, INPUT_PULLUP);
pinMode(levelFour, INPUT_PULLUP);
if(digitalRead(levelOne) == LOW){
rate = 1000;
}
else if(digitalRead(levelTwo) == LOW){
rate = 500;
}
else if(digitalRead(levelThree) == LOW){
rate = 333;
}
else if(digitalRead(levelFour) == LOW){
rate = 250;
}
pinMode(relay, OUTPUT);
digitalWrite(relay, LOW);
lcdPrint("Welcome to", 2, "Gasoline Vendo", 1);
delay(3000);
EEPROM.get(0, passInput);
attachInterrupt(digitalPinToInterrupt(2), readImpulse , FALLING);
attachInterrupt(digitalPinToInterrupt(3), readImpulse , FALLING);
if(passInput > 0){
amountInput = passInput;
}
Serial.println(digitalRead(levelOne));
Serial.println(digitalRead(levelTwo));
Serial.println(digitalRead(levelThree));
Serial.println(digitalRead(levelFour));
}
void loop() {
// put your main code here, to run repeatedly:
if (threshholdState) {
if (amountInput >= 5) {
lcdPrint("Inserted Amount", 0, "P" + String(amountInput), 6);
}
else if(amountInput >= 1 && amountInput < 5){
lcdPrint("Insert More", 2, "P" + String(amountInput), 6);
}
else {
lcdPrint("Insert Coin/Bill", 0, "5/10/20/50/100", 1);
}
threshholdState = 0;
EEPROM.put(0, amountInput);
}
if (amountInput >= 5) {
startPauseButtonPressed();
while (startFlow) {
dispense();
}
}
stopButtonClear();
}
void dispense() {
while (amountInput > 0 && startFlow) {
while (millis() > (prevMillis + rate)) {
amountInput -= 1;
EEPROM.put(0, amountInput);
startPauseButtonPressed();
stopButtonPressed();
prevMillis = millis();
}
startPauseButtonPressed();
stopButtonPressed();
}
if (amountInput <= 0) {
threshholdState = 1;
amountInput = 0;
digitalWrite(relay, LOW);
startFlow = 0;
}
}
void lcdPrint(String row1, int col1, String row2, int col2) {
lcd.clear();
lcd.setCursor(col1, 0);
lcd.print(row1);
lcd.setCursor(col2, 1);
lcd.print(row2);
}
void startPauseButtonPressed() {
while (digitalRead(startPauseButton) == LOW) {
Serial.println("LOW");
if (startPauseButtonState == 0) {
startFlow = !startFlow;
Serial.print("startFlow: ");
Serial.println(startFlow);
startPauseButtonState = 1;
}
if (startFlow) {
digitalWrite(relay, HIGH);
lcdPrint("Dispensing Fuel", 0, "Please Wait", 2);
}
else {
digitalWrite(relay, LOW);
lcdPrint("Dispensing Pause", 0, "", 0);
}
delay(20);
}
startPauseButtonState = 0;
}
void stopButtonPressed() {
while (digitalRead(stopButton) == LOW) {
if (stopButtonState == 0) {
startFlow = false;
amountInput = 0;
digitalWrite(relay, LOW);
lcdPrint("Dispensing stop", 0, "", 0);
EEPROM.put(0, amountInput);
delay(5000);
threshholdState = 1;
stopButtonState = 1;
}
delay(20);
}
stopButtonState = 0;
}
void stopButtonClear(){
while (digitalRead(stopButton) == LOW) {
if (stopButtonState == 0) {
startFlow = false;
amountInput = 0;
digitalWrite(relay, LOW);
EEPROM.put(0, amountInput);
threshholdState = 1;
stopButtonState = 1;
}
delay(20);
}
stopButtonState = 0;
}
void readImpulse(){
amountInput++;
threshholdState = 1;
delay(10);
}