Controlling a motor with arduino

Hello all,
I am making a project using an Arduino Mega ADK and was wondering if anyone with more knowledge of Arduinos would be able to help me out. I am connecting the arduino to a small DC motor and want to code it so that once a day the motor will turn clockwise for 10 seconds, pause for 5 seconds, and then turn anticlockwise for 10 seconds.
The application of the motor is opening a door which allows feed to fall through. I am aware I need a H-Bridge to vary the way the motor spins, but would anyone be able to help me out with the code?
Thanks in advance,
Seand16

Check out how to use millis()

Beginner's guide to millis()

That will give you a time reference in milliseconds. From there you can detect your events (day, 10s forward, 5s pause, 10s reverse).

Being a first project, I would set it up so that when you turn it on the door sequence runs. Call that 0 hour at 24 hours later it will do it again. You can get more complicated using a real-time clock, but that is way more complicated.

Program it up using millis() and show us what you have or where you may be having an issue.

P.S. An Arduino Mega ADK is way more power than this needs. An Uno or Nano can also handle this at much less the cost.

Thanks for the reply, I have been reading through the millis() there, is that the best way you consider to do it? Because I am very in experienced using arduinos and arduino coding and am not sure if I will be able to do it?
Would it not be simpler to do a loop that goes on for 10, delays for 5, goes on for 10 {in opposite direction), then delays for 86375 seconds (23hours 59 minutes and 35 seconds so the loop starts at the same time everyday).
Thanks in advance

Depends on how accurate you want it to be as millis() may be off by a few minutes a day or more. That adds up quickly, after two weeks that's an hour.

If it's something that needs to be done at a fixed time of the day, use a RTC (real time clock). Also the millis() method won't remember how much time passed in case of a power outage.

Yes using delays would work. But using millis() in the long run (both project and learning) is better. Better to learn do to something the better way than have to reteach yourself later.

From best to worst:

  1. Use an RTC
  2. Use millis()
  3. Use delay

My most frank opinion on how to learn and proceed with this project would be to learn and use millis() first with a "short day" (5 minutes or so). That will get the program layout started and get your motor to run forward and reverse. Then Save As... to version to and incorporate an RTC to determine the 24 hour mark (still use millis for the seconds delays). Adafruit offers a RTC (DS3231) (Adafruit DS3231 Precision RTC Breakout : ID 3013 : $17.50 : Adafruit Industries, Unique & fun DIY electronics and kits). I have found they have fairly thorough examples and explanations on how to use the modules.

Hi seand16,

Welcome, have a few suggestions in reguard to the feeder.

Dont just time the motor forward reverse as if things go wrong your motor burns out. Use limiting switches, one for door open and one for door close that way you shut off the motor when the door reaches the right position.

With ref to your daily timer, add a LDR (Light Dependent Resistor) so you can detect day and night then you start the timer from sunrise this limits your error to one day before being reset.

Both of these are easy to add and will remove accumulated errors of just running timers.

I would still use a timer on the motor run time so if the door is ever stuck you can detect it by the fact that the door has not reached the limit switch and the timer runs out. Then set off an alarm.

Main thing is do a rough sketch of what you are putting in and then do a flowchart of how it works. This will make it easyier, to lay the program out and if you need help it will allow others here to see where you are going with the program.

Daz

I think the millis() timer is far more precise than sunrise/sunset time detection by LDR will ever be.
The only thing going for the LDR method is that over the year it'll trigger every day sometime during the morning - when that sometime is depending on the weather and the season - while millis() will end up slowly getting earlier or later.

If this 'door' is within range of WiFi, then you could also use a WeMos D1 mini or NodeMCU etc.
Those tiny $4 boards can get the time off the internet, so you never have to set the clock again.
If you're a smart coder, then you can even change the feed settings from your laptop or smartphone.
Leo..

I only mention the use of the LDR for sunrise because my sister has been trying to get her chooks to go by the clock but they ignore it and get up with the sun.

Daz.