How to run normal motor on every morning 6:00 for 2 seconds

HI

i am very new to Arduino programming.I was trying to create Fish food feeder. i almost created hardware part.

but i dont know hoe to write this. i created simple programe with some delay and the mortor is working fine with that delay.

How to run normal motor on every morning 6:00 for 2 seconds. If we can adjust the time by adding 16*2 LCD and some switches, that will be great.

please help me to solve this

thanks & Regards, Ben

Have you got a Real Time Clock (RTC) installed ?

Thanks for reply

No i am not aware of real time clock.

Can give right directions ?

Regards

You need to buy one because you can't use millis() if you need it to do something everymorning at 6:00am.

HI

thanks for replay. I was checking for Real time clock in shopping site and i got below one. Is this the same one i need to buy.?

http://www.mrbabu.com/sodial-r-tiny-ds3231-at24c32-i2c-module-precision-real-time-clock-module-for-arduino.html

That is exactly the sort that you need. The DS3231 is the preferred device for accurate timekeeping.

raschemmel: You need to buy one because you can't use millis() if you need it to do something everymorning at 6:00am.

For the OP's knowledge, can you also explain why not?

You can make a circadian clock, it will not run exact at 6:00 AM but it will be probably close enough. Monitor dawn and dusk with an LDR on an analog port. The middle in between (LDR dark) is midnight. 00:00:00 Rest can be calculated reasonably well with millis() synchronizes every day auto magically with solar time. see also - http://fgh.si/post/32338612783/circadian-clock-for-arduino -

For the OP’s knowledge, can you also explain why not?

Millis() is limited to the maximum size number the arduino can process , (232=4294967296, which comes out to 49.71026962962962962962962962963 days. (4294967296/1000/60/60/24=49.71026962962962962962962962963)

First you need to know when is 6:00am (at least the arduino does) . If you hardcoded it to start at a specific time knowing how many milliseonds remain to 6:00 am, you would still have to deal with the rollover every 49.7 days.

robtillaart is a real programmer (wheras I am a quasi-hack pseudo programmer) so he might be able to explain it better.

yes millis() rolls over when all possible values are used.

however the blink without delay method can come to a rescue. a day has 86400000 milliseconds so one could do

uint32_t lastTime = 0;
uint32_t day = 1000UL * 60 * 60 * 24;  // 86400000UL

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  uint32_t now = millis();
  if (now - lastTime >= day)
  {
    feederOn();
    delay(2);
    feederOff();
    lastTime = now;
  }
  // do other things here
}

only problem is to sync it so it starts at 6AM.... and yes it might drift - up to 10sec(?) / day

that is why the circadian clock is so funny, it adjusts itself every day .... (ok you need an offset between solar time and actual time)

No RTC needed. Run a clock program, just monitor the time variables and do something when 0:6:0:0 is reached. If you happen to lose power, restart it - or run it from battery:

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

byte hundredths;
byte tenths;
byte secondsOnes;
byte oldSecondsOnes;
byte secondsTens;
byte minutesOnes = 0;
byte minutesTens = 4;
byte hoursOnes = 1;
byte hoursTens = 1;

void setup(){

  Serial.begin(115200); // make serial monitor match
  currentMicros = micros();
  previousMicros = currentMicros;
  Serial.println ("Setup Done");
}

void loop(){

  currentMicros = micros();

  // how long's it been?
  elapsedTime = currentMicros - previousMicros;
  Serial.print ("Elapsed: ");  
  Serial.println (elapsedTime);
  if ( elapsedTime >=10000UL){  // 0.01 second passed? Update the timers
    elapsedTime = 0;
    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;
                hoursOnes = hoursOnes +1;
                if (hoursOnes == 10){
                  hoursOnes = 0;
                  hoursTens = hoursTens =1;
                  if (hoursOnes == 4 && hoursTens ==2){
                    hoursOnes = 0;
                    hoursTens = 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;
    Serial.print ("Time: ");
    Serial.print (hoursTens);
    Serial.print(hoursOnes);
    Serial.print(":");
    Serial.print(minutesTens);
    Serial.print(minutesOnes);
    Serial.print(":");
    Serial.print(secondsTens);
    Serial.println(secondsOnes);

/*
 if ( hoursTens == 0 && hoursOnes == 6 ** minutesTens == 0 && minutesOnes == 0 && secondsTens == 0 && secondsOnes == 0){
// alarm time!
}
*/

  } // end one second check
} // end loop
              } // minutesTens rollover check
            } // minutesOnes rollover check
          } // secondsTens rollover check
        } // secondsOnes rollover check
      } // tenths rollover check
    } // hundredths rollover check
  } // hundredths passing check

if ( hoursTens == 0 && hoursOnes == 6 ** minutesTens == 0 && minutesOnes == 0 && secondsTens == 0 && secondsOnes == 0){

Ick. This is why I prefer to use Unix epoch timestamps.

#define MINUTE 60UL
#define HOUR 3600UL // MINUTE * 60
#define DAY 86400UL // HOUR * 24
#define WEEK 604800UL // DAY * 7

unsigned long actionTime = 0;

if ( now() > actionTime) // time to do stuff yet?
  {
  // do stuff
  actionTime = actionTime + DAY; // re-schedule for same time tomorrow
  }

That's what happens when you let the hardware guys write the code 8)

The advantage of my way is that you're not doing a bunch of slow modulo stuff to break out the digits - they're already broken out - and all ready to send to a shift register for 7-segment display or similar.

CrossRoads: The advantage of my way is that you're not doing a bunch of slow modulo stuff to break out the digits - they're already broken out - and all ready to send to a shift register for 7-segment display or similar.

Fair enough, but I'd rather push off the Unix -> digits stuff into a single function than have to constantly deal with all over the place.

All over the place? Loop as written is that one function.
For hardware output use, I replace the Serial.prints with SPI.transfer commands to send the data out with a font lookup:

digitalWrite (ssPin, LOW);
SPI.transfer(fontArray[HoursTens]);
SPI.transfer(fontArray[HoursOnes]);
SPI.transfer(fontArray[MinutesTens]);
SPI.transfer(fontArray[MinutesOnes]);
SPI.transfer(fontArray[SecondsTens]);
SPI.transfer(fontArray[SecondsOnes]);
digitalWrite(ssPin, HIGH);

Or have all the time elements in an array, and just loop thru the array:

digitalWrite (ssPin, LOW);
for (x=0; x<6; x=x+1){
SPI.transfer(fontArray[timeElements[0]]);
}
digitalWrite(ssPin, HIGH);

CrossRoads:
All over the place?

I mean every time I want compare one time to another or schedule something to happen in the future. Unix timestamps can be evaluated with <, ==, > instead of a bunch of && for each digit.

HI All

after seeing all of these , i got confused. Which one i need to follow?

Regards, Ben

My advice would be to go with using millis() in the first instance. It will not be entirely accurate but only requires one comparison each time through loop() to establish whether the required period has elapsed.

Look at the solution suggested by robtillaart. Other solutions may be more accurate, neater, cleverer etc but you have to start somewhere.

bentech4u: after seeing all of these , i got confused. Which one i need to follow?

Don't mind us, we like to wax philosophical here. :)

As for which one to use, you need to decide if you're okay with a little drift (after a few days, your motor runs at 6:01am or 5:59am, and a few days later, at 6:02am or 5:58am, etc).

If you're okay with drift, then robtillaart's reply #8 is simple and will work just fine for you.

If you're not okay with drift, then you either need to be near a window so you can use a sensor to detect sunrise/sunset, or you need to use a DS3231 RTC.

HI

Little time changes are ok for me as this is to make a fish food feeder.

is it possible to print the time to one lcd (16*2) lcd module. so that i can able to see the status noe? like every day it started on what time and all

i have uploaded the code to IDE and i got some error like below:

#define MOTOR1 3

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


byte hundredths;
byte tenths;
byte secondsOnes;
byte oldSecondsOnes;
byte secondsTens;
byte minutesOnes = 0;
byte minutesTens = 4;
byte hoursOnes = 1;
byte hoursTens = 1;

void setup(){
  pinMode(MOTOR1,OUTPUT);
  Serial.begin(115200); // make serial monitor match
  currentMicros = micros();
  previousMicros = currentMicros;
  Serial.println ("Setup Done");
}

void loop(){

  currentMicros = micros();

  // how long's it been?
  elapsedTime = currentMicros - previousMicros;
  Serial.print ("Elapsed: ");  
  Serial.println (elapsedTime);
  if ( elapsedTime >=10000UL){  // 0.01 second passed? Update the timers
    elapsedTime = 0;
    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;
                hoursOnes = hoursOnes +1;
                if (hoursOnes == 10){
                  hoursOnes = 0;
                  hoursTens = hoursTens =1;
                  if (hoursOnes == 4 && hoursTens ==2){
                    hoursOnes = 0;
                    hoursTens = 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;
    Serial.print ("Time: ");
    Serial.print (hoursTens);
    Serial.print(hoursOnes);
    Serial.print(":");
    Serial.print(minutesTens);
    Serial.print(minutesOnes);
    Serial.print(":");
    Serial.print(secondsTens);
    Serial.println(secondsOnes);


 if ( hoursTens == 0 && hoursOnes == 6 ** minutesTens == 0 && minutesOnes == 0 && secondsTens == 0 && secondsOnes == 0){
  digitalWrite(MOTOR1,HIGH);
  delay(100);
  digitalWrite(MOTOR1,LOW);
  
}


  } // end one second check
} // end loop

and the error is

sketch_oct18a.ino: In function 'void loop()':
sketch_oct18a:84: error: invalid type argument of 'unary *'

please guide me to fix this

thanks
Ben