// K2 READY Arduino Test Code
#include <Wire.h>
#include <Keypad.h>
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;
#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
// Variables for Timing
unsigned long Cycle_Time = 180; // cycle time in seconds
unsigned long Pump_Delay = 0; // delay after cycle start until pump start in seconds
unsigned long Pump_Run_Time = 1; // pump run time after delay in seconds
unsigned long Heater_Delay = 0; // delay after cycle start until heater start in seconds
unsigned long Heater_Run_Time = 60; // heater run time after delay in seconds
unsigned long Cycle_Start = 0; // cycle start time
unsigned long Cycle_Elapsed = 0; // elapsed time in cycle in milliseconds
// Variables for outputs
int Pump_Control = 10;
int Heater_Control = 11;
// Variables for control
int Mode = 0; // 0 = Startup, 1 = Run, 2 = Prog Cycle, 3 = Prog Pump Delay, 4 = Prog Pump Time, 5 = Prog Heat Delay, 6 = Prog Heat Time
unsigned long Heat_Duty_Cycle = 80; // Heater duty cycle
unsigned long HDCycle_Time = 2000; // Heater duty cycle length in ms
unsigned long HDCycle_Start = 0; // Heater duty cycle length in ms
unsigned long HDCycle_Elapsed = 0; // Heater duty cycle length in ms
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.clear();
pinMode(Pump_Control, OUTPUT);
digitalWrite(Pump_Control, LOW);
pinMode(Heater_Control, OUTPUT);
digitalWrite(Heater_Control, LOW);
Cycle_Start = millis(); // Mark the time the cycle started
HDCycle_Start = millis(); // Mark the time the cycle started
Serial.begin(9600);
input_value.reserve(16); // maximum input characters is 17, change if needed
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
if(Mode == 0){ // Startup, display settings
lcd.setCursor(0,0);
lcd.print("CYCLE: "); lcd.print(round(Cycle_Time)); lcd.print(" s");
lcd.setCursor(0,1);
lcd.print("PUMP: "); lcd.print(round(Pump_Delay)); lcd.print(", "); lcd.print(round(Pump_Run_Time));
lcd.setCursor(0,2);
lcd.print("HEAT: "); lcd.print(round(Heater_Delay)); lcd.print(", "); lcd.print(round(Heater_Run_Time)); lcd.print(" @ "); lcd.print (Heat_Duty_Cycle); lcd.print("%");
lcd.setCursor(0,3);
lcd.print("A Run BC Pump D Mode");
char key = keypad.getKey();
if (key){
if(key == 'A') {
Mode = 1;
Cycle_Start = millis();
Cycle_Elapsed = 0;
lcd.clear();
digitalWrite(Pump_Control, LOW);
} else if (key == 'B') {
digitalWrite(Pump_Control, HIGH);
}else if (key == 'D') {
Mode = 2;
digitalWrite(Pump_Control, LOW);
lcd.clear();
} else {
digitalWrite(Pump_Control, LOW);
}
}
} else if(Mode == 1) { // Run mode
Cycle_Elapsed = (millis() - Cycle_Start); // Update how long we have been in this cycle
lcd.setCursor(14,1);lcd.print(Cycle_Elapsed);
lcd.setCursor(14,2);lcd.print(HDCycle_Elapsed < Heat_Duty_Cycle * HDCycle_Time / 100);
if(Cycle_Elapsed >= Cycle_Time*1000) { // Check to see if we need to start a new cycle
Cycle_Start = millis();
Cycle_Elapsed = 0;
lcd.clear();
}
HDCycle_Elapsed = (millis() - HDCycle_Start); // Update how long we have been in this Heater cycle
if(HDCycle_Elapsed >= HDCycle_Time) { // Check to see if we need to start a new Heater cycle
HDCycle_Start = millis();
HDCycle_Elapsed = 0;
}
// Check to see if we need Pump On and Heater On
if((Cycle_Elapsed >= Pump_Delay*1000) && (Cycle_Elapsed < (Pump_Delay*1000 + Pump_Run_Time*1000)) && (Cycle_Elapsed >= Heater_Delay*1000) && (Cycle_Elapsed < (Heater_Delay*1000 + Heater_Run_Time*1000))) {
digitalWrite(Pump_Control, HIGH);
if(HDCycle_Elapsed < Heat_Duty_Cycle * HDCycle_Time / 100){
digitalWrite(Heater_Control, HIGH); digitalWrite(LED_BUILTIN, HIGH);
} else{
digitalWrite(Heater_Control, LOW); digitalWrite(LED_BUILTIN, LOW);
}
lcd.setCursor(0,0);
lcd.print("CYCLE: "); lcd.print(round(Cycle_Elapsed/1000)); lcd.print(" of "); lcd.print(Cycle_Time);
lcd.setCursor(0,1);
lcd.print("PUMP: ON ");
lcd.setCursor(0,2);
lcd.print("HEAT: ON ");
lcd.setCursor(0,3);
lcd.print("A Stop D Mode");
}
// Check to see if we need Pump On and Heater Off
else if(((Cycle_Elapsed >= Pump_Delay*1000) && (Cycle_Elapsed < (Pump_Delay*1000 + Pump_Run_Time*1000))) && ((Cycle_Elapsed < Heater_Delay*1000) || (Cycle_Elapsed >= (Heater_Delay*1000 + Heater_Run_Time*1000)))) {
digitalWrite(Pump_Control, HIGH);
digitalWrite(Heater_Control, LOW); digitalWrite(LED_BUILTIN, LOW);
lcd.setCursor(0,0);
lcd.print("CYCLE: "); lcd.print(round(Cycle_Elapsed/1000)); lcd.print(" of "); lcd.print(round(Cycle_Time));
lcd.setCursor(0,1);
lcd.print("PUMP: ON ");
lcd.setCursor(0,2);
lcd.print("HEAT: OFF");
lcd.setCursor(0,3);
lcd.print("A Stop D Mode");
}
// Check to see if we need Pump Off and Heater On
else if(((Cycle_Elapsed < Pump_Delay*1000) || (Cycle_Elapsed >= (Pump_Delay*1000 + Pump_Run_Time*1000))) && ((Cycle_Elapsed >= Heater_Delay*1000) && (Cycle_Elapsed < (Heater_Delay*1000 + Heater_Run_Time*1000)))) {
digitalWrite(Pump_Control, LOW);
if(HDCycle_Elapsed < Heat_Duty_Cycle * HDCycle_Time / 100){
digitalWrite(Heater_Control, HIGH); digitalWrite(LED_BUILTIN, HIGH);
} else{
digitalWrite(Heater_Control, LOW); digitalWrite(LED_BUILTIN, LOW);
}
lcd.setCursor(0,0);
lcd.print("CYCLE: "); lcd.print(round(Cycle_Elapsed/1000)); lcd.print(" of "); lcd.print(round(Cycle_Time));
lcd.setCursor(0,1);
lcd.print("PUMP: OFF");
lcd.setCursor(0,2);
lcd.print("HEAT: ON ");
lcd.setCursor(0,3);
lcd.print("A Stop D Mode");
}
else { // Turn Pump Off and Heater Off
digitalWrite(Pump_Control, LOW);
digitalWrite(Heater_Control, LOW); digitalWrite(LED_BUILTIN, LOW);
lcd.setCursor(0,0);
lcd.print("CYCLE: "); lcd.print(round(Cycle_Elapsed/1000)); lcd.print(" of "); lcd.print(round(Cycle_Time));
lcd.setCursor(0,1);
lcd.print("PUMP: OFF");
lcd.setCursor(0,2);
lcd.print("HEAT: OFF");
lcd.setCursor(0,3);
lcd.print("A Stop D Mode");
}
char key = keypad.getKey();
if (key){
if(key == 'A') {
Mode = 0;
digitalWrite(Pump_Control, LOW);
digitalWrite(Heater_Control, LOW); digitalWrite(LED_BUILTIN, LOW);
lcd.clear();
}else if (key == 'D') {
Mode = 2;
digitalWrite(Pump_Control, LOW);
digitalWrite(Heater_Control, LOW); digitalWrite(LED_BUILTIN, LOW);
lcd.clear();
}
}
} else if(Mode == 2) { // Program Cycle Time
lcd.setCursor(0,0);
lcd.print("Enter Cycle Time");
lcd.setCursor(0,1);
lcd.print(Cycle_Time); 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();
Cycle_Time = input_int;
}else if (key == 'D') {
Mode = 3;
lcd.clear();
input_value = "";
} else if (key >= '0' && key <= '9') {
input_value += key; // append new character to input value string
}
}
} else if(Mode == 3) { // Program Pump Delay Time
lcd.setCursor(0,0);
lcd.print("Enter Pump Delay");
lcd.setCursor(0,1);
lcd.print(Pump_Delay); 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();
Pump_Delay = 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 Pump Run Time
lcd.setCursor(0,0);
lcd.print("Enter Pump Time");
lcd.setCursor(0,1);
lcd.print(Pump_Run_Time); 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();
Pump_Run_Time = 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 Heater Delay Time
lcd.setCursor(0,0);
lcd.print("Enter Heater Delay");
lcd.setCursor(0,1);
lcd.print(Heater_Delay); 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();
Heater_Delay = input_int;
}else if (key == 'D') {
Mode = 6;
lcd.clear();
input_value = "";
} else if (key >= '0' && key <= '9') {
input_value += key; // append new character to input value string
}
}
} else if(Mode == 6) { // Program Heater Run Time
lcd.setCursor(0,0);
lcd.print("Enter Heater Time");
lcd.setCursor(0,1);
lcd.print(Heater_Run_Time); 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();
Heater_Run_Time = input_int;
}else if (key == 'D') {
Mode = 7;
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 Heater Run Time
lcd.setCursor(0,0);
lcd.print("Enter Heater %");
lcd.setCursor(0,1);
lcd.print(Heat_Duty_Cycle); 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();
Heat_Duty_Cycle = 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
}
}
}
}