#include <Wire.h>
#include <Keypad.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:
// GND - GND
// VCC - 5V
// SDA - A4
// SCL - A5
const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 4; //four columns
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
// Keypad Arduino
// 1 2
// 2 3
// 3 4
// 4 5
// 5 6
// 6 7
// 7 8
// 8 9
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
String input_value;
long input_int;
//Mosfet output wires
#define output1 10
#define output2 13
#define output3 11
#define output4 12
//Output previous time
unsigned long previousOutput1Time = 0;
unsigned long previousOutput2Time = 0;
unsigned long previousOutput3Time = 0;
unsigned long previousOutput4Time = 0;
//Output stay on time 500ms(0.5s)
unsigned long outputOnTime = 500;
//delay after 1 cycle
unsigned long delayAfterCycle = 5;
//Cycle previous time
unsigned long previousCycleTime = 0;
//Timing for auto mode on and off
unsigned long outElapsed = 0;
unsigned long outStartTime = 0;
unsigned long outPreviousTime = 0;
int state = 0;
int mode = 0;
int totalCycle = 3;
int count = 0;
int solenoid = 0;
int outputRelays = 0;
unsigned long elapsed = 0;
unsigned long currentTime = 0;
unsigned long startTime = 0;
void setup() {
// put your setup code here, to run once:
lcd.init();
lcd.clear();
lcd.backlight();
Serial.begin(9600);
pinMode(output1, OUTPUT); digitalWrite(output1, LOW);
pinMode(output2, OUTPUT); digitalWrite(output2, LOW);
pinMode(output3, OUTPUT); digitalWrite(output3, LOW);
pinMode(output4, OUTPUT); digitalWrite(output4, LOW);
startTime = millis();
outStartTime = millis();
lcd.setCursor(0, 0); lcd.print("Marmon Food Service");
lcd.setCursor(3, 1); lcd.print("Build by Talha");
lcd.setCursor(0, 2); lcd.print("Contact Talha for ");
lcd.setCursor(0, 3); lcd.print("any Questions");
delay(2000);
lcd.clear();
}
void loop() {
// put your main code here, to run repeatedly:
if (mode == 0) {
lcd.setCursor(18, 0); lcd.print("M"); lcd.print(mode);
lcd.setCursor(0, 0); lcd.print("Cycle: "); lcd.print(totalCycle);
lcd.setCursor(0, 3); lcd.print("Run:A/B");
lcd.setCursor(0, 1); lcd.print("Cycle Delay: "); lcd.print(delayAfterCycle); lcd.print("s");
lcd.setCursor(0, 2); lcd.print("OutputOnDelay: "); lcd.print(outputOnTime);
//lcd.setCursor(0,3);lcd.print("Next Page:C");
lcd.setCursor(15, 3); lcd.print("Set:D");
char key = keypad.getKey();
if (key) {
} if (key == 'C') {
lcd.clear();
mode = 0;
startTime = millis();
elapsed = 0;
} else if (key == 'D') {
currentTime = millis();
elapsed = 0;
mode = 3;
lcd.clear();
} else if (key == 'B') {
lcd.clear();
mode = 2;
startTime = millis();
elapsed = 0;
lcd.clear();
} else if (key == 'A') {
lcd.clear();
mode = 1;
startTime = millis();
elapsed = 0;
outStartTime = millis();
outElapsed = 0;
}
} else if (mode == 1) {
lcd.setCursor(0, 0); lcd.print("Select OUTPUT ");
lcd.setCursor(0, 1); lcd.print("Press:1,2,3 ");
lcd.setCursor(0, 2); lcd.print("Output:1,2,3");
lcd.setCursor(0, 3); lcd.print("Exit:C");
count = 0;
char key = keypad.getKey();
if (key) {
} if (key == 'C') {
lcd.clear();
currentTime = millis();
elapsed = 0;
mode = 0;
} else if (key == '1') {
lcd.clear();
solenoid = output1;
startTime = millis();
elapsed = 0;
outStartTime = millis();
outElapsed = 0;
mode = 6;
} else if (key == '2') {
lcd.clear();
solenoid = output2;
startTime = millis();
elapsed = 0;
outStartTime = millis();
outElapsed = 0;
mode = 6;
} else if (key == '3') {
lcd.clear();
solenoid = output3;
startTime = millis();
elapsed = 0;
outStartTime = millis();
outElapsed = 0;
mode = 6;
}
//Manual runing mode to drive output solenoids
} else if (mode == 2) {
elapsed = (millis() - startTime);
//lcd.setCursor(0, 3); lcd.print(elapsed / 1000);
lcd.setCursor(5, 0); lcd.print("Manual Run"); //lcd.print(mode);
lcd.setCursor(0, 1); lcd.print("Press:123 > OUT123");
lcd.setCursor(0, 2); lcd.print("On delay Time:");
lcd.setCursor(14, 2); lcd.print(outputOnTime);
lcd.setCursor(18, 2); lcd.print("ms");
lcd.setCursor(14, 3); lcd.print("Exit:C");
if (elapsed - previousOutput1Time >= outputOnTime) {
digitalWrite(output1, LOW);
}
if (elapsed - previousOutput2Time >= outputOnTime) {
digitalWrite(output2, LOW);
}
if (elapsed - previousOutput3Time >= outputOnTime) {
digitalWrite(output3, LOW);
}
char key = keypad.getKey();
if (key) {
if (key == 'C') {
lcd.clear();
digitalWrite(output1, LOW);
digitalWrite(output2, LOW);
digitalWrite(output3, LOW);
currentTime = millis();
elapsed = 0;
mode = 0;
} else if (key == '1') {
digitalWrite(output1, HIGH);
previousOutput1Time = elapsed;
} else if (key == '2') {
digitalWrite(output2, HIGH);
previousOutput2Time = elapsed;
} else if (key == '3') {
digitalWrite(output3, HIGH);
previousOutput3Time = elapsed;
}
}
} else if (mode == 6) {
elapsed = (millis() - startTime);
//lcd.setCursor(0, 2); lcd.print(count);
if (elapsed > ((totalCycle * delayAfterCycle) * 1000)) {
/*startTime = millis();
elapsed = 0;*/
lcd.clear();
digitalWrite(output1, LOW);
digitalWrite(output2, LOW);
digitalWrite(output3, LOW);
currentTime = millis();
outStartTime = millis();
elapsed = 0;
outElapsed = 0;
mode = 7;
}
//
outElapsed = (millis() - outStartTime);
if (outElapsed > delayAfterCycle * 1000) {
outStartTime = millis();
outElapsed = 0;
//lcd.clear();
}
/*if (count == totalCycle) {
lcd.clear();
digitalWrite(output1, LOW);
digitalWrite(output2, LOW);
digitalWrite(output3, LOW);
currentTime = millis();
outStartTime = millis();
elapsed = 0;
outElapsed = 0;
mode = 1;
}*/
if (outElapsed > delayAfterCycle * 1000 - outputOnTime) {
digitalWrite(solenoid, HIGH);
lcd.setCursor(14, 0); lcd.print((elapsed / (delayAfterCycle * 1000))); lcd.print("of"); lcd.print(totalCycle);
lcd.setCursor(14, 1); lcd.print((outElapsed / 1000)); lcd.print("of"); lcd.print(delayAfterCycle);
lcd.setCursor(0, 0); lcd.print("(Auto)Cycles:"); //lcd.print(mode);
lcd.setCursor(0, 1); lcd.print("Solenoid ON ");
lcd.setCursor(0, 3); lcd.print("Exit:C Set:D");
} else {
digitalWrite(solenoid, LOW);
lcd.setCursor(14, 0); lcd.print((elapsed / (delayAfterCycle * 1000))); lcd.print("of"); lcd.print(totalCycle);
lcd.setCursor(14, 1); lcd.print((outElapsed / 1000)); lcd.print("of"); lcd.print(delayAfterCycle); lcd.print("s");
lcd.setCursor(0, 0); lcd.print("(Auto)Cycles:"); //lcd.print(mode);
lcd.setCursor(0, 1); lcd.print("Solenoid OFF");
lcd.setCursor(0, 3); lcd.print("Exit:C Set:D");
}
char key = keypad.getKey();
if (key) {
if (key == 'C') {
lcd.clear();
digitalWrite(output1, LOW);
digitalWrite(output2, LOW);
digitalWrite(output3, LOW);
currentTime = millis();
elapsed = 0;
outElapsed = 0;
mode = 0;
} else if (key == 'D') {
currentTime = millis();
elapsed = 0;
mode = 3;
lcd.clear();
}
}
} else if (mode == 3) { // Program Cycle Time
lcd.setCursor(0, 0);
lcd.print("Enter Cycle");
lcd.setCursor(0, 1);
lcd.print(totalCycle); lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print(input_value); lcd.print(" ");
lcd.setCursor(0, 3);
lcd.print("*Clr # Entr D Next");
char key = keypad.getKey();
if (key) {
if (key == '*') {
input_value = ""; // clear input value
lcd.clear();
} else if ((key == '#') && (input_value.length() > 0)) {
input_int = input_value.toInt();
totalCycle = input_int;
} else if (key == 'D') {
mode = 4;
lcd.clear();
input_value = "";
} else if (key >= '0' && key <= '9') {
input_value += key; // append new character to input value string
}
}
} else if (mode == 4) { // Program Cycle Time
lcd.setCursor(0, 0);
lcd.print("Enter Cycle Delay");
lcd.setCursor(0, 1);
lcd.print(delayAfterCycle); lcd.print(" s");
lcd.setCursor(0, 2);
lcd.print(input_value); lcd.print(" s");
lcd.setCursor(0, 3);
lcd.print("*Clr # Entr D Next");
char key = keypad.getKey();
if (key) {
if (key == '*') {
input_value = ""; // clear input value
lcd.clear();
} else if ((key == '#') && (input_value.length() > 0)) {
input_int = input_value.toInt();
delayAfterCycle = input_int;
} else if (key == 'D') {
mode = 5;
lcd.clear();
input_value = "";
} else if (key >= '0' && key <= '9') {
input_value += key; // append new character to input value string
}
}
} else if (mode == 5) { // Program Cycle Time
lcd.setCursor(0, 0);
lcd.print("Enter OutputOnDelay");
lcd.setCursor(0, 1);
lcd.print(outputOnTime); lcd.print(" ms");
lcd.setCursor(0, 2);
lcd.print(input_value); lcd.print(" ms");
lcd.setCursor(0, 3);
lcd.print("*Clr # Entr D Next");
char key = keypad.getKey();
if (key) {
if (key == '*') {
input_value = ""; // clear input value
lcd.clear();
} else if ((key == '#') && (input_value.length() > 0)) {
input_int = input_value.toInt();
outputOnTime = input_int;
} else if (key == 'D') {
mode = 0;
lcd.clear();
input_value = "";
} else if (key >= '0' && key <= '9') {
input_value += key; // append new character to input value string
}
}
}else if (mode == 7) { // Program Cycle Time
//lcd.setCursor(18, 0); lcd.print("M"); lcd.print(mode);
//lcd.setCursor(0, 0); lcd.print("Cycle: "); lcd.print(totalCycle);
//lcd.setCursor(0, 3); lcd.print("Run:A/B");
//lcd.setCursor(0, 1); lcd.print("Cycle Delay: "); lcd.print(delayAfterCycle); lcd.print("s");
//lcd.setCursor(0, 2); lcd.print("OutputOnDelay: "); lcd.print(outputOnTime);
lcd.setCursor(0,0);lcd.print(totalCycle);lcd.print(" Cycles completed ");
//lcd.setCursor(15, 3); lcd.print("Set:D");
lcd.setCursor(0,3);lcd.print("Next Page:C");
char key = keypad.getKey();
if (key) {
} if (key == 'C') {
lcd.clear();
mode = 0;
startTime = millis();
elapsed = 0;
}
}
}