TimeAlarms questions...

/*

  • TimeAlarmExample.pde
    */

#include <Time.h>
#include <TimeAlarms.h>

int hidPin = 13;
int pumpPin1 = 10;
int pumpPin2 = 11;

void setup()
{
Serial.begin(9600);
Serial.println("Timer Test");
Serial.println();

setTime(8,29,50,1,1,10); // set time to current time/date - disregard actual time, I know it's wrong.

Alarm.alarmRepeat(8,30,0, LightOn); // Set time to come ON
Alarm.alarmRepeat(8,35,0, LightOff); // Set time to shut OFF
Alarm.timerRepeat(90, PumpOn1); // Timer for every 90s
Alarm.timerRepeat(90, PumpOn2);

pinMode(hidPin, OUTPUT);
pinMode(pumpPin1, OUTPUT);
pinMode(pumpPin2, OUTPUT);

}

void LightOn()
{
digitalWrite(hidPin, HIGH);
Alarm.delay(0);
Serial.println("HID ON");
Alarm.delay(0);
}

void LightOff()
{
digitalWrite(hidPin, LOW);
Alarm.delay(0);
Serial.println("HID OFF");
Alarm.delay(0);
}

void PumpOn1(){
digitalWrite(pumpPin1, HIGH);
Serial.println("Pump 1 ON");
Alarm.delay(2000); // Pump stays on for 2 seconds
digitalWrite(pumpPin1, LOW);
Serial.println("Pump 1 OFF");

}

void PumpOn2(){
digitalWrite(pumpPin2, HIGH);
Serial.println("Pump 2 ON");
Alarm.delay(2000);
digitalWrite(pumpPin2, LOW);
Serial.println("Pump 2 OFF");

}

void loop(){
Alarm.delay(0);
}

Ok, so I've been screwing around with the above code for about an hour now. I got everything to work, and I'm making progress, but for some reason I cannot get pumpPin1 and pumpPin2 to stay HIGH at the same time. hidPin will stay HIGH with one of the pumpPins also HIGH, but pumpPin1 and pumpPin2 will not go HIGH together. I'm using LEDs to test this all out, but later on I will be using relays to trigger a light to stay on for 18hrs, and then using pumpPin1 and pumpPin2 to control water pumps via relays, etc.

This is for a greenhouse project (legal lol) and the pumps are used to water the plants... It's not really an issue that the pins aren't both activated at the same time, but I'd like to know what I need to do to make them come on at the same time. I think I need to use the millis() command or whatever that is... or is there a different way? I will also be putting more code in, so I can't have processes screwing with each other.

I will lay out what I need to control,

1 or possibly 2 lights on different timing schedules, 1 for 18hrs on/6 off the other could be for 20hrs on 4/off, etc

3 separate watering pumps which will need to come on once every minute or two for a few seconds each

Temperature control by using a DHT11 temp/humidity sensor, would like to set varying temps throughout the day, slowly warming when the lights turn on, like, 6am temp 72F (low temp), 9am temp 75F, etc

CO2 injection using a sensor from CO2meter.com, keeping the CO2 levels in a set range by using a solenoid valve on a CO2 tank, using either a photoresistor or timer method to allow solenoid to work during light hours but stay OFF during a lights off period.

I will be adding more features later, as I learn more, but right now I only need to code the above items.

Any help on my timer code/questions, how to improve it, etc, would be greatly appreciated, and then any questions I have about the other items I can address at a later time; baby steps :slight_smile:

Thanks so much guys!

I think the problem is that you are turning the pump pins on and off in the same fun function. Can you not schedule the OFF for two seconds after the ON rather than use a delay()?

How would I go about making the pumpPin(s) stay HIGH for x amount of time then? I just figured out that the problem is the Alarm.delay...

John, you mean set a whole new timer for the off functions?

so like,

Alarm.timerRepeat(10, Pump1ON);
Alarm.timerRepeat(2, Pump1OFF);

void Pump1ON(){
digitalWrite(pumpPin1, HIGH);
}

void Pump1OFF(){
digitalWrite(pumpPin1, LOW);
}

??

FlyingSteve:
John, you mean set a whole new timer for the off functions?

Something like that. Perhaps you can set a 2-second Alarm.timerOnce() at the end of the Pump1ON() function to call the Pump1OFF() function.

Perhaps your original problem is caused by this: "Alarm.delay() must be used instead of the usual Arduino delay() function because the alarms are serviced in the Alarm.delay method. Failing to regularly call Alarm.delay will result in the alarms not being triggered so always use Alarm.delay instead of delay in sketches that use the Alarms library."

You can have up to six alarms pending at once.

John, I see what you mean about using Alarm.timeronce... Nice idea.

I tried that idea with no success... It's weird tho cuz when I type Alarm.timerOnce() none of it shows up in orange... I'm thinking of using this library, TimedAction for the pumps and Alarm.alarmRepeat() for the lights.

Still need to figure out how to adjust the thermostat temp by the time of day... Any advice?