Multiple alarms during defined period. RTC?

Hi Folks,

I have had a go at one of the suggestions to my previous question. I have linked in an LDR and after a few hours over a few days - I have the code working!. Just blinking an LED at this stage but I read the concept as going to plan. However, I have a couple more questions if I may:

  1. I understand how to adjust the time between feeds (long interval value), but I would like to be able to control the time of the feed as well. For example - When the LDR is receiving light the feeder will activate every 20mins, but I would like it to be active for only about 4-5 secs (dispensing food), then shut off, then after another 20 mins, the feeder dispenses again for 4-5 secs then shuts off and so on until it becomes dark and the LDR stops the loop. The way I read the code now is that food is dispensed every 20 mins for 20mins - that's alot of wasted feed! How would I do this without including a delay? Could I include for example #define ONTIME 4000, and put that in the loop somewhere? I presume this would go with the
    if (ledState == LOW)
    ledState = HIGH
    Not sure on this one..

  2. I have read that after 50 days or so the millis will reach a maximum number and cease operating. I will not always be able to reset the unit around this time, so is there any way to make the counting process reset itself - maybe at the end of the day when the LDR stops the loop when it gets dark? Or am I just misreading the code and as it stands it will run permanently without need for adjustment?

I have attached the code for some constructive criticism if you have time. This is my first attempt at altering any code so opens to all suggestions.

Thanks kindly.

/* Blink without Delay

Turns on and off a light emitting diode(LED) connected to a digital
pin, without using the delay() function. This means that other code
can run at the same time without being interrupted by the LED code.

The circuit:

  • LED attached from pin 13 to ground.
  • Note: on most Arduinos, there is already an LED on the board
    that's attached to pin 13, so no hardware is needed for this example.

created 2005
by David A. Mellis
modified 8 Feb 2010
by Paul Stoffregen

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/

// constants won't change. Used here to
// set pin numbers:
const int ledPin = 13; // the number of the LED pin
const int sensorPin = A0;

// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
unsigned int sensorValue = 0;

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000; // interval at which to blink (milliseconds) 20mins = 1,200,000 - this is the one to adjust for time BETWEEEN feeds.

void setup() {
// set the digital pin as output:
pinMode(13, OUTPUT);
pinMode(A0, INPUT);
Serial.begin (9600);

}

void loop()
{

//int rate = analogRead(sensorPin);
//float volts = (rate/1023.0) * 5.0;
//int rawRate = rate;

sensorValue = analogRead(sensorPin);

if (sensorValue >700); {

unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
if (ledState == LOW)
ledState = HIGH;
else ledState = LOW;
}
{
if(sensorValue<700)
ledState = LOW;

// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);}
}
{

//Serial.print("Raw Rate: ");
//Serial.println(rawRate);

//Serial.print("Map Rate: ");
//Serial.println(rate);

//Serial.print("Volts DC: ");
//Serial.println(volts);
//Serial.println();

//Serial.print("Led state: ");
//Serial.println(ledState);

//delay(1000);

}
}