Arduino linear clock


I warn in advance, I am not very experimented with the arduino environment.

Anyway, I have this code;

#include <Adafruit_MotorShield.h>
#include “utility/Adafruit_MS_PWMServoDriver.h”</adafruit_motorshield.h></wire.h>

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);

// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotor1 = AFMS.getStepper(300, 1);
Adafruit_StepperMotor *myMotor2 = AFMS.getStepper(300, 2);
boolean motorRunning = false;
unsigned long motorStartMillis;
unsigned long motorRunMillis = 180000;

int delaylegnth = 7;

void setup() {
//start serial connection
//configure pin2 as an input and enable the internal pull-up resistor
pinMode(2, INPUT_PULLUP);

// Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println(“Stepper test!”);

AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz

myMotor1->setSpeed(100); // 10 rpm


void loop(){

//read the pushbutton value into a variable
int sensorVal = digitalRead(2);
sensorVal == LOW;
int delayL = 200;

if (sensorVal == LOW) {

Serial.println(“Minutes ++”);
//myMotor1->step(1640, BACKWARD, DOUBLE);

for (int i=0; i <= 10; i++){
myMotor1->step(147, BACKWARD, DOUBLE);
//analogWrite(PWMpin, i);

Serial.println(“Hours ++”);
myMotor1->step(1615, FORWARD, DOUBLE);

//myMotor2->step(1600, BACKWARD, DOUBLE);
myMotor2->step(220, FORWARD, DOUBLE);

else {

//Serial.println(“Double coil steps”);
myMotor1->step(0, FORWARD, DOUBLE);
myMotor1->step(0, BACKWARD, DOUBLE);

Instead of running the stepper 147 steps for 10 times (with delay between each time), I’d like to run for 1 second precisly and then a delay of 5 minutes for example. How could I run the stepper for a certain amount of time instead of steps? I know, steps are meant to be precise but … anyway.


How could I start a timer as soon as the stepper motor starts it’s steps?


Thank you!
Sorry if it’s not so clear…

How could I run the stepper for a certain amount of time instead of steps?

I can't imagine why you would want to do that, but, the blink without delay example illustrates how to use millis() to control when things happen. When whatever even occurs to cause you to want to start stepping aimlessly, record when that happens (using millis()), and set a flag, steppingUselessly to true.

Periodically (on every pass through loop(), see if it is necessary to step AND time to step. If so, step.

Periodically (on every pass through loop(), see if it is time to quit stepping uselessly. If so, clear the flag and the time.

Understand that steppers do NOT act like regular electric motors, spinning happily because there is voltage applied. They step ONLY when you tell them to step.

The problem that I am experiencing is when the stepper starts to step, it steps until the end and then the delay is reached.

It is supposed to be a clock and I don't know exactly how much time the motor is taking to step his 147 steps. What I want is; as soon as the motor begins his 147 steps, a countdown of 5 minutes would be enabled. As soon as the the counter is over, it would step an other 147 and wait again 5 minutes. Repeat for 10 times.

Like I said, I am a beginner with arduino, so don't assume that I know how everything works....

Thank you!
How could I start a timer as soon as the stepper motor starts it's steps?

There is a function called millis.

To measure how much time has passed, we can do something like this:

// declare a variable which we will need
unsigned long startTime;

// use that variable to remember when we started something
startTime = millis();

// later, you can calculate elapsed time thus:
unsigned long elapsedTime = millis() - startTime;
// (elapsedTime will be in milliseconds)
Adafruit_StepperMotor *myMotor1 = AFMS.getStepper(300, 1);
myMotor1->setSpeed(100);  // 100 rpm

300 steps per revolution * 100 revolutions per minute = 30000 steps per minute

30000 steps per minute / 60 seconds per minute = 500 steps per second

Move 500 steps.

Thank you @odometer and @johnwasser! I'll try some things! You give me good cues.