#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
// LCD Connection:
// LCD Arduino Pin
// GND GND
// VCC 5V
// SDA A4
// SCL A5
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'1','2','3', 'A'},
{'4','5','6', 'B'},
{'7','8','9', 'C'},
{'*','0','#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2};//connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
boolean PURGE = false;
boolean Valve_state;
unsigned long Start_Delay = 0; // delay from start of dispense until flavor starts injecting
unsigned long Flavor1 = 180; // flavor 1 pour time in ms
unsigned long Flavor2 = 180; // flavor 2 pour time in ms
unsigned long Flavor_Delay = 20; // space between flavor 1 and flavor 2 in ms
// Variables for outputs
int Valve1 = 10; // Flavor 1 - Red Wires
int Valve2 = 11; // Flavor 2 - Orange Wires
int Valve3 = 12; // Dispense - Brown Wires
int Valve4 = 13; // Purge - Black Wires
void setup(){
Serial.begin(9600);
Valve_state = digitalRead(Valve3); // Store initial LED state. HIGH when LED is on.
keypad.addEventListener(keypadEvent); // Add an event listener for this keypad
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("FMFV TEST - RUN MODE");
lcd.setCursor(0,1);
lcd.print("POUR: *");
lcd.setCursor(0,2);
lcd.print("PURGE: #");
/*lcd.setCursor(0,3);
lcd.print("ICE HAMMER: D ");*/
pinMode(Valve1, OUTPUT);
digitalWrite(Valve1, LOW);
pinMode(Valve2, OUTPUT);
digitalWrite(Valve2, LOW);
pinMode(Valve3, OUTPUT);
digitalWrite(Valve3, LOW);
pinMode(Valve4, OUTPUT);
digitalWrite(Valve4, LOW);
}
void loop(){
char key = keypad.getKey();
if (key) {
Serial.println(key);
}
if (PURGE){
digitalWrite(Valve3, HIGH); // Purging cycle
delay(Start_Delay);
digitalWrite(Valve1, HIGH);
delay(Flavor1);
digitalWrite(Valve1, LOW);
delay(Flavor_Delay);
digitalWrite(Valve2, HIGH);
delay(Flavor2);
digitalWrite(Valve2, LOW);
delay(Flavor_Delay);
}
}
// Taking care of some special events.
void keypadEvent(KeypadEvent key){
switch (keypad.getState()){
case PRESSED:
if (key == '#') {
digitalWrite(Valve3,!digitalRead(Valve3));
Valve_state = digitalRead(Valve3); // Remember LED state, lit or unlit.
}
break;
case RELEASED:
if (key == '*' || key == '#') { //Default ending cycle for FMFV valve to clean valve and make white cap on drink
digitalWrite(Valve3,Valve_state);
PURGE = false;
digitalWrite(Valve4, HIGH);
delay(110);
digitalWrite(Valve4, LOW);
delay(115);
digitalWrite(Valve3, LOW);
delay(160);
digitalWrite(Valve4, HIGH);
delay(88);
digitalWrite(Valve3, HIGH);
delay(112);
digitalWrite(Valve3, LOW);
delay(88);
digitalWrite(Valve4, LOW);
}
break;
case HOLD:
if (key == '*') {
PURGE = true; // Purge the product when holding the * key.
}
break;
}
}