Currently I'm working on a vending machine project. The vending machine works using 3 button on LCD Keypad Shield DFRobot where 1 button of it assigned as 0.10 cent,the other two 0.20 cent and 0.50 cent respectively. My problem here is how can my system keep track on the balance/coin inserted so that whenever I pressed button 1 = balance 0.10
then later I pressed button 2 = balance 0.30
(because 0.20 cent added to the system). Or if you have a better idea for my code, that should be great. This is my code so far:
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
volatile float balance = 0.00;
float number1 = 0.10;
float number2 = 0.20;
float number3 = 0.50;
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // read the value from the sensor
if (adc_key_in > 1000) return btnNONE;
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
return btnNONE;
}
void setup()
{
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print("TouchNGo Machine");
}
void loop()
{
lcd.setCursor(0,1);
lcd.print("Balance=");
lcd.setCursor(10,1);
lcd_key = read_LCD_buttons();
balance = 0.00 ;
switch(lcd_key)
{
case(btnLEFT):
{
balance = balance + number1 ;
lcd.print("RM");
lcd.println(balance);
break;
}
case(btnRIGHT):
{
balance = balance + number2 ;
lcd.print("RM");
lcd.println(balance);
break;
}
case(btnUP):
{
balance = balance + number3 ;
lcd.print("RM");
lcd.println(balance);
break;
}
}
}
I would love to hear any comment from you guys.Thanks for the help!