Run dc motor for certain amount of time.

I’m brand new to this. I’ve looked into the forums and can’t find an answer to my question. I am running a dc motor with the following code. I would like the motor to stop after three hours. Thanks for reading.

int motorPin = 9;

void setup() {
pinMode(motorPin, OUTPUT);
Serial.begin(9600);
while (! Serial);
Serial.println(“Speed 0 to 255”);
}

void loop() {
if (Serial.available()) {
int speed = Serial.parseInt();
if (speed >= 0 && speed <= 255) {
analogWrite(motorPin, speed);
}
}
}

Three hours… after what?

after I input the speed. The speed will be 40 inputed in the serial monitor. I know I sound clueless. sorry

You could define a 'variable' that stores a reference 'arduino' time ... eg.

int motorPin = 9;
unsigned long ref_time = 0;
int flag = 0;

.... just before running the motor, you have to set the reference time to arduino's current time...eg.

if (flag == 0) {
ref_time = millis();
flag = 1;
}

And ... in your loop, you keep checking to see if the arduino's time (which keeps building up) has surpassed the reference (starting) time by 3 hours. And if the condition where 3 hours has elapsed and is detected, then set the speed of the motor to zero.

eg.

if (  ((millis() - ref_time)  >= 10800000)   &&   (flag == 1)  )       {
      speed = 0;
      flag = 0;
}

Use millis() based timing:
Record the time whenever you last entered a number.
When 3 hours (3 * 3600 * 1000 milliseconds) have passed, stop the motor (analogWrite(motorPin, 0)).
Now as long as you enter something within that three hours it will continue to run for another three hours.

Or is it supposed to stay put at that speed for three hours not accepting a new speed, then stop, and only start again if you enter a new speed?

Does it have to be exactly three hours? Or is a minute or two more or less acceptable? The latter is the accuracy you can expect from the millis() counter. If you need more accuracy you have to add an RTC (real time clock). They can keep track of the three hours to well within a second.

It does not have to be exactly three hours. Do you know what code I have to add to this?

int motorPin = 9;

void setup() {
pinMode(motorPin, OUTPUT);
Serial.begin(9600);
while (! Serial);
Serial.println(“Speed 0 to 255”);
}

void loop() {
if (Serial.available()) {
int speed = Serial.parseInt();
if (speed >= 0 && speed <= 255) {
analogWrite(motorPin, speed);
}
}
}

I want it to run at 40 speed for three hours after I select the speed in the serial monitor. I then want the motor to stop after approximately three hours of running.

dirkdiggles15:
I want it to run at 40 speed for three hours after I select the speed in the serial monitor.

Why selecting the speed in the Serial monitor if you're going to ignore it and run the motor at speed 40 anyway?

Because that's the only code I've learned to run the motor at that speed. If you have better code to run it at that speed without having to input it into the serial monitor that would work.

analogRead(motorPin, 40);

sets the motor speed to that value.