/* 
The idea behind this sketch is to create a tool that could create a light-painted 
clock. If you set a camera up with a longer shutter speed, you can capture
the light trails of the LEDs connected to the servo arms. This would result
in a streak of light and a frozen light source on the right time. 

Due to the limitations of the servo, we would imagine a gear on the servo 
connected to a smaller gear at a 1:2 ratio. This would mean that the smaller 
gear would rotate the full 360 degrees when the servo motor rotates it's max 
of 180 degrees.

Current issues: 
lights fading based on the Arduino IDE's fade example isn't working. I don't 
yet know if that's only a limitation of WOKWI or whether the same would be 
true on a real Arduino Uno. There's a WOKWI sketch for fading here: 
https://wokwi.com/projects/313268562698437186
There is a chance that incorporating Gamma tables could fix the fading issue.

In making this on an actual board, I think there might need to be some more 
code to sync the clock upon compiling. I haven't checked.
*/

//Servo setup
#include <Servo.h>
Servo servoH;
Servo servoM;
int posH = 0;
int posM = 0;

//Time setup
#include "RTClib.h"
RTC_DS1307 rtc;


//pin inputs
int mLed = 9;
int hLed = 10;
int buttonPin = 2;
int hBrightness = 0;
int mBrightness = 0;

//fading variables
float fadeAmountH = 0;
float fadeAmountM = 0;

//time setting:
//the time set here is an example and gets overwritten later on using the RealTime Clock component
int timeHours = 12;
int timeMinutes = 34;
int sRotH = 0;         
int sRotM = 0;


void setup() {
  Serial.begin(9600);
  Serial.println("Starting up");

  pinMode(mLed, OUTPUT);
  pinMode(hLed, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  servoH.attach(12);
  servoM.attach(13);
  servoH.write(posH); 
  servoM.write(posM);

  if (! rtc.begin()) {
  Serial.println("Couldn't find RTC");
  Serial.flush();
  abort();
  }
}

void loop() {
  DateTime now = rtc.now();
  timeHours = now.hour();
  if(timeHours > 12){
    timeHours = timeHours - 12;
  };
  timeMinutes = now.minute();
  sRotH = timeHours * 15;
  sRotM = timeMinutes * 3;  

if(digitalRead(buttonPin) == LOW){
  Serial.println("Pushing clock hands");
  Serial.print("It is currently ");
  Serial.print(timeHours);
  Serial.print(":");
  Serial.println(timeMinutes);

  
  analogWrite(hLed, 255);  // This is a temporary solution to the light fades not behaving as expected in the simulation.   
  analogWrite(mLed, 255);  // This way the light trail will be consistant instead of fading in from behind.

  
  for (posH = 0; posH < sRotH; posH += 1) { // Servo Rotation to the hours of the clock
    servoH.write(posH);
    posM = posH*(static_cast<float>(sRotM)/static_cast<float>(sRotH)); // finds the relation between hours and minutes to move the minutes correctly
    servoM.write(posM);


    /*
    This section finds the relationship between the max position and maps it to the 
    brightness of the LEDs, but this doesn't show in the simulation. Might be a WOKWI issue.
    Ideally, this would fade in the lights to create a light trail increasing in intensity. 

    hBrightness = (posH * 100 / sRotH) * 2.55;
    Serial.print("posH is ");
    Serial.print(posH);
    Serial.print(", while hBrightness is "); 
    Serial.println(hBrightness);
    analogWrite(hLed, hBrightness);     
    analogWrite(mLed, hBrightness);  
    */
    delay(5);
  }

  delay(1000);
  // Reset motors and LEDs
  servoH.write(0);
  servoM.write(0);
} else {
  analogWrite(hLed, 0);
  analogWrite(mLed, 0);
}


}
$abcdeabcde151015202530fghijfghij
1
2
3
4
5
6
7
8
9
10
11
12
12
1
2
3
4
5
6
7
8
9
10
11
12
12
GND5VSDASCLSQWRTCDS1307+
Hours
Minutes