#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <RotaryEncoder.h>
//---Pins & Settings Lainnya---//
const int btn_power = A1;
const int btn_mode = A2;
const int btn_pause = A3;
const int sensor1 = 2;
const int sensor2 = 3;
const int sensor3 = 4;
const int sensor4 = 5;
const int motor[4] = {9, 8, 7, 6};
const int ind_auto = A0;
const int ind_manual = 13;
#define CLK 12 // Pin untuk CLK
#define DT 11 // Pin untuk DT
#define SW 10 // Pin untuk tombol
byte token_eeprom = 0x50;
#define hidup LOW
float waktuJeda[4] = {1, 1, 1, 1}; //waktu Jeda sebelum mulai dalam detik
//---Variable Lainnya yang Digunakan---//
struct Time {
bool start[4];
unsigned long mulai[4];
unsigned long selesai[4];
unsigned long Stopwatch[4];
unsigned long pause[4];
int detik[4];
int miliDetik[4];
int setDetik[4] = {0, 0, 0, 0};
int setMiliDetik[4] = {0, 0, 0, 0};
};
Time time;
String mode_alat = "utama";
char buff[4][10];
bool automatic = true;
bool power_status = false;
bool mode_pause = false;
bool once_siklus[4];
bool reset_auto[4];
bool wait_power[4];
bool wait_menu[4];
bool wait_sensor[4];
bool set_data = false;
bool set_all = false;
int count_subMenu = 0;
int current_menu = 0;
int noCH = 0;
unsigned long ms_indikator, ms_jeda[4];
//Variable Menus
//Define Main menu and sub menus...
#define MainMenu 0 //MainMenu
#define SubMenu1 1 //GameMenu
#define SubMenu2 2 //DifficultyMenu
#define SubMenu3 3 //LevelMenu
#define SubMenu4 4 //LightMenu
#define SubMenu5 5 //There is no SubMenu5...it is used for volume
#define SubMenu6 6 //LanguageMenu
#define SubMenu7 7 //Exit
const int maxItemSize = 18;
int result = 0;
int cursorLine = 0;
int displayFirstLine = 0;
//Define Menu Arrays
char startMenu[][maxItemSize] = {"Set Timer Motor 1", "Set Timer Motor 2", "Set Timer Motor 3", "Set Timer Motor 4", "Set All Tmr Manual", "Reset Timer", "Reset All data Tmr", "Exit"};
const int itemsPerScreen = 4; // one less than max rows..
int menuItems;
char *menuSelected = NULL;
int menuOption = 0;
//bool select_menu = false;
const int SHORT_PRESS_TIME = 500; // 500 milliseconds
// Variables will change:
int lastState[4] = {LOW, LOW, LOW, LOW}; // the previous state from the input pin
int currentState[4]; // the current reading from the input pin
unsigned long pressedTime[4];
unsigned long releasedTime[4];
//---Mendefinisikan Library---//
LiquidCrystal_I2C lcd(0x27, 20, 4);
RotaryEncoder encoder(DT, CLK, RotaryEncoder::LatchMode::FOUR3);
void setup() {
Serial.begin(9600);
EEPROM.begin();
Wire.beginTransmission(0x3F);
if (Wire.endTransmission()) {
lcd = LiquidCrystal_I2C(0x27, 20, 4);
}
lcd.begin(20,4);
lcd.backlight();
pinMode(SW, INPUT);
pinMode(btn_power, INPUT_PULLUP);
pinMode(btn_mode, INPUT_PULLUP);
pinMode(btn_pause, INPUT_PULLUP);
pinMode(sensor1, INPUT_PULLUP);
pinMode(sensor2, INPUT_PULLUP);
pinMode(sensor3, INPUT_PULLUP);
pinMode(sensor4, INPUT_PULLUP);
for (int i = 0; i < 4; i++) {
pinMode(motor[i], OUTPUT);
digitalWrite(motor[i], !hidup);
}
pinMode(ind_auto, OUTPUT);
pinMode(ind_manual, OUTPUT);
digitalWrite(ind_auto, LOW);
digitalWrite(ind_manual, LOW);
default_settings();
//menuItems = sizeof(startMenu) / sizeof(startMenu[0]);
menuItems = sizeof startMenu / sizeof * startMenu - 1; //-1 digunakan untuk mencegah error text
menuSelected = &startMenu[0][0]; //Start Menu
menuOption = MainMenu; //Main Menu
lcd.setCursor(0, 0);
lcd.print(F(" FILLING MACHINE"));
lcd.setCursor(0, 1);
lcd.print(F(" WELLCOME GARLISE"));
delay(1500);
lcd.clear();
delay(15);
for (int i = 0; i < 4; i++) {
sprintf(buff[i], "%03d:%02d", time.setDetik[i], time.setMiliDetik[i]);
lcd.setCursor(0, i);
lcd.print(F("M"));
lcd.print(i + 1);
lcd.print(F(":000:00 ("));
lcd.print(buff[i]);
lcd.print(F(")"));
//delay(100);
}
}
void loop() {
encoder.tick();
display_print();
cek_timer();
cek_btn();
}
void display_print() {
if (mode_alat == "utama") {
if (time.start[0]) {
if (automatic) {
if (!reset_auto[0]) {
sprintf(buff[0], "%03d:%02d", time.detik[0], time.miliDetik[0]);
lcd.setCursor(0, 0);
lcd.print("M1:");
lcd.print(buff[0]);
}
}
else {
//Print Timer Manual Motor 1
sprintf(buff[0], "%03d:%02d", time.detik[0], time.miliDetik[0]);
lcd.setCursor(0, 0);
lcd.print("M1:");
lcd.print(buff[0]);
}
}
if (time.start[1]) {
if (automatic) {
if (!reset_auto[1]) {
sprintf(buff[1], "%03d:%02d", time.detik[1], time.miliDetik[1]);
lcd.setCursor(0, 1);
lcd.print("M2:");
lcd.print(buff[1]);
}
}
else {
//Print Timer Manual Motor 2
sprintf(buff[1], "%03d:%02d", time.detik[1], time.miliDetik[1]);
lcd.setCursor(0, 1);
lcd.print("M2:");
lcd.print(buff[1]);
}
}
if (time.start[2]) {
if (automatic) {
if (!reset_auto[2]) {
sprintf(buff[2], "%03d:%02d", time.detik[2], time.miliDetik[2]);
lcd.setCursor(0, 2);
lcd.print("M3:");
lcd.print(buff[2]);
}
}
else {
//Print Timer Manual Motor 3
sprintf(buff[2], "%03d:%02d", time.detik[2], time.miliDetik[2]);
lcd.setCursor(0, 2);
lcd.print("M3:");
lcd.print(buff[2]);
}
}
if (time.start[3]) {
if (automatic) {
if (!reset_auto[3]) {
sprintf(buff[3], "%03d:%02d", time.detik[3], time.miliDetik[3]);
lcd.setCursor(0, 3);
lcd.print("M4:");
lcd.print(buff[3]);
}
}
else {
//Print Timer Manual Motor 4
sprintf(buff[3], "%03d:%02d", time.detik[3], time.miliDetik[3]);
lcd.setCursor(0, 3);
lcd.print("M4:");
lcd.print(buff[3]);
}
}
}
else if (mode_alat == "menu") {
if (menuOption >= 1 && menuOption <= 4) {
if (current_menu != count_subMenu) {
current_menu = count_subMenu;
lcd.setCursor(0, 0);
lcd.print(F("Set Timer Motor "));
lcd.print(noCH + 1);
lcd.print(F(":"));
sprintf(buff[noCH], "%03d:%02d", time.setDetik[noCH], time.setMiliDetik[noCH]);
lcd.setCursor(4, 1);
lcd.print(buff[noCH]);
lcd.setCursor(0, 2);
lcd.print(F(" "));
//lcd.setCursor(0, 3);
//lcd.print(F(" "));
if (count_subMenu != 1 or count_subMenu != 4) {
lcd.setCursor(0, 3);
lcd.print(F("Batal "));
lcd.setCursor(12, 3);
lcd.print(F(" Simpan"));
}
if (count_subMenu == 1) {
lcd.setCursor(0, 3);
lcd.print(F("Batal<<"));
}
else if (count_subMenu == 2) {
lcd.setCursor(4, 2);
if (set_data) {
lcd.print(F("^"));
}
else {
lcd.print(F("^^^"));
}
}
else if (count_subMenu == 3) {
lcd.setCursor(8, 2);
if (set_data) {
lcd.print(F("^"));
}
else {
lcd.print(F("^^"));
}
}
else if (count_subMenu == 4) {
lcd.setCursor(12, 3);
lcd.print(F(">>Simpan"));
}
}
}
}
}
void displayUpdate(const char *menuInput, int ROWS, int COLS) {
int n = 4; //4 rows
lcd.clear();
//lcd.setCursor(0, 0);
//lcd.print(" Menu ");
if (ROWS < n - 1) {
n = ROWS + 1;
}
for (int i = 0; i < n; i++) {
lcd.setCursor(1, i); //(col, row)
for (int j = 0; j < COLS; j++) {
if (*(menuInput + ((displayFirstLine + i) * COLS + j)) != '\0') {
lcd.print(*(menuInput + ((displayFirstLine + i) * COLS + j)));
}//end if
}//end for j
}//end for i
lcd.setCursor(0, (cursorLine - displayFirstLine));
//lcd.print("<");
lcd.print(F(">"));
}//end display_menu
void chooseMenu() {
displayUpdate(menuSelected, menuItems, maxItemSize);
}//end chooseMenu
void selectionMainMenu() {
lcd.clear();
switch (cursorLine) {
case 0:
menuOption = SubMenu1;
noCH = menuOption - 1;
current_menu = 0;
count_subMenu = 1;
break;
case 1:
menuOption = SubMenu2;
noCH = menuOption - 1;
current_menu = 0;
count_subMenu = 1;
break;
case 2:
menuOption = SubMenu3;
noCH = menuOption - 1;
current_menu = 0;
count_subMenu = 1;
break;
case 3:
menuOption = SubMenu4;
noCH = menuOption - 1;
current_menu = 0;
count_subMenu = 1;
break;
case 4:
if (!automatic) {
for (int i = 0; i < 4; i++) {
EEPROMWriteint((1 + (i * 3)), time.detik[i]);
EEPROM.write(15 + i, time.miliDetik[i]);
}
lcd.clear();
delay(50);
eeprom_read();
lcd.setCursor(0, 1);
lcd.print(F("Menyimpan data"));
for (int i = 0; i < 3; i++) {
lcd.print(F("."));
delay(400);
}
lcd.clear();
delay(50);
set_all = true;
}
else {
lcd.setCursor(2, 1);
lcd.print(F(" Set ke Manual "));
lcd.setCursor(2, 2);
lcd.print(F("Terlebih dahulu"));
delay(1200);
}
returnToMainMenu();
chooseMenu();
break;
case 5:
for (int i = 0; i < 4; i++) {
once_siklus[i] = false;
reset_auto[i] = false;
time.pause[i] = 0;
time.start[i] = false;
time.detik[i] = 0;
time.miliDetik[i] = 0;
// sprintf(buff[i], "%03d:%02d", time.setDetik[i], time.setMiliDetik[i]);
// lcd.setCursor(0, i);
// lcd.print(F("M"));
// lcd.print(i + 1);
// lcd.print(F(":000:00 ("));
// lcd.print(buff[i]);
// lcd.print(F(")"));
}
lcd.setCursor(2, 1);
lcd.print(F(" Timer Reset "));
delay(1000);
returnToMainMenu();
chooseMenu();
break;
case 6:
for (int i = 0; i < 4; i++) {
once_siklus[i] = false;
reset_auto[i] = false;
time.pause[i] = 0;
time.start[i] = false;
time.detik[i] = 0;
time.miliDetik[i] = 0;
time.setDetik[i] = 0;
time.setMiliDetik[i] = 0;
EEPROMWriteint((1 + (i * 3)), time.setDetik[i]);
EEPROM.write(15 + i, time.setMiliDetik[i]);
}
delay(50);
eeprom_read();
lcd.setCursor(0, 1);
lcd.print(F("Menyimpan data"));
for (int i = 0; i < 3; i++) {
lcd.print(F("."));
delay(400);
}
lcd.clear();
delay(50);
returnToMainMenu();
chooseMenu();
break;
case 7:
displayFirstLine = 0; //initialize display_menu to 1st line
cursorLine = 0;
menuItems = sizeof startMenu / sizeof * startMenu - 1; //-1 digunakan untuk mencegah error text
menuSelected = &startMenu[0][0]; //Start Menu
menuOption = MainMenu; //Main Menu
for (int i = 0; i < 4; i++) {
lcd.setCursor(0, i);
if (automatic) {
if (wait_menu[i]) {
time.detik[i] = 0;
time.miliDetik[i] = 0;
}
}
lcd.print(F("M"));
lcd.print(i + 1);
lcd.print(F(":"));
sprintf(buff[i], "%03d:%02d", time.detik[i], time.miliDetik[i]);
lcd.print(buff[i]);
lcd.print(F(" ("));
sprintf(buff[i], "%03d:%02d", time.setDetik[i], time.setMiliDetik[i]);
lcd.print(buff[i]);
lcd.print(F(")"));
//delay(100);
}
mode_alat = "utama";
}//end switch
}//end selectionMainMenu
void cek_btn() {
currentState[3] = digitalRead(SW);
//Button Power
if (lastState[3] == HIGH && currentState[3] == LOW) { // button is pressed
pressedTime[3] = millis();
}
else if (lastState[3] == LOW && currentState[3] == HIGH) { // button is released
releasedTime[3] = millis();
long pressDuration3 = releasedTime[3] - pressedTime[3];
if ( pressDuration3 < SHORT_PRESS_TIME ) {
Serial.println("A short press is detected 1");
if (mode_alat == "utama") {
displayFirstLine = 0; //initialize display_menu to 1st line
cursorLine = 0;
//menuItems = sizeof(startMenu) / sizeof(startMenu[0]);
menuItems = sizeof startMenu / sizeof * startMenu - 1; //-1 digunakan untuk mencegah error text
menuSelected = &startMenu[0][0]; //Start Menu
menuOption = MainMenu; //Main Menu
chooseMenu();
power_status = false;
digitalWrite(ind_auto, LOW);
digitalWrite(ind_manual, LOW);
for (int i = 0; i < 4; i++) {
digitalWrite(motor[i], !hidup);
if (automatic) {
if (!reset_auto[i] && !wait_power[i]) {
time.pause[i] = time.Stopwatch[i];
time.start[i] = false;
once_siklus[i] = false;
wait_menu[i] = false;
}
else {
once_siklus[i] = false;
reset_auto[i] = false;
time.pause[i] = 0;
time.start[i] = false;
wait_menu[i] = true;
}
}
else {
reset_auto[i] = false;
time.pause[i] = time.Stopwatch[i];
time.start[i] = false;
once_siklus[i] = false;
}
}
mode_pause = false;
mode_alat = "menu";
}
else if (mode_alat == "menu") {
if (menuOption == MainMenu) {
selectionMainMenu();
}
else {
if (menuOption >= 1 && menuOption <= 4) {
if (count_subMenu <= 3) {
if (count_subMenu >= 2) {
if (set_data) {
set_data = false;
}
else {
set_data = true;
}
current_menu = 0;
}
else {
eeprom_read();
returnToMainMenu();
chooseMenu();
}
}
else {
EEPROMWriteint((1 + (noCH * 3)), time.setDetik[noCH]);
EEPROM.write(15 + noCH, time.setMiliDetik[noCH]);
lcd.clear();
delay(50);
eeprom_read();
lcd.setCursor(0, 1);
lcd.print(F("Menyimpan data"));
for (int i = 0; i < 3; i++) {
lcd.print(F("."));
delay(400);
}
lcd.clear();
delay(50);
returnToMainMenu();
chooseMenu();
}
}
}
}
delay(200);
}
}
lastState[3] = currentState[3];
if (mode_alat == "utama") {
// read the state of the switch/button:
currentState[0] = digitalRead(btn_power);
currentState[1] = digitalRead(btn_mode);
currentState[2] = digitalRead(btn_pause);
//Button Power
if (lastState[0] == HIGH && currentState[0] == LOW) { // button is pressed
pressedTime[0] = millis();
}
else if (lastState[0] == LOW && currentState[0] == HIGH) { // button is released
releasedTime[0] = millis();
long pressDuration1 = releasedTime[0] - pressedTime[0];
if ( pressDuration1 < SHORT_PRESS_TIME ) {
Serial.println("A short press is detected 1");
//automatic = !automatic;
power_status = !power_status;
if (power_status) {
if (automatic) {
digitalWrite(ind_auto, HIGH);
digitalWrite(ind_manual, LOW);
}
else {
digitalWrite(ind_auto, LOW);
digitalWrite(ind_manual, HIGH);
}
if (set_all) {
if (automatic) {
for (int i = 0; i < 4; i++) {
once_siklus[i] = false;
reset_auto[i] = false;
time.pause[i] = 0;
time.start[i] = false;
time.detik[i] = 0;
time.miliDetik[i] = 0;
lcd.setCursor(0, i);
lcd.print(F("M"));
lcd.print(i + 1);
sprintf(buff[i], ":%03d:%02d", time.detik[i], time.miliDetik[i]);
lcd.print(buff[i]);
lcd.print(F(" ("));
sprintf(buff[i], "%03d:%02d", time.setDetik[i], time.setMiliDetik[i]);
lcd.print(buff[i]);
lcd.print(F(")"));
}
set_all = false;
}
}
}
else {
mode_pause = false;
digitalWrite(ind_auto, LOW);
digitalWrite(ind_manual, LOW);
//Timer1
for (int i = 0; i < 4; i++) {
digitalWrite(motor[i], !hidup);
if (automatic) {
if (!reset_auto[i] && !wait_power[i]) {
time.pause[i] = time.Stopwatch[i];
time.start[i] = false;
once_siklus[i] = false;
}
else {
once_siklus[i] = false;
reset_auto[i] = false;
time.pause[i] = 0;
time.start[i] = false;
time.detik[i] = 0;
time.miliDetik[i] = 0;
lcd.setCursor(0, i);
lcd.print(F("M"));
lcd.print(i + 1);
sprintf(buff[i], ":%03d:%02d", time.detik[i], time.miliDetik[i]);
lcd.print(buff[i]);
lcd.print(F(" ("));
sprintf(buff[i], "%03d:%02d", time.setDetik[i], time.setMiliDetik[i]);
lcd.print(buff[i]);
lcd.print(F(")"));
}
}
else {
reset_auto[i] = false;
time.pause[i] = time.Stopwatch[i];
time.start[i] = false;
once_siklus[i] = false;
}
}
}
delay(200);
}
}
//Button Mode
if (lastState[1] == HIGH && currentState[1] == LOW) { // button is pressed
pressedTime[1] = millis();
}
else if (lastState[1] == LOW && currentState[1] == HIGH) { // button is released
releasedTime[1] = millis();
long pressDuration2 = releasedTime[1] - pressedTime[1];
if ( pressDuration2 < SHORT_PRESS_TIME ) {
Serial.println("A short press is detected 2");
if (!time.start[0] && !time.start[1] && !time.start[2] && !time.start[3]) {
if (power_status) {
automatic = !automatic;
if (set_all) {
if (automatic) {
for (int i = 0; i < 4; i++) {
once_siklus[i] = false;
reset_auto[i] = false;
time.pause[i] = 0;
time.start[i] = false;
time.detik[i] = 0;
time.miliDetik[i] = 0;
lcd.setCursor(0, i);
lcd.print(F("M"));
lcd.print(i + 1);
sprintf(buff[i], ":%03d:%02d", time.detik[i], time.miliDetik[i]);
lcd.print(buff[i]);
lcd.print(F(" ("));
sprintf(buff[i], "%03d:%02d", time.setDetik[i], time.setMiliDetik[i]);
lcd.print(buff[i]);
lcd.print(F(")"));
}
set_all = false;
}
}
if (automatic) {
digitalWrite(ind_auto, HIGH);
digitalWrite(ind_manual, LOW);
for (int i = 0; i < 4; i++) {
if (reset_auto[i]) {
reset_auto[i] = false;
time.pause[i] = 0;
time.start[i] = false;
time.detik[i] = 0;
time.miliDetik[i] = 0;
lcd.setCursor(0, i);
lcd.print(F("M"));
lcd.print(i + 1);
sprintf(buff[i], ":%03d:%02d", time.detik[i], time.miliDetik[i]);
lcd.print(buff[i]);
lcd.print(F(" ("));
sprintf(buff[i], "%03d:%02d", time.setDetik[i], time.setMiliDetik[i]);
lcd.print(buff[i]);
lcd.print(F(")"));
}
}
}
else {
digitalWrite(ind_auto, LOW);
digitalWrite(ind_manual, HIGH);
}
}
delay(200);
}
}
}
//Button Pause
if (lastState[2] == HIGH && currentState[2] == LOW) { // button is pressed
pressedTime[2] = millis();
}
else if (lastState[2] == LOW && currentState[2] == HIGH) { // button is released
releasedTime[2] = millis();
long pressDuration3 = releasedTime[2] - pressedTime[2];
if ( pressDuration3 < SHORT_PRESS_TIME ) {
Serial.println("A short press is detected 3");
if (power_status) {
mode_pause = !mode_pause;
if (!mode_pause) {
if (automatic) {
digitalWrite(ind_auto, HIGH);
digitalWrite(ind_manual, LOW);
}
else {
digitalWrite(ind_auto, LOW);
digitalWrite(ind_manual, HIGH);
}
}
else {
ms_indikator = millis();
}
//Timer1
for (int i = 0; i < 4; i++) {
digitalWrite(motor[i], !hidup);
if (automatic) {
if (!reset_auto[i] && !wait_power[i]) {
time.pause[i] = time.Stopwatch[i];
time.start[i] = false;
once_siklus[i] = false;
}
else {
once_siklus[i] = false;
reset_auto[i] = false;
time.pause[i] = 0;
time.start[i] = false;
time.detik[i] = 0;
time.miliDetik[i] = 0;
lcd.setCursor(0, i);
lcd.print(F("M"));
lcd.print(i + 1);
sprintf(buff[i], ":%03d:%02d", time.detik[i], time.miliDetik[i]);
lcd.print(buff[i]);
lcd.print(F(" ("));
sprintf(buff[i], "%03d:%02d", time.setDetik[i], time.setMiliDetik[i]);
lcd.print(buff[i]);
lcd.print(F(")"));
}
}
else {
reset_auto[i] = false;
time.pause[i] = time.Stopwatch[i];
time.start[i] = false;
once_siklus[i] = false;
}
}
delay(200);
}
}
}
// save the the last state
lastState[0] = currentState[0];
lastState[1] = currentState[1];
lastState[2] = currentState[2];
}
else if (mode_alat == "menu") {
static int pos = 0;
encoder.tick();
int newPos = encoder.getPosition();
if (pos != newPos) {
// Cek arah putaran
if (newPos > pos) {
// Putaran ke kanan
if (menuOption == MainMenu) {
if (cursorLine == (displayFirstLine + itemsPerScreen - 1)) {
displayFirstLine++;
}
//If reached last item...roll over to first item
if (cursorLine == menuItems) {
cursorLine = 0;
displayFirstLine = 0;
} else {
cursorLine = cursorLine + 1;
}
chooseMenu();
}
else {
if (!set_data) {
if (count_subMenu >= 4) {
count_subMenu = 4;
}
else {
count_subMenu++;
}
}
else {
if (menuOption >= 1 && menuOption <= 4) {
if (count_subMenu == 2) {
if (time.setDetik[noCH] >= 999) {
time.setDetik[noCH] = 0;
}
else {
time.setDetik[noCH]++;
}
}
else if (count_subMenu == 3) {
if (time.setMiliDetik[noCH] >= 99) {
time.setMiliDetik[noCH] = 0;
}
else {
time.setMiliDetik[noCH]++;
}
}
current_menu = 0;
}
}
}
}
else {
// Putaran ke kiri
if (menuOption == MainMenu) {
if ((displayFirstLine == 0) & (cursorLine == 0)) {
if (menuItems > itemsPerScreen - 1) {
displayFirstLine = menuItems - itemsPerScreen + 1;
}
} else if (displayFirstLine == cursorLine) {
displayFirstLine--;
}
if (cursorLine == 0) {
//Untuk Mencegah Lopping Jika Item per Screen kuarng dari Maximal
//if (menuItems > itemsPerScreen - 1) {
cursorLine = menuItems; //roll over to last item
//}
} else {
cursorLine = cursorLine - 1;
}
chooseMenu();
}
else {
if (!set_data) {
if (count_subMenu <= 1) {
count_subMenu = 1;
}
else {
count_subMenu--;
}
}
else {
if (menuOption >= 1 && menuOption <= 4) {
if (count_subMenu == 2) {
if (time.setDetik[noCH] <= 0) {
time.setDetik[noCH] = 999;
}
else {
time.setDetik[noCH]--;
}
}
else if (count_subMenu == 3) {
if (time.setMiliDetik[noCH] <= 0) {
time.setMiliDetik[noCH] = 99;
}
else {
time.setMiliDetik[noCH]--;
}
}
current_menu = 0;
}
}
}
}
pos = newPos;
}
}
}
void cek_timer() {
if (mode_alat == "utama") {
if (power_status) {
if (mode_pause) {
if (millis() - ms_indikator >= 500) {
if (automatic) {
digitalWrite(ind_auto, !digitalRead(ind_auto));
}
else {
digitalWrite(ind_manual, !digitalRead(ind_manual));
}
ms_indikator = millis();
}
}
else {
//Sensor 1
if (digitalRead(sensor1) == LOW) {
if (!once_siklus[0]) {
if (automatic) {
if (!wait_sensor[0]) {
ms_jeda[0] = millis();
wait_sensor[0] = true;
}
if (millis() - ms_jeda[0] >= waktuJeda[0] * 1000) {
if (time.detik[0] >= time.setDetik[0] && time.miliDetik[0] >= time.setMiliDetik[0]) {
}
else {
digitalWrite(motor[0], hidup);
time.start[0] = true;
time.mulai[0] = millis();
once_siklus[0] = true;
wait_power[0] = false;
wait_sensor[0] = false;
}
}
}
else {
digitalWrite(motor[0], hidup);
time.start[0] = true;
time.mulai[0] = millis();
once_siklus[0] = true;
wait_power[0] = false;
}
}
}
else {
if (once_siklus[0]) {
digitalWrite(motor[0], !hidup);
if (automatic && reset_auto[0]) {
reset_auto[0] = false;
time.pause[0] = 0;
time.detik[0] = 0;
time.miliDetik[0] = 0;
lcd.setCursor(0, 0);
lcd.print(F("M"));
lcd.print(1);
sprintf(buff[0], ":%03d:%02d", time.detik[0], time.miliDetik[0]);
lcd.print(buff[0]);
lcd.print(F(" ("));
sprintf(buff[0], "%03d:%02d", time.setDetik[0], time.setMiliDetik[0]);
lcd.print(buff[0]);
lcd.print(F(")"));
}
else {
time.pause[0] = time.Stopwatch[0];
}
time.start[0] = false;
once_siklus[0] = false;
}
}
//Sensor 2
if (digitalRead(sensor2) == LOW) {
if (!once_siklus[1]) {
if (automatic) {
if (!wait_sensor[1]) {
ms_jeda[1] = millis();
wait_sensor[1] = true;
}
if (millis() - ms_jeda[1] >= waktuJeda[1] * 1000) {
if (time.detik[1] >= time.setDetik[1] && time.miliDetik[1] >= time.setMiliDetik[1]) {
}
else {
digitalWrite(motor[1], hidup);
time.start[1] = true;
time.mulai[1] = millis();
once_siklus[1] = true;
wait_power[1] = false;
wait_sensor[1] = false;
}
}
}
else {
digitalWrite(motor[1], hidup);
time.start[1] = true;
time.mulai[1] = millis();
once_siklus[1] = true;
wait_power[1] = false;
}
}
}
else {
if (once_siklus[1]) {
digitalWrite(motor[1], !hidup);
if (automatic && reset_auto[1]) {
reset_auto[1] = false;
time.pause[1] = 0;
time.detik[1] = 0;
time.miliDetik[1] = 0;
lcd.setCursor(0, 1);
lcd.print(F("M"));
lcd.print(2);
sprintf(buff[1], ":%03d:%02d", time.detik[1], time.miliDetik[1]);
lcd.print(buff[1]);
lcd.print(F(" ("));
sprintf(buff[1], "%03d:%02d", time.setDetik[1], time.setMiliDetik[1]);
lcd.print(buff[1]);
lcd.print(F(")"));
}
else {
time.pause[1] = time.Stopwatch[1];
}
time.start[1] = false;
once_siklus[1] = false;
}
}
//Sensor 3
if (digitalRead(sensor3) == LOW) {
if (!once_siklus[2]) {
if (automatic) {
if (!wait_sensor[2]) {
ms_jeda[2] = millis();
wait_sensor[2] = true;
}
if (millis() - ms_jeda[2] >= waktuJeda[2] * 1000) {
if (time.detik[2] >= time.setDetik[2] && time.miliDetik[2] >= time.setMiliDetik[2]) {
}
else {
digitalWrite(motor[2], hidup);
time.start[2] = true;
time.mulai[2] = millis();
once_siklus[2] = true;
wait_power[2] = false;
wait_sensor[2] = false;
}
}
}
else {
digitalWrite(motor[2], hidup);
time.start[2] = true;
time.mulai[2] = millis();
once_siklus[2] = true;
wait_power[2] = false;
}
}
}
else {
if (once_siklus[2]) {
digitalWrite(motor[2], !hidup);
if (automatic && reset_auto[2]) {
reset_auto[2] = false;
time.pause[2] = 0;
time.detik[2] = 0;
time.miliDetik[2] = 0;
lcd.setCursor(0, 2);
lcd.print(F("M"));
lcd.print(3);
sprintf(buff[2], ":%03d:%02d", time.detik[2], time.miliDetik[2]);
lcd.print(buff[2]);
lcd.print(F(" ("));
sprintf(buff[2], "%03d:%02d", time.setDetik[2], time.setMiliDetik[2]);
lcd.print(buff[2]);
lcd.print(F(")"));
}
else {
time.pause[2] = time.Stopwatch[2];
}
time.start[2] = false;
once_siklus[2] = false;
}
}
//Sensor 4
if (digitalRead(sensor4) == LOW) {
if (!once_siklus[3]) {
if (automatic) {
if (!wait_sensor[3]) {
ms_jeda[3] = millis();
wait_sensor[3] = true;
}
if (millis() - ms_jeda[3] >= waktuJeda[3] * 1000) {
if (time.detik[3] >= time.setDetik[3] && time.miliDetik[3] >= time.setMiliDetik[3]) {
}
else {
digitalWrite(motor[3], hidup);
time.start[3] = true;
time.mulai[3] = millis();
once_siklus[3] = true;
wait_power[3] = false;
wait_sensor[3] = false;
}
}
}
else {
digitalWrite(motor[3], hidup);
time.start[3] = true;
time.mulai[3] = millis();
once_siklus[3] = true;
wait_power[3] = false;
}
}
}
else {
if (once_siklus[3]) {
digitalWrite(motor[3], !hidup);
if (automatic && reset_auto[3]) {
reset_auto[3] = false;
time.pause[3] = 0;
time.detik[3] = 0;
time.miliDetik[3] = 0;
lcd.setCursor(0, 3);
lcd.print(F("M"));
lcd.print(4);
sprintf(buff[3], ":%03d:%02d", time.detik[3], time.miliDetik[3]);
lcd.print(buff[3]);
lcd.print(F(" ("));
sprintf(buff[3], "%03d:%02d", time.setDetik[3], time.setMiliDetik[3]);
lcd.print(buff[3]);
lcd.print(F(")"));
}
else {
time.pause[3] = time.Stopwatch[3];
}
time.start[3] = false;
once_siklus[3] = false;
}
}
//Start Time 1
if (time.start[0]) {
if (automatic && reset_auto[0]) {
sprintf(buff[0], "%03d:%02d", time.setDetik[0], time.setMiliDetik[0]);
lcd.setCursor(0, 0);
lcd.print("M1:");
lcd.print(buff[0]);
}
else {
time.selesai[0] = millis();
unsigned long over;
// MATH time!!!
time.Stopwatch[0] = time.selesai[0] - time.mulai[0];
time.Stopwatch[0] = time.pause[0] + time.Stopwatch[0];
//jam = int(dataStopWatch[0] / 3600000);
//over = dataStopWatch[0] % 3600000;
//menit = int(over / 60000);
//over = over % 60000;
time.detik[0] = int (time.Stopwatch[0] / 1000);
time.miliDetik[0] = (time.Stopwatch[0] % 1000) / 10;
if (time.detik[0] >= time.setDetik[0] && time.miliDetik[0] >= time.setMiliDetik[0]) {
if (automatic) {
digitalWrite(motor[0], !hidup);
}
reset_auto[0] = true;
wait_power[0] = true;
}
}
}
//Start Time 2
if (time.start[1]) {
if (automatic && reset_auto[1]) {
sprintf(buff[1], "%03d:%02d", time.setDetik[1], time.setMiliDetik[1]);
lcd.setCursor(0, 1);
lcd.print("M2:");
lcd.print(buff[1]);
}
else {
time.selesai[1] = millis();
unsigned long over;
// MATH time!!!
time.Stopwatch[1] = time.selesai[1] - time.mulai[1];
time.Stopwatch[1] = time.pause[1] + time.Stopwatch[1];
//jam = int(dataStopWatch[0] / 3600000);
//over = dataStopWatch[0] % 3600000;
//menit = int(over / 60000);
//over = over % 60000;
time.detik[1] = int (time.Stopwatch[1] / 1000);
time.miliDetik[1] = (time.Stopwatch[1] % 1000) / 10;
if (time.detik[1] >= time.setDetik[1] && time.miliDetik[1] >= time.setMiliDetik[1]) {
if (automatic) {
digitalWrite(motor[1], !hidup);
}
reset_auto[1] = true;
wait_power[1] = true;
}
}
}
//Start Time 3
if (time.start[2]) {
if (automatic && reset_auto[2]) {
sprintf(buff[2], "%03d:%02d", time.setDetik[2], time.setMiliDetik[2]);
lcd.setCursor(0, 2);
lcd.print("M3:");
lcd.print(buff[2]);
}
else {
time.selesai[2] = millis();
unsigned long over;
// MATH time!!!
time.Stopwatch[2] = time.selesai[2] - time.mulai[2];
time.Stopwatch[2] = time.pause[2] + time.Stopwatch[2];
//jam = int(dataStopWatch[0] / 3600000);
//over = dataStopWatch[0] % 3600000;
//menit = int(over / 60000);
//over = over % 60000;
time.detik[2] = int (time.Stopwatch[2] / 1000);
time.miliDetik[2] = (time.Stopwatch[2] % 1000) / 10;
if (time.detik[2] >= time.setDetik[2] && time.miliDetik[2] >= time.setMiliDetik[2]) {
if (automatic) {
digitalWrite(motor[2], !hidup);
}
reset_auto[2] = true;
wait_power[2] = true;
}
else {
digitalWrite(motor[2], hidup);
}
}
}
//Start Time 4
if (time.start[3]) {
if (automatic && reset_auto[3]) {
sprintf(buff[3], "%03d:%02d", time.setDetik[3], time.setMiliDetik[3]);
lcd.setCursor(0, 3);
lcd.print("M4:");
lcd.print(buff[3]);
}
else {
time.selesai[3] = millis();
unsigned long over;
// MATH time!!!
time.Stopwatch[3] = time.selesai[3] - time.mulai[3];
time.Stopwatch[3] = time.pause[3] + time.Stopwatch[3];
//jam = int(dataStopWatch[0] / 3600000);
//over = dataStopWatch[0] % 3600000;
//menit = int(over / 60000);
//over = over % 60000;
time.detik[3] = int (time.Stopwatch[3] / 1000);
time.miliDetik[3] = (time.Stopwatch[3] % 1000) / 10;
if (time.detik[3] >= time.setDetik[3] && time.miliDetik[3] >= time.setMiliDetik[3]) {
if (automatic) {
digitalWrite(motor[3], !hidup);
}
reset_auto[3] = true;
wait_power[3] = true;
}
}
}
}
}
}
}
void returnToMainMenu() {
displayFirstLine = 0;
cursorLine = 0;
menuItems = sizeof startMenu / sizeof * startMenu - 1;
menuSelected = &startMenu[0][0];
menuOption = MainMenu;
}//end returnToMainMenu
void default_settings() {
if (EEPROM.read(0) != token_eeprom) {
EEPROM.write(0, token_eeprom);
for (int i = 0; i < 4; i++) {
EEPROMWriteint((1 + (i * 3)), time.setDetik[i]);
EEPROM.write(15 + i, time.setMiliDetik[i]);
}
}
delay(50);
eeprom_read();
}
void eeprom_read() {
for (int i = 0; i < 4; i++) {
time.setDetik[i] = EEPROMReadint((1 + (i * 3)));
time.setMiliDetik[i] = EEPROM.read(15 + i);
}
}
//---Write and Read EEPROM unsigned int---///
void EEPROMWriteint(int address, int number) {
EEPROM.write(address, number >> 8);
EEPROM.write(address + 1, number & 0xFF);
//EEPROM.commit();
}
unsigned int EEPROMReadint(int address) {
return (EEPROM.read(address) << 8) + EEPROM.read(address + 1);
}
//---end Write and Read---///