#include <LiquidCrystal.h>
//#include <string.h>
//#include <OneWireHub.h>
// Define PINs
// DIP
#define DIP_1 35
#define DIP_2 32
#define DIP_3 33
#define DIP_4 25
#define DIP_5 26
#define DIP_6 27
#define DIP_7 14
#define DIP_8 12
// Display
#define LCD_Reset 23
#define LCD_Enable 22
#define LCD_D4 19
#define LCD_D5 18
#define LCD_D6 5
#define LCD_D7 17
// Hotwire
#define HW_start 15
#define HW_end 21
#define HW 16
// 1-Wire
#define OW 15
// Buttons
#define BT1 4
#define BT2 0
#define BT3 2
// Variables
bool onlinemode;
int address;
// LCD display size
#define LCD_WIDTH 16
#define LCD_HEIGHT 2
// Initialize the LiquidCrystal object with the defined pin configuration
LiquidCrystal lcd(LCD_Reset, LCD_Enable, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Debug: Setup started");
// Set the Button pins as input
pinMode(BT1, INPUT);
pinMode(BT2, INPUT);
pinMode(BT3, INPUT);
Serial.println("Debug: Buttoninputs set");
// Set HotWire PINs
pinMode(HW_start, INPUT);
pinMode(HW_end, INPUT);
pinMode(HW, INPUT);
Serial.println("Debug: HW set as Input");
// Set the DIP pins as input
pinMode(DIP_1, INPUT);
pinMode(DIP_2, INPUT);
pinMode(DIP_3, INPUT);
pinMode(DIP_4, INPUT);
pinMode(DIP_5, INPUT);
pinMode(DIP_6, INPUT);
pinMode(DIP_7, INPUT);
pinMode(DIP_8, INPUT);
Serial.println("Debug: Reading DIPs");
if (digitalRead(DIP_1) == HIGH) {
onlinemode = true;
} else {
onlinemode = false;
}
// Read address from DIP switches DIP_2 to DIP_8
if (onlinemode == true) address = PINs_to_Int(DIP_2, DIP_3, DIP_4, DIP_5, DIP_6, DIP_7, DIP_8);
// Setup LCD
Serial.println("initializing LCD");
lcd.begin(16, 2);
LCD_Startanimation(2);
if (onlinemode == true) {
clearprint("Onlinemode: On");
delay(2000);
lcd.clear();
lcd.print("1-Wire Addr:");
lcd.setCursor(0, 1);
lcd.print(address);
delay(2000);
OneWireSetup(address, OW);
} else {
clearprint("Onlinemode: Off");
delay(2000);
}
clearprint("finished");
delay(2000);
Serial.println("Debug: Setup finished");
}
void loop() {
if (onlinemode == true) {
clearprint("To be finished");
} else {
while (true) {
int lasttime = 0;
const char* items[] = { "Start Time", "Last Time", "Shutdown" };
int menuselect = 2; //Menu(items, 3);
Serial.print("menuselect: ");
Serial.println(menuselect);
//Start Time
if (menuselect == 1) {
//Time
lasttime = Timer(HW_start, HW_end, HW);
menuselect = 2;
}
//Last Time
if (menuselect == 2) {
lcd.clear();
lcd.home();
lcd.print("Last Time:");
lcd.setCursor(0, 1);
if (lasttime == 0) {
lcd.print("Invalid 204");
} else {
lcd.print(String(lasttime) + " sec");
}
while (true) {
if(digitalRead(BT2)==HIGH){break; delay(10);}
}
}
//Shutdown
if (menuselect == 3) {
//Stop
break;
}
}
}
delay(10); // this speeds up the simulation
}
//Setup/helperfunctions
int LCD_Startanimation(int rep) {
int count = 0;
lcd.print("s");
delay(100);
lcd.clear();
lcd.print("st");
delay(100);
lcd.clear();
lcd.print("sta");
delay(100);
lcd.clear();
lcd.print("star");
delay(100);
lcd.clear();
lcd.print("start");
delay(100);
lcd.clear();
lcd.print("starti");
delay(100);
lcd.clear();
lcd.print("startin");
delay(100);
lcd.clear();
lcd.print("starting");
delay(100);
lcd.clear();
while (count != rep) {
count++;
lcd.print("starting.");
delay(100);
lcd.clear();
lcd.print("starting..");
delay(100);
lcd.clear();
lcd.print("starting...");
delay(100);
lcd.clear();
lcd.print("starting");
delay(100);
lcd.clear();
Serial.print("LCD Print Debug: Startanimation Cycle=");
Serial.print(count);
Serial.print("\n");
}
lcd.print("awaiting");
delay(10);
return 1;
}
int clearprint(const char* content) {
Serial.print("LCD Print Debug: ");
Serial.print(content);
Serial.print("\n");
lcd.clear();
lcd.print(content);
return 1;
}
int clearprint(int content) {
Serial.print("LCD Print Debug: ");
Serial.print(content);
Serial.print("\n");
lcd.clear();
lcd.print(content);
return 1;
}
int PINs_to_Int(int PIN1, int PIN2, int PIN3, int PIN4, int PIN5, int PIN6, int PIN7) {
int decimalvalue;
byte binvalue = 0000000;
if (digitalRead(PIN1) == HIGH) {
bitWrite(binvalue, 6, 1);
Serial.print(1);
} else {
Serial.print(0);
}
if (digitalRead(PIN2) == HIGH) {
bitWrite(binvalue, 5, 1);
Serial.print(1);
} else {
Serial.print(0);
}
if (digitalRead(PIN3) == HIGH) {
bitWrite(binvalue, 4, 1);
Serial.print(1);
} else {
Serial.print(0);
}
if (digitalRead(PIN4) == HIGH) {
bitWrite(binvalue, 3, 1);
Serial.print(1);
} else {
Serial.print(0);
}
if (digitalRead(PIN5) == HIGH) {
bitWrite(binvalue, 2, 1);
Serial.print(1);
} else {
Serial.print(0);
}
if (digitalRead(PIN6) == HIGH) {
bitWrite(binvalue, 1, 1);
Serial.print(1);
} else {
Serial.print(0);
}
if (digitalRead(PIN7) == HIGH) {
bitWrite(binvalue, 0, 1);
Serial.print(1);
} else {
Serial.print(0);
}
Serial.print("\n");
decimalvalue = binvalue;
return decimalvalue;
}
int WaitforBTpress() {
while (true) {
// Check if BT1 is pressed
if (digitalRead(BT1) == HIGH) {
delay(200); // Debounce delay
return 1; // Return 1 for BT1
}
// Check if BT2 is pressed
if (digitalRead(BT2) == HIGH) {
delay(200); // Debounce delay
return 2; // Return 2 for BT2
}
// Check if BT3 is pressed
if (digitalRead(BT3) == HIGH) {
delay(200); // Debounce delay
return 3; // Return 3 for BT3
}
}
return 0;
}
int OneWireSetup(int address, int PIN) {
//TODO
clearprint("1-Wire starting");
delay(1000);
return 1;
}
int Menu(const char* items[], int n_menu) {
int cursor = 1; // Start the cursor at position 1
int old_cursor = 0; // To track changes in cursor position
Serial.println("Menu function started");
// Menu runtime loop
while (true) {
// Check cursor boundaries and wrap around if needed
if (cursor == 0) {
cursor = n_menu - 1;
} else if (cursor == n_menu) {
cursor = 1;
}
Serial.print("Cursor: ");
Serial.println(cursor);
// Update the LCD only if the cursor has changed
if (cursor != old_cursor) {
Serial.println("LCD Print Debug: Menu drawn");
lcd.clear();
lcd.print("Menu");
// Calculate the position for the "(cursor/n_menu)" text
int length_of_item_index = String(cursor).length() + String(n_menu).length() + 3;
lcd.setCursor(LCD_WIDTH - length_of_item_index, 0);
lcd.print("(" + String(cursor) + "/" + String(n_menu) + ")");
// Display the current menu item on the second line
lcd.setCursor(0, 1);
lcd.print(items[cursor - 1]);
// Update the old cursor value
old_cursor = cursor;
}
// Wait for a button press and update the cursor accordingly
if (digitalRead(BT1) == HIGH) {
cursor--; // Move up in the menu
delay(200); // Debounce delay
}
if (digitalRead(BT3) == HIGH) {
cursor++; // Move down in the menu
delay(200); // Debounce delay
}
if (digitalRead(BT2) == HIGH) {
return cursor; // Select the current menu item and exit the function
}
// Small delay to avoid excessive loop executions
delay(100);
}
return 0; // Placeholder return value
}
unsigned long Timer(int PIN1, int PIN2, int PIN3) {
clearprint("Go to the start");
while (true) {
if (digitalRead(PIN1) == HIGH) {
break;
}
delay(10);
}
clearprint("Ready!!!");
while (true) {
if (digitalRead(PIN1) == HIGH) {
break;
}
delay(10);
}
unsigned long startTime = millis();
clearprint("Funny Thing");
while (true) {
if (digitalRead(PIN2) == HIGH) {
// If PIN2 goes HIGH before PIN3, abort and return 0
return 0;
}
if (digitalRead(PIN3) == HIGH) {
// If PIN3 goes HIGH, calculate and return the elapsed time
return millis() - startTime;
}
delay(1);
}
}
int RoundUp(float value) {
return (int)(value + 0.999); // Adds 0.999 to effectively round up
}