#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
const int buttonPins[] = {A0, A1, A2, A3};
const int relayPins[] = {9, 8, 7, 6};
const int ledPins[] = {5, 4, 3, 2};
const int activatorButtonPin = 11;
const int additionalLedPin = 12;
int selectedButton = 0;
unsigned long startTimes[] = {0, 0, 0, 0};
bool buttonDisabled[] = {false, false, false, false};
const unsigned long activationTime = 3600000;
unsigned long activationTimes[] = {0, 0, 0, 0};
unsigned long lastButtonPressTime = 0;
void setup() {
lcd.backlight();
lcd.begin(20, 4);
lcd.print(" CHARGING STATION");
lcd.setCursor(0, 2);
lcd.print(" by: Shirhan A. B.");
delay(2000);
lcd.clear();
for (int i = 0; i < 4; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
pinMode(relayPins[i], OUTPUT);
pinMode(ledPins[i], OUTPUT);
}
pinMode(activatorButtonPin, INPUT_PULLUP);
pinMode(additionalLedPin, OUTPUT);
digitalWrite(additionalLedPin, HIGH);
}
void loop() {
for (int i = 0; i < 4; i++) {
if (digitalRead(buttonPins[i]) == LOW) {
lcd.setCursor((i < 2) ? 0 : 10, i % 2);
lcd.print(String(i + 1) +":");
selectedButton = i + 1;
delay(1000);
lastButtonPressTime = millis();
}
}
if (digitalRead(activatorButtonPin) == LOW && selectedButton != 0) {
activateRelayAndLed(relayPins[selectedButton - 1], ledPins[selectedButton - 1], startTimes[selectedButton - 1]);
activationTimes[selectedButton - 1] = activationTimes[selectedButton - 1] + activationTime;
selectedButton = 0;
}
if (millis() - lastButtonPressTime >= 2000) {
digitalWrite(additionalLedPin, LOW);
} else {
digitalWrite(additionalLedPin, HIGH);
}
for (int i = 0; i < 4; i++) {
if (startTimes[i] > 0) {
unsigned long remainingTime = activationTimes[i] - (millis() - startTimes[i]);
unsigned long remainingMinutes = remainingTime / 60000;
unsigned long remainingSeconds = (remainingTime % 60000) / 1000;
lcd.setCursor((i < 2) ? 0 : 10, i % 2);
lcd.print(String(1 + i) + ":" + String(remainingMinutes) + "m " + String(remainingSeconds) + "s");
}
lcd.setCursor(0,2);
lcd.print(" Select charger and");
lcd.setCursor(0, 3);
lcd.print(" insert coins...");
if (millis() - startTimes[i] >= activationTimes[i] && digitalRead(relayPins[i]) == LOW) {
digitalWrite(relayPins[i], HIGH);
digitalWrite(ledPins[i], LOW);
startTimes[i] = 0;
activationTimes[i]=0;
selectedButton = 0;
buttonDisabled[i] = false;
}
}
}
void activateRelayAndLed(int relay, int led, unsigned long &startTime) {
if (startTime == 0) {
digitalWrite(relay, LOW);
digitalWrite(led, HIGH);
startTime = millis();
}
}