/* 
Millis() clock with timed event intervals.
- Set interval time for event to take place. Example: set to have event 
take place every hour.
- Set duration of event to be 'on' in seconds.
- Replace led with DC motor. Have motor run a small animation.
Make a 'peanut' man and have him sawing wood.


*/


#include <TinyWireM.h>
#include <Tiny4kOLED.h>
#include <TinyDebug.h>

#define led PB3

uint8_t width = 128;
uint8_t height = 64;

// Time variables
uint8_t tenth, second, minute, hour;

// Event interval variables
uint8_t eventIntervalHours = 0;  // Set event, every x hours
uint8_t eventIntervalMinutes = 4;  // Set event, every x minutes
uint8_t eventIntervalSeconds = 0;  // Set event, every x seconds
unsigned long eventDuration = 120;  // Set event duration in seconds

// Calculated values
unsigned long totalIntervalSeconds;
unsigned long lastEventMillis = 0;  // Tracks when the last event occurred
unsigned long eventEndMillis = 0;   // Tracks when the event ends

void setup() {
  Debug.begin();
  pinMode(led, OUTPUT);
  oled.begin(width, height, sizeof(tiny4koled_init_128x64br), tiny4koled_init_128x64br);
  oled.setFont(FONT8X16);
  oled.clear();
  oled.on();

  digitalWrite(led, LOW);

  // Calculate total interval in seconds
  totalIntervalSeconds = (eventIntervalHours * 3600) + (eventIntervalMinutes * 60) + eventIntervalSeconds;
}

void loop() {
  // Get the current time in milliseconds and calculate seconds
  unsigned long currentMillis = millis();
  unsigned long elapsedSeconds = currentMillis / 1000;

  // Convert current time to hours, minutes, and seconds
  hour = (elapsedSeconds / 3600) % 12;  // 12-hour format
  minute = (elapsedSeconds / 60) % 60;
  second = elapsedSeconds % 60;
  tenth = (currentMillis / 100) % 10;

  // Calculate time since the last event
  unsigned long timeSinceLastEvent = elapsedSeconds - (lastEventMillis / 1000); // 120 - (60000 / 1000) '60' = 60
  unsigned long timeUntilNextEvent = totalIntervalSeconds - timeSinceLastEvent; // 60 - 60 = 0

  // Break down timeUntilNextEvent into hours, minutes, and seconds
  uint8_t countdownHours = timeUntilNextEvent / 3600;
  uint8_t countdownMinutes = (timeUntilNextEvent % 3600) / 60;
  uint8_t countdownSeconds = timeUntilNextEvent % 60;

  // Event Trigger Logic
  if (currentMillis >= lastEventMillis + (totalIntervalSeconds * 1000)) { // 60000 + (60 * 1000) '60000' = 120000
    // Start the event
    lastEventMillis = currentMillis;  // Reset the time for the last event
    eventEndMillis = (currentMillis + (eventDuration * 1000));  // Calculate end time
    digitalWrite(led, HIGH);  // Turn on the LED
    Debug.println("---");
    Debug.println("Event started. LED ON.");
    Debug.print("currentMillis: ");
    Debug.print(currentMillis);
    Debug.print(", sec: ");
    Debug.println(elapsedSeconds);
    Debug.print("eventDuration (sec): ");
    Debug.println(eventDuration);
    Debug.print("Event end millis: ");
    Debug.print(eventEndMillis);
    Debug.print(", sec: ");
    Debug.println(eventEndMillis / 1000);
    Debug.println("---");
  }

  // - - - - - - - - - - - - - Original

  // Turn off LED when the event duration ends
  // Stays in this if statement because eventEndMillis = 0

  if (currentMillis >= eventEndMillis) {
    digitalWrite(led, LOW);
    Debug.print(currentMillis);
    Debug.print(" >= ");
    Debug.println(eventEndMillis);
  }

  // - - - - - - - - - - - - - Revised
/*
  // Ensure the LED stays on for the full event duration
  if (currentMillis < eventEndMillis) {
    digitalWrite(led, HIGH);  // Keep the LED on during the event
  } else if (currentMillis >= eventEndMillis && currentMillis - eventEndMillis < 100) {
    // Turn off the LED after the event ends
    digitalWrite(led, LOW);
    Debug.println("Event ended. LED OFF.");
  }
*/
  // - - - - - - - - - - - - -

  // Display Current Time
  //oled.clear();
  oled.setCursor(0, 0);
  oled.print("Time");

  oled.setCursor(0, 2);
  if (hour < 10) oled.print("0");
  oled.print(hour); oled.print(":");
  if (minute < 10) oled.print("0");
  oled.print(minute); oled.print(":");
  if (second < 10) oled.print("0");
  oled.print(second); oled.print(".");
  oled.print(tenth);

  // Display Countdown to Next Event
  oled.setCursor(0, 4);
  oled.print("Next Event");

  oled.setCursor(0, 6);
  if (countdownHours < 10) oled.print("0");
  oled.print(countdownHours); oled.print(":");
  if (countdownMinutes < 10) oled.print("0");
  oled.print(countdownMinutes); oled.print(":");
  if (countdownSeconds < 10) oled.print("0");
  oled.print(countdownSeconds);


  delay(10);  // Small delay to stabilize loop execution
}




ATTINY8520PU