Hi guys,
Me and my friend want to build a little arduino based vending machine. The main problems are the lcd display and the keypad. Normally you should press a button and a product name with the corresponding price should appear. Then coins have to be inserted and the coin counter detects them and subtract the coins value from the products price (this should be seen on the lcd). When the price hits zero the product is given to the customer. We used arrays in order to make the code easier to understand. The idea behind the code is that the productIndex should help the program, so that it knows which product is being selected at all time. The button is only activated by the corresponding coin. CurrentlyThis text will be hidden the code is only for one button, in order to test it.
Code:
#include <Keypad.h>
#include <LiquidCrystal.h>
const byte ROWS= 3;
const byte COLS= 3;
char hexaKeys [ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'}
};
byte rowPins[ROWS]= {10,8,7};
byte colPins[COLS]= {6,5,4};
const int rs = 0,en = 1, d4 = 2, d5 = 3, d6 = 12, d7 = 13;
LiquidCrystal lcd( rs, en, d4, d5, d6, d7);
char customKey;
Keypad customKeypad= Keypad(makeKeymap(hexaKeys),rowPins,colPins,ROWS,COLS);
const int buttonPin1 = 22;
const int buttonPin2 = 23;
boolean lastbuttonState1;
boolean lastbuttonState2;
int produktIndex;
int verbleibenderPreis;
String produkte[]= {"Kinderriegel", "Kindercountry", "Mentos", "KitKat"};
double preise []= { 1.0, 0.80, 0.80, 0.70};
void setup() {
lcd.begin (16,2);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
char customKey = customKeypad.getKey();
}
void loop() {
if (customKey == '1'){
produktIndex = 0;
}else if (customKey == '2'){
produktIndex = 0;
}else if (customKey == '3'){
produktIndex = 0;
}else if (customKey == '4'){
produktIndex = 0;
}
if (produktIndex >= 0){
lcd.setCursor (0,0);
lcd.print ("Produkt:");
lcd.setCursor(0,10);
lcd.print( produkte[produktIndex]);
lcd.setCursor (1,0);
lcd.print ("Preis:");
lcd.setCursor(1,8);
lcd.print(preise[produktIndex]);
}
if (digitalRead (buttonPin1) ==HIGH && lastbuttonState1 == false){
lastbuttonState1 = true;
if (produktIndex>0){
verbleibenderPreis = preise[produktIndex];
produktIndex = -1;
}
verbleibenderPreis -= 0.10;
}else if( digitalRead (buttonPin1)== LOW){
lastbuttonState1 = false;
}
}`Preformatted text`