// Servo Sweep example for the ESP32
// https://wokwi.com/arduino/projects/323706614646309460
#include <ESP32Servo.h>
#include <Wire.h>
#include <DS3231.h>
DS3231 clk;
const int servoPin = 18;
Servo servo;
int format;
int hours;
int minutes;
int seconds;
String time_period = "";
bool h12Flag;
bool pmFlag;
void time_setup()
{
Serial.println(" ");
Serial.println("DS3231 based Clock and Calender");
//Set up the preferred Time Format (12 hrs / 24 hrs)
Serial.println("Select the time format (12/24) : ");
while (Serial.available() == 0) { } // Wait for user input
format = Serial.parseInt();
Serial.print("Time format : ");
Serial.print(format); //Use this to set 12/24 clock cycle
Serial.println(" hrs");
//Take in the time values from the user
Serial.println("Enter the hours : ");
while(Serial.available() == 0) { } // Wait for user input
hours = Serial.parseInt();
Serial.println("Enter the minutes : ");
while(Serial.available() == 0) { } // Wait for user input
minutes = Serial.parseInt();
Serial.println("Enter the seconds : ");
while(Serial.available() == 0) { } // Wait for user input
seconds = Serial.parseInt();
//If 12 hrs clock cycle, AM or PM?
if(format == 12)
{
Serial.println("Time Period (AM/PM) : ");
while(Serial.available() == 0) { } // Wait for user input
time_period = Serial.readString();
}
//Print the values
Serial.println("-------------------------------------------------------------");
Serial.print("The RTC is set to : ");
Serial.print(hours);
Serial.print(" : ");
Serial.print(minutes);
Serial.print(" : ");
Serial.print(seconds);
if(format == 12)
{
Serial.print(" ");
Serial.println(time_period);
}
else
Serial.println(" ");
Serial.println("-------------------------------------------------------------");
}
void RTC_setup()
{
clk.setHour(hours);
clk.setMinute(minutes);
clk.setSecond(seconds);
if (format==12)
clk.setClockMode(true);
else
clk.setClockMode(false);
delay(1000);
Serial.println("RTC has be set with new time.");
Serial.println("-------------------------------------------------------------");
Serial.println("The DS3231 time is : ");
}
void RTC_display()
{
/*
* This block displays the time in the serial monitor.
*/
delay(1000);
hours = clk.getHour(h12Flag,pmFlag);
minutes = clk.getMinute();
seconds = clk.getSecond();
// Print Hours
if(hours<10)
Serial.print(" 0");
else
Serial.print(" ");
Serial.print(hours);
Serial.print(" : ");
// Print Minutes
if(minutes<10)
Serial.print("0");
Serial.print(minutes);
Serial.print(" : ");
// Print Seconds
if(seconds<10)
Serial.print("0");
Serial.print(seconds);
//Print AM/PM
if(format == 12)
{
Serial.print(" ");
Serial.println(time_period);
}
else
Serial.println(" ");
// Reset AM/PM during clock transitions
if (format == 12 && hours == 11 && minutes == 59 && seconds ==59)
{
if ( time_period == "AM")
time_period = "PM";
else if ( time_period == "PM")
time_period = "AM";
}
}
void setup() {
RTC_setup();
servo.attach(servoPin, 500, 2400);
//Set the Baud Rate
Serial.begin(9600);
time_setup();
}
int pos = 0;
void loop() {
RTC_display();
for (pos = 0; pos <= 180; pos += 1) {
servo.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 1) {
servo.write(pos);
delay(15);
}
}