/*
The DS1307 communicates using the I2C protocol. You'll need to connect the SDA and SCL pins of the RTC to the corresponding I2C pins on the ESP32.
Connections:
VCC (DS1307) to 3.3V (ESP32)
GND (DS1307) to GND (ESP32)
SDA (DS1307) to SDA (ESP32 GPIO 21) - This might vary depending on your board.
SCL (DS1307) to SCL (ESP32 GPIO 22) - This might vary depending on your board.
Required Libraries
You will need the Wire library for I2C communication and the RTClib library for interacting with the DS1307. You can install these through the Arduino Library Manager.
*/
#include <Wire.h>
#include <RTClib.h>
#define DIR_PIN 15
#define M_STEP_PIN 2
#define MM_STEP_PIN 0
#define H_STEP_PIN 4
#define HH_STEP_PIN 16
#define STEPS_PER_REVOLUTION 200
int Button_pin = 14;
int Button_state;
int CurrentMinute = 0;
int Current_Hour = 0;
int Saved_Current_Minute = 0;
int Saved_Current_H = 0;
int Saved_Current_HH = 0;
int M_Digit = 0;
int MM_Digit = 0;
int H_Digit = 0;
int HH_Digit = 0;
int Saved_M_Digit = 0;
int Saved_MM_Digit = 0;
int Saved_H_Digit = 0;
//testing
int MM_timer_count = 0;
int H_timer_count = 0;
int Current_Hour_counter = 0;
byte Clockwise = HIGH; //Direction - clockwise = HIGH - counterclockwise = LOW
RTC_DS1307 rtc;
void setup() {
Serial.begin(115200);
pinMode(M_STEP_PIN, OUTPUT);
pinMode(MM_STEP_PIN, OUTPUT);
pinMode(H_STEP_PIN, OUTPUT);
pinMode(HH_STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
pinMode(Button_pin, INPUT);
digitalWrite(DIR_PIN, Clockwise);
// Initialize I2C communication
Wire.begin();
// Check if the RTC is connected properly
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
// Check if the RTC lost power and if so, set the time
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// This line sets the RTC with an explicit date & time, for example to set
// January 1, 2024 at 00:00 you would call:
rtc.adjust(DateTime(2024, 1, 1, 0, 0, 0));
}
}
void loop() {
Button_state = digitalRead(Button_pin);
if(Button_state == HIGH){
Serial.println("pressed");
digitalWrite(M_STEP_PIN, HIGH);
digitalWrite(M_STEP_PIN, LOW);
}
//definitions
DateTime now = rtc.now();
CurrentMinute = now.minute(), DEC; // Example using the 'Time' library's minute() function
Current_Hour = now.hour(), DEC;
M_Digit = CurrentMinute % 10; // The modulo operation gives the remainder when divided by 10
MM_Digit = CurrentMinute / 10;
H_Digit = Current_Hour % 10; // The modulo operation gives the remainder when divided by 10
HH_Digit = Current_Hour / 10;
//TESTING
/*
Current_Hour = (millis()/1000)-(24*Current_Hour_counter);
if (Current_Hour > 23){
Current_Hour_counter++;
Current_Hour = (millis()/1000)-(24*Current_Hour_counter);
}
M_Digit = (millis()/1000) % 10;
MM_Digit = (millis()/1000) - (6 * MM_timer_count);
if (MM_Digit > 5){
MM_timer_count++;
MM_Digit = (millis()/1000) - (6 * MM_timer_count);
}
H_Digit = Current_Hour % 10;
HH_Digit = Current_Hour / 10;
*/
// XX:X_
while (Saved_M_Digit != M_Digit){
Saved_M_Digit++;
if(Saved_M_Digit == 10){
Saved_M_Digit = M_Digit;
}
// Spin the stepper motor 20 steps
for (int i = 0; i < 20; i++) {
// These four lines result in 1 step:
digitalWrite(M_STEP_PIN, HIGH);
delayMicroseconds(500);
digitalWrite(M_STEP_PIN, LOW);
delayMicroseconds(500);
}
}
// XX:_X
while (Saved_MM_Digit != MM_Digit){
Saved_MM_Digit++;
if(Saved_MM_Digit > 5){
Saved_MM_Digit = MM_Digit;
// Spin the stepper motor 20 steps
for (int i = 0; i < 20*5; i++) {
// These four lines result in 1 step:
digitalWrite(MM_STEP_PIN, HIGH);
delayMicroseconds(500);
digitalWrite(MM_STEP_PIN, LOW);
delayMicroseconds(500);
}
}
else{
// Spin the stepper motor 20 steps
for (int i = 0; i < 20; i++) {
// These four lines result in 1 step:
digitalWrite(MM_STEP_PIN, HIGH);
delayMicroseconds(500);
digitalWrite(MM_STEP_PIN, LOW);
delayMicroseconds(500);
}
}
}
// X_:XX
while (Saved_Current_H != Current_Hour){
Saved_Current_H++;
if(Saved_Current_H > 23){
Saved_Current_H = Current_Hour;
// Spin the stepper motor 20 steps
for (int i = 0; i < 20*7; i++) {
// These four lines result in 1 step:
digitalWrite(H_STEP_PIN, HIGH);
delayMicroseconds(500);
digitalWrite(H_STEP_PIN, LOW);
delayMicroseconds(500);
}
}
else{
// Spin the stepper motor 20 steps
for (int i = 0; i < 20; i++) {
// These four lines result in 1 step:
digitalWrite(H_STEP_PIN, HIGH);
delayMicroseconds(500);
digitalWrite(H_STEP_PIN, LOW);
delayMicroseconds(500);
}
}
}
// _X:XX
while (Saved_Current_HH != HH_Digit){
Saved_Current_HH++;
if(Saved_Current_HH > 2){
Saved_Current_HH = HH_Digit;
// Spin the stepper motor 20 steps
for (int i = 0; i < 20*8; i++) {
// These four lines result in 1 step:
digitalWrite(HH_STEP_PIN, HIGH);
delayMicroseconds(500);
digitalWrite(HH_STEP_PIN, LOW);
delayMicroseconds(500);
}
}
else{
// Spin the stepper motor 20 steps
for (int i = 0; i < 20; i++) {
// These four lines result in 1 step:
digitalWrite(HH_STEP_PIN, HIGH);
delayMicroseconds(500);
digitalWrite(HH_STEP_PIN, LOW);
delayMicroseconds(500);
}
}
}
//Serial monitor debugging
Serial.println();
Serial.print("Saved_Current_HH = ");
Serial.println(Saved_Current_HH);
Serial.print("Current_Hour = ");
Serial.println(Current_Hour);
Serial.print("H_Digit = ");
Serial.println(H_Digit);
Serial.print("HH_Digit = ");
Serial.println(HH_Digit);
/*
// Print the Current date and time to the serial monitor
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.Hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');*/
//Serial.print(now.second(), DEC);
//Serial.println();
delay(10); // Wait for 1 second
}
1
4
0
5
2
3
6
7
8
9
1
4
0
5
2
3
6
7
8
9
1
4
0
5
2
3
6
7
8
9
1
4
0
5
2
3
6
7
8
9