#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
#include <SD.h>
#include <EEPROM.h>
#include <avr/wdt.h> // Include library for watchdog timer
// Initialize the LCD library with the I2C address
LiquidCrystal_I2C lcd(0x27, 20, 4);
RTC_DS1307 rtc;
const int chipSelect = 10;
int counter = 0;
File myFile;
// Pin definitions
const int relay1Pin = 2;
const int relay2Pin = 3;
const int relayidtPin = 9;
const int upperPositionSensor = 4;
const int lowerPositionSensor = 5;
const int presenceSensor = 6;
const int startButton = 8;
void resetArduino() {
Serial.println("Resetting Arduino...");
wdt_enable(WDTO_15MS); // Enable the watchdog timer to reset Arduino after 15 ms
while (1); // Wait for the watchdog timer to trigger
}
void setup() {
// Initialize the LCD
lcd.begin(20, 4);
lcd.backlight();
// Initialize the RTC
if (!rtc.begin()) {
lcd.print("Couldn't find RTC");
while (1);
}
if (!rtc.isrunning()) {
lcd.print("RTC is NOT running!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
// Initialize the SD card
if (!SD.begin(chipSelect)) {
lcd.print("SD init failed!");
while (1);
}
// Initialize pin modes
pinMode(relay1Pin, OUTPUT);
pinMode(relay2Pin, OUTPUT);
pinMode(upperPositionSensor, INPUT_PULLUP);
pinMode(lowerPositionSensor, INPUT_PULLUP);
pinMode(presenceSensor, INPUT_PULLUP);
pinMode(startButton, INPUT_PULLUP);
// Ensure relays are initially off
digitalWrite(relay1Pin, LOW);
digitalWrite(relay2Pin, LOW);
// Initialize Serial for debug messages
Serial.begin(9600);
Serial.println("Setup complete.");
// Retrieve the counter value from EEPROM
counter = EEPROM.read(0) | (EEPROM.read(1) << 8);
Serial.print("Counter value retrieved from EEPROM: ");
Serial.println(counter);
// Print initial message
lcd.setCursor(0, 0);
lcd.print("Pokreni uredjaj");
}
void loop() {
Serial.println("Entering loop.");
// Check if the start button is pressed
if (digitalRead(startButton) == LOW) {
counter++;
DateTime now = rtc.now();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Count: ");
lcd.print(counter);
lcd.setCursor(0, 1);
lcd.print(now.timestamp(DateTime::TIMESTAMP_DATE));
lcd.setCursor(0, 2);
lcd.print(now.timestamp(DateTime::TIMESTAMP_TIME));
lcd.setCursor(0, 3);
lcd.print("Ubacite telefon");
delay(500); // Debounce delay
// Save to SD card
myFile = SD.open("count.txt", FILE_WRITE);
if (myFile) {
myFile.print("Count: ");
myFile.print(counter);
myFile.print(" Date: ");
myFile.print(now.timestamp(DateTime::TIMESTAMP_DATE));
myFile.print(" Time: ");
myFile.println(now.timestamp(DateTime::TIMESTAMP_TIME));
myFile.close();
} else {
lcd.clear();
lcd.print("Error opening file");
}
// Save the counter value to EEPROM
EEPROM.write(0, counter & 0xFF);
EEPROM.write(1, (counter >> 8) & 0xFF);
Serial.print("Counter value saved to EEPROM: ");
Serial.println(counter);
// Wait for the presence sensor to detect an object
while (digitalRead(presenceSensor) == HIGH);
lcd.setCursor(0, 3);
lcd.print("Telefon se unistava");
// Activate relay 1
digitalWrite(relay1Pin, HIGH);
// Wait for the lower position sensor to be triggered
while (digitalRead(lowerPositionSensor) == HIGH);
digitalWrite(relay1Pin, LOW);
// Activate relay 2
digitalWrite(relay2Pin, HIGH);
// Wait for the upper position sensor to be triggered
while (digitalRead(upperPositionSensor) == HIGH);
digitalWrite(relay2Pin, LOW);
digitalWrite(relayidtPin, HIGH);
delay(100);
digitalWrite(relayidtPin, LOW);
lcd.setCursor(0, 3);
lcd.print("Uzmite-------Telefon");
delay(5000); // Delay before restarting the loop
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Hvala");
Serial.println("Process complete. Resetting Arduino.");
// Reset the Arduino programmatically
resetArduino();
}
}