/*
Prelemenary Part for buttons and LCD
2 interrupt pins working
*/
#include <FS.h>
#include <SPIFFS.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Interrupt Pins and Variables
#define BILL_COIN_PIN 12 // Change to the desired GPIO pin
#define FLOW_METER_SENSOR 13
volatile float inserted_coin_bill = 0;
volatile float flow_sensor_value = 0;
volatile float deduct_per_turn = 0;
//Variable for print if any change made
volatile bool something_changed = true;
// Product Pins
#define Product_1 14
#define Product_2 27
#define Product_3 26
bool product_1_selected, product_2_selected, product_3_selected;
// Initialize the LCD with the I2C address (usually 0x27 for 20x4 LCDs -> SDA Pin 21, SCL Pin 22)
LiquidCrystal_I2C lcd(0x27, 20, 4);
// Debounce variables
unsigned long lastDebounceTime1 = 0;
unsigned long lastDebounceTime2 = 0;
unsigned long lastDebounceTime3 = 0;
const unsigned long debounceDelay = 10; // milliseconds
// Button state variables
int lastButtonState1 = HIGH;
int lastButtonState2 = HIGH;
int lastButtonState3 = HIGH;
int buttonState1;
int buttonState2;
int buttonState3;
//price of each product
float P_1_price = 10;
float P_2_price = 10;
float P_3_price = 10;
void IRAM_ATTR handleCoinInsert() {
/*Interrupt service routine (ISR)* -> for coin slot*/
something_changed = true;
}
void IRAM_ATTR handleFlowMeter() {
/*Interrupt service routine (ISR)* -> for flow sensor*/
flow_sensor_value += 1;
something_changed = true;
}
void setup() {
Serial.begin(115200);
// Initialize the LCD
lcd.init();
lcd.backlight();
// Display a welcome message on the LCD
lcd.setCursor(0, 0);
lcd.print("Welcome!");
lcd.setCursor(0, 1);
lcd.print("Inserted Coins:");
// Configure the button pins as input with pullup resistor
pinMode(BILL_COIN_PIN, INPUT_PULLUP);
pinMode(FLOW_METER_SENSOR, INPUT_PULLUP);
pinMode(Product_1, INPUT_PULLUP);
pinMode(Product_2, INPUT_PULLUP);
pinMode(Product_3, INPUT_PULLUP);
// Attach the interrupt to the button pins, triggered on falling edge
attachInterrupt(digitalPinToInterrupt(BILL_COIN_PIN), handleCoinInsert, FALLING);
attachInterrupt(digitalPinToInterrupt(FLOW_METER_SENSOR), handleFlowMeter, FALLING);
}
void loop() {
// Debounce logic for each product button
debounceButton(Product_1, lastButtonState1, buttonState1, lastDebounceTime1, "Product 1");
debounceButton(Product_2, lastButtonState2, buttonState2, lastDebounceTime2, "Product 2");
debounceButton(Product_3, lastButtonState3, buttonState3, lastDebounceTime3, "Product 3");
print_if_changed();
lcd_print();
}
void print_if_changed() {
/*Just Print if something Change*/
if (something_changed) {
// Print the number of inserted coins to the Serial Monitor
Serial.println(inserted_coin_bill);
Serial.println(flow_sensor_value);
something_changed = false;
}
}
void debounceButton(int buttonPin, int &lastButtonState, int &buttonState,
unsigned long &lastDebounceTime, const char* productName) {
/* Debounce function */
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) {
// Product button pressed
select_product(productName);
something_changed = true;
}
}
}
lastButtonState = reading;
}
void select_product(String select) {
/*Select Product Sccording to Clicked Button*/
if (select == "Product 1") {
Serial.printf("%s selected\n", select.c_str());
product_1_selected;
} else if (select == "Product 2") {
Serial.printf("%s selected\n", select.c_str());
product_2_selected;
} else if (select == "Product 3") {
Serial.printf("%s selected\n", select.c_str());
product_3_selected;
} else {
Serial.println("Unknown product selected");
}
}
void lcd_print() {
if (something_changed) {
lcd.clear();
}
// Display a welcome message on the LCD
lcd.setCursor(0, 0);
lcd.print("Welcome!");
lcd.setCursor(0, 1);
lcd.print("Inserted Coins:");
}