Help with stepper motor sketch!

Hello, i'm sort of a beginner here and need some help and advice!

My project involves two stepper motors and a strobe light.
So far i have a sketch that works (motors and strobe turning continuasly), but i would like to add a timer function (if this is possible), so that every 30 seconds, it stops for 30 seconds and then starts again?

this is the code i have so far:

#include <AFMotor.h>

AF_Stepper motor(100, 1);

int triggerPin = 13;

void setup() {
pinMode(triggerPin, OUTPUT);
Serial.println("Stepper test!");



void loop() {

digitalWrite (triggerPin, HIGH);
motor.step(5, FORWARD, DOUBLE);



Is this possible with scripting or do i need to add an external timer?

The millis() function returns the length of time since the Arduino was reset. You can use that information to toggle the state of a boolean variable. When the state is true, move the steppers and flash the strobe. When it is false, don't.

Why are you releasing the stepper in loop()?

As i said i am a beginner and to be honest don't really know what i am doing!
This is a project that I started more than a year ago, where i mostly had help writing the original script which worked with three shields, a sensor, 6 motors and a strobe.
I am now trying to simplify it all(without that person that originally helped me), basing myself on those old sketches, not fully remembering/understanding what it is i am doing!

Help me please!!!

i'm sorry to bother you all with this but, i don't really know where to turn and i need to get this up and running by thursday!

Add this before setup:

unsigned long lastRun = 0;
unsigned long runTime = 30000UL;
boolean running = true;

In loop(), add this at the top:

unsigned long timeNow = millis();
if(timeNow - lastRun > runTime)
   running = !running;
   lastRun = timeNow;


Add this to the bottom of loop():


Select all the (existing code) between the { after the if(running) statement and the new } at the end, and increase the indent level one position.

This will set the state initially to running, and indicate that it started running at 0.

When loop gets called, millis() will return a value that is greater than 0, but less than 30 seconds (30000 milliseconds), so the state stays constant, and the loop code runs, because running is true.

After a while, when loop gets called, millis()'s return value WILL be greater than 30 seconds, so the state is changed to not running, and the time that it began not running is set to now. The body of the if running statement is not executed, since running is false.

After a while, when loop gets called, millis()'s return value will have exceeded the last state change time by 30 seconds, and the state changes to running again, and the time that the state change occurs is recorded, and the it running block is again executed, for another 30 seconds.

Thank you so much!!!

It worked i just added this at the bottom as the triggerpin 13(the strobe) was not responding without it.

else {
digitalWrite (triggerPin, LOW);

Does that seem right?
Is the motor.release() necessary?
But now it seems to be working just fine!
I'm not sure i fully grasp exactly how the millis() works the second time around, but it works!
just incase you're interested a few pictures of the project in it's previous form:

And if i may just one more question :stuck_out_tongue:

would it be very complicated to get the runtime and not running time to be different: like 30 seconds running and 1 minute not running?

would it be very complicated to get the runtime and not running time to be different: like 30 seconds running and 1 minute not running?

Not too difficult.


unsigned long notTime =  60000UL;
unsigned long interval = notTime;

after the declaration or runTime.

Change the if test to use interval instead of runTime;


  interval = notTime;
  interval = runTime;

after changing the state of running.

If this doesn't work like you want, it may be necessary to invert the values assigned to interval. I haven't tested that the correct assignments are made.