Turn motor on every 2 hours, and off when pin is connected to ground

I have built this circuit:

I also ran accross this code and I have been able to test everything:

int pwm= 9;
 void setup(){
 // nothing goes here//
 }
 void loop(){
  analogWrite(pwm, 40);
  delay(1000);
  analogWrite(pwm, 60);
  delay(1000);
  analogWrite(pwm, 80);
  delay(3000);
  analogWrite(pwm, 0);
  delay(100000);
 }

What I am trying to to do is:

do nothing for roughly 2 hours
then slowly spin up motor (code above is about right)
stop motor when pinX is connected to ground after XX milliseconds
(pin will most likely remain connected to ground until motor spins again)

I also want to build in some safety in the form of never run the motor for longer then xx milliseconds.

If there is a similar project out there feel free to point me in that direction. I will continue to search for it in the meantime.

What MOSFET are you using to control your motor?

When the motor starts running:

  • set a motor_running flag
  • start a countdown timer for xx milliseconds
  • enable the pin change interrupt for pinX (which is moded as INPUT_PULLUP)
    ....
    If the interrupt fires or the timer runs out (whichever comes first), turn the motor off, clear the flag, reset the timer and turn it off and disable the pin change interrupt.

30N06L

We volunteer here to help you with coding problems.
We do not supply you with links of similar projects.

This is a very simple project.
If you have mastered the examples that come with the IDE, you should be able to get quite close to what you want.

When you use the delay function, all program flow stops until the delay time has expired. Probably not a good thing to do.
Read this Using millis() for timing. A beginners guide - Introductory Tutorials - Arduino Forum

FQP30N06L ?

The transistor should be wired as Q3 in this schematic.

I think you can solve your probkem witch free timers.
First is 2 hours long timer that switchs on the motor.
The other one is the safety timer than switchs off the motor compolsory after a few milliseconds
The third is a like timer that count the time when the button is pressed (or everyway the pin is connected to grd.

At the beginning the motor is stopped
You start first timer
At its and you start the motor, restart the long timer, and start the second one.
Every loop() you vontroll the pin, and if it is NOT vlnnected to gnd youbstart the third timer, so the only moment when the time runs is when the pin IS connected co gnd.
After it you check if the second or the third timer is passed. If so you stopped the motor.

Every timer have a specific variable, and "start" it means equal that variable to millis().

Do you understand?

I think I followed that logic. I am looking into how to code with multiple timers now.

Silente:
I think you can solve your probkem witch free timers.
First is 2 hours long timer that switchs on the motor.
The other one is the safety timer than switchs off the motor compolsory after a few milliseconds
The third is a like timer that count the time when the button is pressed (or everyway the pin is connected to grd.

At the beginning the motor is stopped
You start first timer
At its and you start the motor, restart the long timer, and start the second one.
Every loop() you vontroll the pin, and if it is NOT vlnnected to gnd youbstart the third timer, so the only moment when the time runs is when the pin IS connected co gnd.
After it you check if the second or the third timer is passed. If so you stopped the motor.

Every timer have a specific variable, and "start" it means equal that variable to millis().

Do you understand?

This link explains some techniques that would be appropriate:

https://forum.arduino.cc/index.php?topic=525240.0