Could someone possibly please look at the code and tell me what needs to be changed in order for this to keep time properly?
Thanks in advance
#include <RTClib.h>
RTC_DS3231 rtc;
// minutes
uint8_t min, prev_min;
// ports used to control the stepper motor
// if your motor rotate to the opposite direction,
// change the order as {4, 5, 6, 7};
// for Arduino nano
int port[4] = {7, 6, 5, 4};
// for ESP32 (example)
// int port[4] = {12, 32, 25, 27};
// Motor and clock parameters
#define STEPS_PER_ROTATION 1536 // steps for a minute
// wait for a single step of stepper
#define DELAYTIME 3
// sequence of stepper motor control
int seq[4][4] = {
{ LOW, LOW, HIGH, LOW},
{ LOW, LOW, LOW, HIGH},
{ HIGH, LOW, LOW, LOW},
{ LOW, HIGH, LOW, LOW}
};
void rotate(int step) {
static int phase = 0;
int i, j;
int delta = (step > 0) ? 1 : 3;
int dt = 20;
step = (step > 0) ? step : -step;
for(j = 0; j < step; j++) {
phase = (phase + delta) % 4;
for(i = 0; i < 4; i++) {
digitalWrite(port[i], seq[phase][i]);
}
delay(dt);
if(dt > DELAYTIME) dt--;
}
// power cut
for(i = 0; i < 4; i++) {
digitalWrite(port[i], LOW);
}
}
void setup() {
DateTime thetime;
//Serial for debugging
Serial.begin(9600);
// setup rtc
rtc.begin();
if (!rtc.begin()) {
Serial.println("RTC is NOT running!");
abort();
}
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(__DATE__, __TIME__));
thetime = rtc.now();
Serial.println("RTC is running!");
Serial.print(thetime.year(), DEC);
Serial.print('/');
Serial.print(thetime.month(), DEC);
Serial.print('/');
Serial.print(thetime.day(), DEC);
Serial.print(' ');
Serial.print(thetime.hour(), DEC);
Serial.print(':');
Serial.print(thetime.minute(), DEC);
Serial.print(':');
Serial.print(thetime.second(), DEC);
Serial.println();
// setup stepper
pinMode(port[0], OUTPUT);
pinMode(port[1], OUTPUT);
pinMode(port[2], OUTPUT);
pinMode(port[3], OUTPUT);
// set prev_min to an invalid value, so that program starts with a min rotation after reset
prev_min = 99;
}
void loop() {
min = rtc.now().minute();
if(prev_min != min) {
rotate(-10); // for approach run
rotate(10); // approach run without heavy load
rotate(STEPS_PER_ROTATION);
prev_min = min;
}
delay(100);
}