#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 22
#define output2 23
#define output3 24
#define output4 25
//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)
int outputOnTime = 500;
int mode = 0;
int totalCycle = 10;
int outputRelays = 0;
unsigned long delayAfterCycle = 10 * 1000;
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);
pinMode(output2, OUTPUT);
pinMode(output3, OUTPUT);
pinMode(output4, OUTPUT);
unsigned long startTime = millis();
}
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/1000);lcd.print("s");
//lcd.setCursor(0,3);lcd.print("Next Page:C");
lcd.setCursor(14,3);lcd.print("Mode:D");
char key = keypad.getKey();
if(key){
}if(key == 'C'){
lcd.clear();
mode = 1;
startTime = millis();
elapsed = 0;
}else if(key == 'D'){
currentTime = millis();
elapsed = 0;
mode = 3;
}else if(key == 'B'){
lcd.clear();
mode = 2;
startTime = millis();
elapsed = 0;
lcd.clear();
}
}else if(mode == 1){
lcd.setCursor(18,0);lcd.print("M");lcd.print(mode);
lcd.setCursor(0,3);lcd.print("Back Page:C");
char key = keypad.getKey();
if(key){
}if(key == 'C'){
lcd.clear();
currentTime = millis();
elapsed = 0;
mode = 0;
}else if(key == 'D'){
}else if(key == 'A'){
}
//Manual runing mode to drive output solenoids
}else if(mode == 2){
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");
elapsed = (millis() - startTime);
//lcd.setCursor(0,0);lcd.print(elapsed);
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 == 3) { // Program Cycle Time
lcd.setCursor(0,0);
lcd.print("Enter Cycle");
lcd.setCursor(0,1);
lcd.print(totalCycle); 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();
totalCycle = 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
}
}
}
}