Hi, I'm a novice programmer, but in need of some guidance. My code is for a stepper motor clock using two stepper motors. The motors each use an A4988 driver module, and my code loop starts by checking the state change input pulses on pin 3 of my Nano. Each pulse has a 1 second duration, and the minutes motor steps once every minute. The second part of my code waits for each hour (3600 seconds) at which point the hours motor advances 5 steps, within each 5 minute interval, making a 12 hour period.
My code compiles and runs fine, with each minute rotation completed. However on each hour the hour dial should complete 4 steps, for each hour shown on the 12 hour dial, It will only move two steps at a time.
If I compile and run just the hour section of my code to test it, I can get the 4 steps as required to show each hour, which runs fine. My problem is running the minutes and hours together within the my program .
Maybe I'm missing something, also as each hour advances (3600 seconds) the pulse counter should reset back to zero, and I'm not certain it does?`/*
/*
Clock experimental code. Timer module supplies 1 second pulses.
After 60 one second pulses are counted, stepper drive motor is moved to next 1 minute segment
Version Dated 30/06/2023
Uses A4988 stepper driver module
*/
// this constant won't change:
const int clockPin = 3; // clock signal input pin
const int ledPin = 13; // the pin that the LED is attached to
const int ledPinb = 9; // no used
#define stepPina 2 // minutes driver
#define stepPinb 7 // hours driver
#define dirPina 4 // minutes motor direction
#define dirPinb 5 // motor hours direction
#define enPina 6 // enable motor driver module minutes
#define enPinb 8 // enable motor driver module hours
// Variables will change:
int clockCounter = 0; // counter for the number of clcok pulses
int clockState = 0; // current state of the clock signal pulse
int lastClockState = 0; // previous state of the clock signal pulse
void setup() {
pinMode(clockPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(ledPinb, OUTPUT);
pinMode(dirPina, OUTPUT);
}
void loop() {
// read the clock input pin: read 1sec pulse
clockState = digitalRead(clockPin);
// compare the clockState to its previous state
if (clockState != lastClockState) {
// if the state has changed, increment the counter
if (clockState == LOW) { // LOW
// if the current state is HIGH then the clock went from off to on:
clockCounter++;
}
// save the current state as the last state, for next time through the loop
lastClockState = clockState;
if (clockCounter % 60 == 0) // 1 minute elapsed //// turns on the LED every 60 seconds
{
// digitalWrite(ledPin, HIGH); // not used
//digitalWrite (enPina, LOW); // stepper drive enable
digitalWrite(dirPina, HIGH); // Enables the motor to move in a particular direction
for (int x = 0; x < 63; x++) { // number of steps ( 5 > 10 > 15> 20.....)
digitalWrite(stepPina, HIGH); // enable driver module
digitalWrite(stepPina, LOW); // disable driver module
//digitalWrite (enPina,HIGH); // stepper drive enable
// digitalWrite(ledPin, LOW); // not used
}
//+++++++++++++++++++++++++++++++++++++++++++++++++ hour stepper driver +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if (clockCounter % 120 == 0) // 1 hour elapsed ( enter seconds) 120 = test only
{digitalWrite(dirPinb, HIGH); // motor direction
digitalWrite (enPinb,LOW); // enable driver module (A4988)
for (int x = 0; x <4; x++) { // number of steps
digitalWrite(stepPinb, HIGH); // start stepping hours motor
delayMicroseconds(7000);
digitalWrite(stepPinb, LOW);
delayMicroseconds(7000);
digitalWrite(enPinb, HIGH); // disable drive motor
}
}
}
}
}