#include <Stepper.h>
const int stepsPerRevolution = 2048; // 28BYJ-48 motor full turn
const int motorSpeedRPM = 10; // RPM for the motor
const int pin1 = 2;
const int pin2 = 4;
const int pin3 = 3;
const int pin4 = 5;
Stepper myStepper(stepsPerRevolution, pin1, pin2, pin3, pin4);
// Track time for timing step pulses
unsigned long previousMillis = 0; // Store the last time stepper moved
unsigned long currentMillis = 0; // Current time (from millis())
const unsigned long hourInterval = 3600000; // 1 hour in milliseconds (3600 seconds * 1000)
const unsigned long minuteInterval = 60000; // 1 minute in milliseconds (60 seconds * 1000)
// Counters for hours and minutes
int completedHours = 0;
int completedMinutes = 0;
void setup() {
Serial.begin(9600);
myStepper.setSpeed(1); // Set any safe speed
Serial.println("Homing to 0 position...");
myStepper.step(-2048); // Rotate counterclockwise 1 full turn to ensure we're at zero
// Now motor is considered to be at "0" position
delay(1000);
Serial.println("At 0 position. Beginning regular motion...");
}
void loop() {
currentMillis = millis(); // Get current time
// Check if 1 hour has passed (3600000 milliseconds)
if (currentMillis - previousMillis >= hourInterval) {
// It's been an hour, increment the hour counter and reset the previousMillis
completedHours++;
previousMillis = currentMillis;
// Print the completed hours
Serial.print("Completed ");
Serial.print(completedHours);
Serial.println(" hour(s).");
}
// Check if 1 minute has passed (60000 milliseconds)
if (currentMillis - previousMillis >= minuteInterval) {
// It's been a minute, increment the minute counter
completedMinutes++;
previousMillis = currentMillis;
// Print the completed minutes
Serial.print("Completed ");
Serial.print(completedMinutes);
Serial.println(" minute(s).");
}
// After every full revolution, rotate the motor in the opposite direction (counterclockwise)
myStepper.step(-stepsPerRevolution); // 1 full revolution counterclockwise
delay(1000); // Allow time to observe
// Repeat the process
Serial.println("Counterclockwise rotation complete.");
}