⏱️

Arduino Code for the Vidor Clock

//Code By Vimarsh  T@Vimarsh244  
  
#include "VidorGraphics.h"  
#include "Vidor_GFX.h"  
#include <SPI.h>  
//#include <WiFi101.h>  
#include <WiFiNINA.h> //Include this instead of WiFi101.h as needed  
#include <WiFiUdp.h>  
#include <RTCZero.h>  
  
RTCZero rtc;  
void(* resetFunc) (void) = 0; //declare reset function @ address 0  
  
char ssid[] = "ssid";        // your network SSID (name)  
char pass[] = "psk";    // your network password (use for WPA, or use as key for WEP)  
//int keyIndex = 0;                           // your network key Index number (needed only for WEP)  
  
int status = WL_IDLE_STATUS;  
  
const int Hr = 5; //change this to adapt it to your time zone //enter number of hours  
const int minutes = 30; //change this to adapt it to your time zone //enter number of minutes  
  
Vidor_GFX  vdgfx;  
//drawClock();  
int x = 0;  
int y = 0;  
int prev_sec;  
int prev_mint;  
void setup() {  
  Serial.begin(9600);  
  
  //while (!Serial); //disable to run without cxonnrcting to computer.  
  wifi_setup();  
  // Initialize the FPGA  
  if (!FPGA.begin()) {  
    Serial.println("Initialization failed!");  
    while (1) {}  
  }  
  String date = String(rtc.getDay()) + "/" + String(rtc.getMonth()) + "/" + String(rtc.getYear());  
  int got_sec = (rtc.getHours() + Hr) * 3600 + (rtc.getMinutes() + minutes) * 60 + rtc.getSeconds();  
  drawClockFace();  
  vdgfx.text.setCursor(50, 50);  
  vdgfx.text.setAlpha(255);  
  vdgfx.text.setSize(1);  
  vdgfx.text.setColor(vdgfx.lightBlue());  
  vdgfx.println("Vidor Clock");  
  vdgfx.text.setCursor(200, 100);  
  vdgfx.text.setSize(2);  
  vdgfx.println(date);  
  
  
  //The old code for the clock moving... Almost worked. Discarded for Hours....  
  /* 
    void loop() 
    { 
    for (int mint = 0; mint < 60; mint++) { 
      for (int sec = 0; sec < 60; sec++) { 
        if (sec  == mint) { 
          x = 320 + 80 * sin(mint * 0.1048); 
          y = 280 - 80 * cos(mint * 0.1048); 
          vdgfx.drawLine(320, 280, x, y, vdgfx.Yellow()); 
          vdgfx.drawLine(320, 280, x, y, vdgfx.Yellow()); 
          vdgfx.drawLine(320, 280, x, y, vdgfx.Yellow()); 
        } 
        x = 320 + 130 * sin(prev_sec * 0.1048); 
        y = 280 - 130 * cos(prev_sec * 0.1048); 
        vdgfx.drawLine(320, 280, x, y, vdgfx.Yellow()); 
 
        x = 320 + 130 * sin(sec * 0.1048); 
        y = 280 - 130 * cos(sec * 0.1048); 
        vdgfx.drawLine(320, 280, x, y, vdgfx.Purple()); 
        prev_sec = sec; 
        if (sec - 1 == mint) { 
          x = 320 + 80 * sin(mint * 0.1048); 
          y = 280 - 80 * cos(mint * 0.1048); 
          vdgfx.drawLine(320, 280, x, y, 5, vdgfx.Purple()); 
        } 
 
        delay(100); 
      } 
      x = 320 + 800 * sin(prev_mint * 0.1048); 
      y = 280 - 80 * cos(prev_mint * 0.1048); 
      vdgfx.drawLine(320, 280, x, y, vdgfx.Yellow()); 
      vdgfx.drawLine(320, 280, x, y, vdgfx.Yellow()); 
      vdgfx.drawLine(320, 280, x, y, vdgfx.Yellow()); 
 
      x = 320 + 80 * sin(mint * 0.1048); 
      y = 280 - 80 * cos(mint * 0.1048); 
      vdgfx.drawLine(320, 280, x, y, 5, vdgfx.Purple()); 
      prev_mint = mint; 
 
    } 
    } 
  */  
  
  ///Actual moving of clock and all calculation  
  for (int sec = got_sec; sec < 3600 * 24; sec++) {  
  
    x = 320 + 130 * sin(prev_sec * 0.1048);  
    y = 280 - 130 * cos(prev_sec * 0.1048);  
    vdgfx.drawLine(320, 280, x, y, vdgfx.Yellow());  
  
    x = 320 + 90 * sin(prev_sec * 0.1048 / 60);  
    y = 280 - 90 * cos(prev_sec * 0.1048 / 60);  
    vdgfx.drawLine(320, 280, x, y, vdgfx.Yellow());  
  
    x = 320 + 50 * sin(prev_sec * 0.1048 / (60 * 12));  
    y = 280 - 50 * cos(prev_sec * 0.1048 / (60 * 12));  
    vdgfx.drawLine(320, 280, x, y, vdgfx.Yellow());  
  
    x = 320 + 130 * sin(sec * 0.1048);  
    y = 280 - 130 * cos(sec * 0.1048);  
    vdgfx.drawLine(320, 280, x, y, vdgfx.Purple());  
  
    x = 320 + 90 * sin(sec * 0.1048 / 60);  
    y = 280 - 90 * cos(sec * 0.1048 / 60);  
    vdgfx.drawLine(320, 280, x, y, vdgfx.Black());  
  
    x = 320 + 50 * sin(prev_sec * 0.1048 / (60 * 12));  
    y = 280 - 50 * cos(prev_sec * 0.1048 / (60 * 12));  
    vdgfx.drawLine(320, 280, x, y, vdgfx.Blue());  
    prev_sec = sec;  
  
    delay(1000);  
  }  
  resetFunc();  //call reset to reset the board  
}  
void loop() {  
  
}  
//void printTime()  
//{  
//  print2digits(rtc.getHours() + GMT);  
//  Serial.print(":");  
//  print2digits(rtc.getMinutes());  
//  Serial.print(":");  
//  print2digits(rtc.getSeconds());  
//  Serial.println();  
//}  
//  
//void printDate()  
//{  
//  Serial.print(rtc.getDay());  
//  Serial.print("/");  
//  Serial.print(rtc.getMonth());  
//  Serial.print("/");  
//  Serial.print(rtc.getYear());  
//  
//  Serial.print(" ");  
//}  
  
void drawClockFace() {  
  // Fill the screen with a white background  
  vdgfx.fillRect(0, 0, 640, 480, vdgfx.Yellow());  
  //Draw the clock face  
  vdgfx.fillCircle(320, 280, 150 , vdgfx.Green());  
  vdgfx.fillCircle(320, 280, 140 , vdgfx.Yellow());  
  vdgfx.fillCircle(320, 280, 2 , vdgfx.Green());  
  
  for (int m = 0; m < 360; m = m + 30) {  
    int x = 0;  
    int y = 0;  
    x = 320 + 140 * sin(m * 0.0175);  
    y = 280 - 140 * cos(m * 0.0175);  
    vdgfx.fillRect(x, y, 4, 4, vdgfx.Purple());  
  }  
}  
  
  
//white: yellow  
//Light Blue: Green  
//Red: Purple  
  
void wifi_setup() {  
  // check if the WiFi module works  
  if (WiFi.status() == WL_NO_SHIELD) {  
    Serial.println("WiFi shield not present");  
    // don't continue:  
    while (true);  
  }  
  
  // attempt to connect to WiFi network:  
  while ( status != WL_CONNECTED) {  
    Serial.print("Attempting to connect to SSID: ");  
    Serial.println(ssid);  
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:  
    status = WiFi.begin(ssid, pass);  
  
    // wait 10 seconds for connection:  
    delay(10000);  
  }  
  
  // you're connected now, so print out the status:  
  printWiFiStatus();  
  
  rtc.begin();  
  
  unsigned long epoch;  
  int numberOfTries = 0, maxTries = 6;  
  do {  
    epoch = WiFi.getTime();  
    numberOfTries++;  
  }  
  while ((epoch == 0) && (numberOfTries < maxTries));  
  
  if (numberOfTries == maxTries) {  
    Serial.print("NTP unreachable!!");  
    while (1);  
  }  
  else {  
    Serial.print("Epoch received: ");  
    Serial.println(epoch);  
    rtc.setEpoch(epoch);  
  
    Serial.println();  
  }  
}  
  
  
void printWiFiStatus() {  
  // print the SSID of the network you're attached to:  
  Serial.print("SSID: ");  
  Serial.println(WiFi.SSID());  
  
  // print your WiFi shield's IP address:  
  IPAddress ip = WiFi.localIP();  
  Serial.print("IP Address: ");  
  Serial.println(ip);  
  
  // print the received signal strength:  
  long rssi = WiFi.RSSI();  
  Serial.print("signal strength (RSSI):");  
  Serial.print(rssi);  
  Serial.println(" dBm");  
}  
  
void print2digits(int number) {  
  if (number < 10) {  
    Serial.print("0");  
  }  
  Serial.print(number);  
}