#include <Wire.h>
#include <RTClib.h>
#include <SD.h>
#include <EEPROM.h>
#include <avr/wdt.h>
#include <TM1637Display.h>
// Define the connections pins for the TM1637
#define CLK 2
#define DIO 3
// Initialize the TM1637 display
TM1637Display display(CLK, DIO);
// Initialize the RTC
RTC_DS1307 rtc;
const int chipSelect = 10;
int counter = 0;
File myFile;
// Pin definitions
const int relay1Pin = 4;
const int relay2Pin = 5;
const int relay3Pin = 9;
const int presenceSensor = 6;
const int startButton = 7;
const int ledPin = 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() {
display.setBrightness(0x0f); // Set brightness to maximum
// Initialize the RTC
if (!rtc.begin()) {
while (1);
}
if (!rtc.isrunning()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
// Initialize the SD card
if (!SD.begin(chipSelect)) {
while (1);
}
// Initialize pin modes
pinMode(ledPin, OUTPUT);
pinMode(relay1Pin, OUTPUT);
pinMode(relay2Pin, OUTPUT);
pinMode(relay3Pin, OUTPUT);
pinMode(presenceSensor, INPUT_PULLUP);
pinMode(startButton, INPUT_PULLUP);
// Ensure relays and LED are initially off
digitalWrite(ledPin, LOW);
digitalWrite(relay1Pin, LOW);
digitalWrite(relay2Pin, LOW);
digitalWrite(relay3Pin, 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);
if (counter < 0) {
counter = 0; // Initialize to 0 if the counter is invalid
}
Serial.print("Counter value retrieved from EEPROM: ");
Serial.println(counter);
// Display initial count
display.showNumberDec(counter, true, 4, 0); // Adjust the number of digits as needed
}
void loop() {
Serial.println("Entering loop.");
// Check if the start button is pressed
if (digitalRead(startButton) == LOW) {
Serial.println("Start button pressed");
digitalWrite(ledPin, HIGH); // Turn on LED
// Wait for the presence sensor to detect an object
Serial.println("Waiting for presence sensor...");
while (digitalRead(presenceSensor) == HIGH);
Serial.println("Presence detected");
digitalWrite(relay1Pin, HIGH); // Turn on relay 1
delay(5000); // Wait for 5 seconds
digitalWrite(relay1Pin, LOW); // Turn off relay 1
digitalWrite(relay2Pin, HIGH); // Turn on relay 2
delay(5000); // Wait for 5 seconds
digitalWrite(relay2Pin, LOW); // Turn off relay 2
digitalWrite(relay3Pin, HIGH); // Turn on relay 3 for 10 ms
delay(100);
digitalWrite(relay3Pin, LOW); // Turn off relay 3
Serial.println("Relay 3 turned on for 10 ms");
counter++; // Increment counter
// Display the updated count
display.showNumberDec(counter, true, 4, 0); // Adjust the number of digits as needed
// Save the updated counter to EEPROM
EEPROM.write(0, counter & 0xFF);
EEPROM.write(1, (counter >> 8) & 0xFF);
Serial.print("Counter value saved to EEPROM: ");
Serial.println(counter);
// Save the updated counter to SD card in CSV format
DateTime now = rtc.now();
myFile = SD.open("data.csv", FILE_WRITE);
if (myFile) {
myFile.print(counter);
myFile.print(",");
myFile.print(now.timestamp(DateTime::TIMESTAMP_DATE));
myFile.print(",");
myFile.println(now.timestamp(DateTime::TIMESTAMP_TIME));
myFile.close();
}
// Turn off LED
digitalWrite(ledPin, LOW);
}
delay(100); // Small delay to prevent bouncing issues
}