#include <Wire.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);
int counter = 0;
// Pin definitions
const int relay1Pin = 4;
const int relay2Pin = 5;
const int relay3Pin = 9;
const int presenceSensor = 6;
const int startButton = 7;
const int resetButton = 10; // Define the reset button pin
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 pin modes
pinMode(ledPin, OUTPUT);
pinMode(relay1Pin, OUTPUT);
pinMode(relay2Pin, OUTPUT);
pinMode(relay3Pin, OUTPUT);
pinMode(presenceSensor, INPUT_PULLUP);
pinMode(startButton, INPUT_PULLUP);
pinMode(resetButton, INPUT_PULLUP); // Initialize reset button pin
// 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);
// Turn off LED
digitalWrite(ledPin, LOW);
}
// Check if the reset button is pressed
if (digitalRead(resetButton) == LOW) {
Serial.println("Reset button pressed");
counter = 0; // Reset the counter to zero
// 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 reset and saved to EEPROM: ");
Serial.println(counter);
}
delay(100); // Small delay to prevent bouncing issues
}