// VGE TMU (Tower managment Unit)
// Version 00 Gen 001
// By Adar Carmel
#include <LiquidCrystal.h>
//preSet
String Ver = "V.00 G.001";
LiquidCrystal lcd(12, 11, 10, 9, 8, 7); //lcd pins
String msg = "null";
int posDeg = 00;
int relayR = 5;
int relayL = 6;
int tenthRotDT = 1500;
int helfRotDT = 5000;
String RTurnMsg = "Right";
String LTurnMsg = "Left";
unsigned long previousMillis = 0; // Store the previous time
const unsigned long shortDelayDuration = 3600000/1000; // 1 hour in milliseconds
const unsigned long longDelayDuration = 36000000/1000; // 10 hour in milliseconds
int actionCount;
int switchPin1 = 2;
int switchPinR = 3;
int switchPinL = 4;
bool modeVal;
bool rotSwitchValR;
bool rotSwitchValL;
void setup() {
lcd.begin(16, 2);
logingDispaly();
pinMode(relayR, INPUT);
pinMode(relayL, INPUT);
pinMode(switchPin1, INPUT_PULLUP);
pinMode(switchPinR, INPUT_PULLUP);
pinMode(switchPinL, INPUT_PULLUP);
Serial.begin(115200);
Serial.println(Ver);
Serial.println("Welcome to VGE tower managment.");
digitalWrite(relayR, LOW);
digitalWrite(relayL, LOW);
delay(2000);
actionCount = 1;
}
void loop(){
modeSelection();
if (modeVal == 0){
//Serial.println("Auto mode selected");
byHourRot();
}
if (modeVal == 1){
//Serial.println(rotSwitchValL + rotSwitchValR);
//Serial.println("Manual mode selected");
manualMode();
actionCount = 1;
posDeg = 0;
}
}
void modeSelection(){
modeVal = digitalRead(switchPin1);
// Serial.println(modeVal);
// modes is 0 or 1
}
void byHourRot() {
unsigned long currentMillis = millis(); // Get the current time
screenSaver();
if ((actionCount >= 1) && (actionCount <= 10)){
if (currentMillis - previousMillis >= shortDelayDuration) {
// This code will run once every 1 hour
turnRight18d();
actionCount = actionCount + 1;
Serial.println("turned right 18d, actio count: ");
Serial.println(actionCount);
previousMillis = currentMillis; // Reset the previous time to the current time
posDeg = posDeg + 18;
}
}
if (actionCount == 11){
if (currentMillis - previousMillis >= shortDelayDuration) {
// This code will run once every 1 hour
turnLeft180d();
actionCount = actionCount + 1;
Serial.println("turned left 180d, actio count: ");
Serial.println(actionCount);
previousMillis = currentMillis; // Reset the previous time to the current time
posDeg = posDeg - 180;
}
}
if ((actionCount > 12) && (actionCount <= 24)){
if (currentMillis - previousMillis >= shortDelayDuration) {
// This code will run once every 1 hour
Serial.println("night time, static pos. action count: ");
Serial.println(actionCount);
previousMillis = currentMillis; // Reset the previous time to the current time
actionCount = actionCount + 1;
}
}
if (actionCount == 25){
actionCount = 1;
Serial.println("count reset: ");
Serial.println(actionCount);
}
}
void logingDispaly(){
lcd.setCursor(0,0);
lcd.print("Start Prog.");
delay(250);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Start Prog..");
delay(250);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Start Prog...");
delay(250);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Start Prog....");
delay(250);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Weolcome to VGE");
delay(250);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Tw.Mang.Un.V0G01");
lcd.setCursor(0,1);
lcd.print("Act:"+ msg + " pos:" + posDeg);
}
void screenSaver(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Act: " + msg);
lcd.setCursor(5,1);
lcd.print(posDeg);
lcd.setCursor(0,1);
lcd.print("Pos: ");
delay(20);
}
void turnRight18d(){
digitalWrite(relayR, HIGH);
//18 degree turn 360/20
int rotVal = 18;
//display
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Act: "+ RTurnMsg);
lcd.setCursor(5,1);
lcd.print(posDeg);
lcd.setCursor(0,1);
lcd.print("Pos: ");
delay(tenthRotDT);
digitalWrite(relayR, LOW);
}
void turnLeft180d(){
int rotVal = 180;
digitalWrite(relayL, HIGH);
//18 degree turn 360/20
delay(helfRotDT);
digitalWrite(relayL, LOW);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Act: "+ LTurnMsg);
lcd.setCursor(5,1);
lcd.print(posDeg);
lcd.setCursor(0,1);
lcd.print("Pos: ");
}
void manualMode(){
rotSwitchValR = digitalRead(switchPinR);
rotSwitchValL = digitalRead(switchPinL);
//Serial.print(rotSwitchValL);
if (rotSwitchValR == 0){
digitalWrite(relayR, HIGH);
Serial.println("manual turn right");
delay(20);
}
if (rotSwitchValL == 0){
digitalWrite(relayL, HIGH);
Serial.println("manual turn left");
delay(20);
}
digitalWrite(relayR, LOW);
digitalWrite(relayL, LOW);
Serial.println("Waiting for command");
delay(50);
}