Need sample code for stepper motor to move based on real time!!

Hi guys,

I am doing a project which required 2 stepper motor to move at certain time.For example, at 8.00 am stepper1 will move 2 steps and so on. I am new to arduino would be grateful if anyone can give me sample coding.

Without some sort of clock, all you can do is count the millisenconds. The main thing is that milliseconds wrap after a few hours, so

a) you need to accumulate the hours in a separate spot; and b) you cant just go "wait for 5 minutes from now", because five minutes from now the time might have wrapped around.

You can think of the wraparound as being a bit like a vehicle odometer. Let's say your odo reads '999950', and you want to wait for a minute from now. If you go "wait till the time is >= 60 seconds from now", your event will fire immediately, because 60 seconds from now is '000010' and the time now is already greater than that.

Instead, you have to say "wait until the time, minus the time now, is greater than 60". This will work because addition and subtraction wrap around. 000010 - 999950 actually does come out to 60.

A big problem is that you need to be able to tell the program what time it is now. If you go "wait for 10 hours until 8AM", then you have to start the program at exactly 10PM or it won't work.

So you will probably need a "set the time" function. Something like "count the number of clicks on button 1, treat it as hours, and flash output 13 (the onboard LED) to indicate what time has been set".

The main thing is that milliseconds wrap after a few hours

yep, they go from 0xffffffff back to 0 every 49.71 days.

If you measure elapsed time,

if ((currentTime - previousTime) >= duration){
// do something

then it will not matter if it wraps unless you wait more than 0x80000000 mS (1/2 of 49.71 days).

Arduino millisecond is also not accurate time keeper. You need an RTC.

Arduino micros() is pretty timekeeper tho. Try this:

unsigned long currentMicros;
unsigned long previousMicros;
unsigned long elapsedTime;

byte hundredths;
byte tenths;
byte secondsOnes;
byte oldsecondsOnes;
byte secondsTens;
byte minutesOnes;
byte minutesTens;

void setup(){

Serial.begin(115200); // make serial monitor match
Serial.println (Setup Done");

void loop(){

currentMicros = micros();

// how long's it been?
elapsedTime = currentMicros - previousMicros;
if ( elapsedTime >=10000UL){  // 0.01 second passed? Update the timers
previousMicros  = previousMicros + 10000UL;
hundredths = hundredths+1;
if (hundredths == 10){
    hundredths = 0;
    tenths = tenths +1;
    if (tenths == 10){
        tenths = 0;
        secondsOnes = secondsOnes + 1;
        if (secondsOnes == 10){
            secondsOnes = 0;
            secondsTens = secondsTens +1;
            if (secondsTens == 6){ 
                secondsTens = 0;
                minutesOnes =  minutesOnes + 1;
                if (minutesOnes == 10){
                    minutesOnes = 0;
                    minutesTens = minutesTens +1;
                    if (minutesTens == 6){
                        minutesTens = 0;
                        } // minutesTens rollover check
                     } // minutesOnes rollover check
                 } // secondsTens rollover check
              } // secondsOnes rollover check
          } // tenths rollover check
       } // hundredths rollover check
} // hundredths passing check

if (oldSecondsOnes != secondsOnes){  // show the elapsed time
oldSecondsOnes = secondsOnes;





} // end one second check

} // end loop

You may be interested in stepper motor basics and this simple stepper code


Thank you for everyone help. How to write that condition case to move the motor provided i got RTC?sorry im new to arduino in the progress of learning

Use the Time.h and TimeAlarms.h library.