using multiple millis() to replace multiple delay() within the same loop

Hi Everyone,

I'm a bit new to Arduino and cannot figure out how to run my loop function in the background so that it does not delay the actual program.

I am currently using the delay() inside my loop but I have external inputs that need to be accounted for. From what I understand, all the delay() need to be removed so that the rest of the program is not delayed. I have been trying to figure out the millis() but I am ripping my hair out...

Is it possible to run the following on just the millis()?
If so how can I do that?

boolean evening_feed = 0;
int FEEDERON_pin = 7; //digital pin on
int FEEDEROFF_pin = 8; // digital pin off
int HR;//hours
int MIN;//minutes
#include <WProgram.h>
#include <Wire.h>
#include <DS1307.h> // written by mattt on the Arduino forum and modified by D. Sjunnesson

void setup()
{
Serial.begin(9600);

RTC.stop();
RTC.set(DS1307_SEC,46); //set the seconds
RTC.set(DS1307_MIN,59); //set the minutes
RTC.set(DS1307_HR,13); //set the hours
RTC.set(DS1307_DOW,1); //set the day of the week
RTC.set(DS1307_DATE,17); //set the date
RTC.set(DS1307_MTH,4); //set the month
RTC.set(DS1307_YR,11); //set the year
RTC.start();

}

void loop()
{
////////////////////////////SETUP

Serial.println();

Serial.print(RTC.get(DS1307_HR,true)); //read the hour and also update all the values by pushing in true
Serial.print(":");
Serial.print(RTC.get(DS1307_MIN,false));//read minutes without update (false)
Serial.println();
HR = (RTC.get(DS1307_HR,false));
pinMode (FEEDERON_pin, OUTPUT);
pinMode (FEEDEROFF_pin, OUTPUT);

/////////////////LOOP
{

if (RTC.get(DS1307_HR, false) == 12 || (RTC.get(DS1307_HR, false) == 13))
{
evening_feed = 1;

Serial.println("inside evening feed loop"); // confirm inside morning feed

while (evening_feed == 1) //when evening_feed = 1 look for the time until
{
(HR = (RTC.get(DS1307_HR,true)));
(MIN = (RTC.get(DS1307_MIN,false)));

//then look for time every min until

Serial.println("inside evening while loop"); //confirm inside while loop

Serial.print(RTC.get(DS1307_HR, false));
Serial.print(":");
Serial.println(RTC.get(DS1307_MIN, false));

if ( HR == 14 && MIN == 1 && evening_feed )
{
digitalWrite(FEEDERON_pin, HIGH);
Serial.println( "feed me evening!!!!");
delay (20) ;
digitalWrite(FEEDERON_pin, LOW);
Serial.println("feed me evening low");

delay (3000);

digitalWrite(FEEDEROFF_pin, HIGH);
Serial.println("feedoff evening high");
delay (20) ;
digitalWrite(FEEDEROFF_pin, LOW);
Serial.println("feedoff evening low");
afternoon_feed = 0;

delay(30000);
}
}
delay(15000);
}
}

Thank you for your help in advance!
Shane

When posting code, please use the "#" button in the post editor to add the code tags, which makes it much easier to read.

millis() is not a direct replacement for delay(). It requires you to add some logic to your code based on how long it has been running. Look at this simple introduction to millis(): http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

At first glance of your code, few events occur outside of your if-statements. Those statements appears to be based on time from the RTC. So why even put the delays() in there anyway?

shgarshane:

void setup()

{
  Serial.begin(9600);

RTC.stop();
  RTC.set(DS1307_SEC,46);        //set the seconds
  RTC.set(DS1307_MIN,59);     //set the minutes
  RTC.set(DS1307_HR,13);       //set the hours
  RTC.set(DS1307_DOW,1);       //set the day of the week
  RTC.set(DS1307_DATE,17);       //set the date
  RTC.set(DS1307_MTH,4);        //set the month
  RTC.set(DS1307_YR,11);         //set the year
  RTC.start();

}

Do you really want to do this? Once the clock is set, it stays set. If you leave that there, it will always set the time back to 13:59:46.