switching from datetime library to time library

This is my first project not in the getting started with arduino book from Make:projects and I think I bit of more than I can chew. I am doing the garduino project from make magazine which was written using the datetime library. I cannot find this library as it is superceded by the time library. I have fiddled with the code to no avail and am hoping someone can give me advice on how to change it.
This is the code after I tried to modify it but when I serial print it gives me a value of 0 on all time variables so I know I’m not thinking right here.
Hope you can help an overly zealos garden builder.

//include the datetime library, so our garduino can keep track of how long the lights are on
#include <Time.h>

//define analog inputs to which we have connected our sensors
int moistureSensor = 0;
int lightSensor = 1;
int tempSensor = 2;

//define digital outputs to which we have connecte our relays (water and light) and LED (temperature)
int waterPump = 7;
int lightSwitch = 8;
int tempLed = 2;

//define variables to store moisture, light, and temperature values
int moisture_val;
int light_val;
int temp_val;

//decide how many hours of light your plants should get daily
float hours_light_daily_desired = 14;

//calculate desired hours of light total and supplemental daily based on above values
float proportion_to_light = hours_light_daily_desired / 24;
float seconds_light = 0;
float proportion_lit;

//setup a variable to store seconds since arduino switched on
float start_time;
float seconds_elapsed;
float seconds_elapsed_total;
float seconds_for_this_cycle;

void setup() {
//open serial port
Serial.begin(9600);
//set the water, light, and temperature pins as outputs that are turned off
pinMode (waterPump, OUTPUT);
pinMode (lightSwitch, OUTPUT);
pinMode (tempLed, OUTPUT);
digitalWrite (waterPump, LOW);
digitalWrite (lightSwitch, LOW);
digitalWrite (tempLed, LOW);

//establish start time
setTime (start_time);
time_t start_time = now();
seconds_elapsed_total = 0;

}
void loop() {
// read the value from the moisture-sensing probes, print it to screen, and wait a second
moisture_val = analogRead(moistureSensor);
Serial.print("moisture sensor reads ");
Serial.println( moisture_val );
delay(1000);
// read the value from the photosensor, print it to screen, and wait a second
light_val = analogRead(lightSensor);
Serial.print("light sensor reads ");
Serial.println( light_val );
delay(1000);
// read the value from the temperature sensor, print it to screen, and wait a second
temp_val = analogRead(tempSensor);
Serial.print("temp sensor reads ");
Serial.println( temp_val );
delay(1000);
Serial.print("seconds total = ");
Serial.println( seconds_elapsed_total );
delay(1000);
Serial.print("seconds lit = ");
Serial.println( seconds_light);
delay(1000);
Serial.print("proportion desired = ");
Serial.println( proportion_to_light);
delay(1000);
Serial.print("proportion achieved = ");
Serial.println( proportion_lit);
delay(1000);

//turn water on when soil is dry, and delay until soil is wet
if (moisture_val < 850)
{
digitalWrite(waterPump, HIGH);
}

while (moisture_val < 850)
{
delay(10000);
}

digitalWrite(waterPump, LOW);

//update time, and increment seconds_light if the lights are on
seconds_for_this_cycle = start_time = now() - seconds_elapsed_total;
seconds_elapsed_total = start_time = now() - start_time;
if (light_val > 900)
{
seconds_light = seconds_light + seconds_for_this_cycle;
}

//cloudy days that get sunny again: turn lights back off if light_val exceeds 900. this works b/c the supplemental lights aren't as bright as the sun:)
if (light_val > 900)
{
digitalWrite (lightSwitch, LOW);
}

//turn off lights if proportion_lit>proportion_to_light, and then wait 5 minutes
if (proportion_lit > proportion_to_light)
{
digitalWrite (lightSwitch, LOW);
delay (300000);
}

//figure out what proportion of time lights have been on
proportion_lit = seconds_light/seconds_elapsed_total;

//turn lights on if light_val is less than 900 and plants have light for less than desired proportion of time, then wait 10 seconds
if (light_val < 900 and proportion_lit < proportion_to_light)
{
digitalWrite(lightSwitch, HIGH);
delay(10000);
}

//turn on temp alarm light if temp_val is less than 850 (approximately 50 degrees Fahrenheit)
if (temp_val < 850)
{
digitalWrite(tempLed, HIGH);
}

}

Thanks for looking
Dan

//setup a variable to store seconds since arduino switched on
float start_time;
float seconds_elapsed;
float seconds_elapsed_total;
float seconds_for_this_cycle;

None of the values that would reasonably be stored in variables with these names are floats. They are unsigned long.

setTime (start_time);

What time did you initialize start_time to?

time_t start_time = now();

A local variable that immediately goes out of scope. How useful is that?

A local variable with the same name as a global. How good an idea is that?

while (moisture_val < 850)
{
delay(10000);
}

Why does it matter how many times the while loop iterates? Get rid of the delay() call in there. It is doing nothing.

Thanks for your help. I think the real problem is I don’t understand how to initialize the time.
Quote

//setup a variable to store seconds since arduino switched on
float start_time;
float seconds_elapsed;
float seconds_elapsed_total;
float seconds_for_this_cycle;
None of the values that would reasonably be stored in variables with these names are floats. They are unsigned long.
I can see this so I rewrote it to usigned long.

Code:

setTime (start_time);
What time did you initialize start_time to?

I don’t understand how to initialize start time. Do I just need to give it a starting value like this?

//establish start time
start_time = 1;
setTime (start_time);
seconds_elapsed_total = 0;

And on this I thought it was letting the switch operate for ten seconds before turning it off.

Code:

while (moisture_val < 850)
{
delay(10000);
}
Why does it matter how many times the while loop iterates? Get rid of the delay() call in there. It is doing nothing.

Do I just need to give it a starting value like this?

No. When you call the one argument setTime() method, you need to specify the number of seconds from Jan. 1, 1970.

It really does not look like you care about exact times. All your times are relative, just like millis(). I think your attempts to use the Time library are misguided.

Quote

Do I just need to give it a starting value like this? No. When you call the one argument setTime() method, you need to specify the number of seconds from Jan. 1, 1970.

It really does not look like you care about exact times. All your times are relative, just like millis(). I think your attempts to use the Time library are misguided.

You are probably right. I was just trying to use the original code written for this project. I will read up on the millis() and try and work with it. Thanks Dan

I think I see why the original author did not want to use the millis() command. He wanted about 14 hours of light in a day so I am thinking he used time instead of millis() as the millis() overflows at around nine hours. I don't have to get 14 hours of light at a time but I can see the reasoning behind it. Is there any way to squeeze more time out of the millis() command?

so I am thinking he used time instead of millis() as the millis() overflows at around nine hours.

Really? An unsigned long, which is what millis() returns can hold a value up to 4,294,967,295. Get out your handy dandy calculator, and see how many seconds that corresponds to. Then, see how many hours and days.

It works out to more than 49 days, not 9 hours.

Got that fact out of the arduino programming notebook by Brian Evans off of the manuals and curriculm page. I will certainly try millis() as it looks like it might be easier to write something with my limited skills. I will keep you posted on how it is going and whine a lot as I fail :) thanks Dan