rotating a DC motor incrementally how to stop after 1 minute?

I am in serious need of help.

I am rotating a motor with incrementally from 0 to 90 degrees.
but i dont know how to stop the motor after 1 minute.

can someone please help me with an alternative?
or tell me how to add a stop function at 1 minute

const unsigned long eventInterval_1 = 14000;
const unsigned long eventInterval_2= 14020;
const unsigned long eventInterval_3= 30000;
unsigned long previousTime1 = 0;
unsigned long previousTime2= 0;
unsigned long previousTime3= 0;

void setup() {
pinMode (5, OUTPUT); //motor speed
pinMode (7, OUTPUT); //motor input 1
pinMode (2, OUTPUT); //motor input 2


void loop() {
/* Updates frequently */
unsigned long currentTime = millis();

if (currentTime - previousTime1 >= eventInterval_1){
analogWrite (5,107);
digitalWrite (7,HIGH);
digitalWrite (2,LOW);
previousTime1 = currentTime;
//motor spins clockwise ever 14 seconds

if (currentTime - previousTime2 >= eventInterval_2){
analogWrite (5,127);
digitalWrite (7,LOW);
digitalWrite (2,LOW);
previousTime2 = currentTime;
//motor stops every 14.02 seconds

Use CTRL T to format your code.

Attach your ‘complete’ sketch between code tags

[code]Paste your sketch here[/code]

So, your sketch rotates the motor from 0 to 9 over 60 seconds at 1.5 degrees per second, and then it stops and that's it? That's all the sketch does?

Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What is the motor you are using?
Can you post a link to data/specs please.

Thanks.. Tom.. :slight_smile:

I am rotating a motor with incrementally from 0 to 90 degrees.
but i dont know how to stop the motor after 1 minute.

i'm puzzled. i'm surprised you can turn a motor 1/4 rotation simply by providing power for a specific amount of time without any feedback (e.g. position switch, encoder).

but looking at your partial code listing, it looks like your multiple conditional tests using currentTime can get out of sync with one another.

it looks like Interval_1 is intended to turn the motor on and Interval_2, turn it off, that you want to turn the motor on for 20 msec every 14 sec. but these intervals will quickly become out of sync. the motor will be turned on every 14 sec, but will be turned off at 14.02, 28.04, 42.06 -- resulting in the motor being on for longer and longer periods.

seems that you need to recognize what state the motor is in (on/off) and wait the desired amount of time.

if you start with the motor off, the first event is to turn it on, wait 20 msec and turn it off, wait 13.98 secs and repeat.

there also needs to be a counter that does this a specific number of times to presumably stop when the motor rotates a 1/4 turn

Can you tell us the application?
What does the motor drive?
Will the load on the motor vary? If so how will you ensure your increments will not change due to the load?

Thanks.. Tom... :slight_smile: