I am trying to improve the coding to allow the 2 alarms to be used for 2 different time periods.I have had some success in getting the relay to stay activated for the desired time period.
I developing this idea I first got it working with Alarm1 on the DS3231 that I am using. This worked fine apart for the time period was shorter than expected. I used 60000 millis for 1 minute but it only spanned 32 secs?
This was not a major problem(I just doubled the figure to get a minute), however I want to use Alarm 2 as well, so I came up with just ORing the Alarm2 in the if statemenent.
When I tried this it completely ignored Alarm2.
Here is the line:
if(DS3231_triggered_a1()== true|| DS3231_triggered_a2()== true){
Where am I going wrong, the statement looks OK to me?
Have I got my logic wrong?
I see it as:
Alarm1 - TRUE OR Alarm2 -FALSE = TRUE
Alarm1 - FALSE OR Alarm2 - TRUE = TRUE
Alarm1 - FALSE OR Alarm2 - FALSE = FALSE
Any help would be appreciated!
Here is the full code:
// rtc_ds3231_alarm7 (Working with sensor)
// during an alarm the INT pin of the RTC is pulled low
//SDA - pin A4
// SCL - pin A5
// this is handy for minimizing power consumption for sensor-like devices,
// since they can be started up by this pin on given time intervals.
#include <Wire.h>
#include "ds3231.h"
#define BUFF_MAX 256
// time when to wake up
uint8_t wake1_HOUR = 17;
uint8_t wake1_MINUTE = 2;
uint8_t wake1_SECOND = 00;
uint8_t wake2_HOUR = 17;
uint8_t wake2_MINUTE = 5;
int relaypin = 10;
int SQWpin = 8;
int tankSensePin = A0;
int curCounter = 0;
int sensorValue = 0;
boolean Alarm1On = false;
boolean Alarm2On = false;
unsigned long StartTime=0;
unsigned long EndTime=0;
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
// constants won't change :
unsigned long prev = 1000, interval = 1000;
void set_alarm(void) {
// flags define what calendar component to be checked against the current time in order
// to trigger the alarm - see datasheet
// A1M1 (seconds) (0 to enable, 1 to disable)
// A1M2 (minutes) (0 to enable, 1 to disable)
// A1M3 (hour) (0 to enable, 1 to disable)
// A1M4 (day) (0 to enable, 1 to disable)
// DY/DT (dayofweek == 1/dayofmonth == 0)
uint8_t flags1[5] = { 0, 0, 0, 1, 1 };
// A2M2 (minutes) (0 to enable, 1 to disable)
// A2M3 (hour) (0 to enable, 1 to disable)
// A2M4 (day) (0 to enable, 1 to disable)
uint8_t flags2[3] = {0, 0, 1};
// set Alarm1
DS3231_set_a1(wake1_SECOND, wake1_MINUTE, wake1_HOUR, 0, flags1);
// activate Alarm1
DS3231_set_creg(DS3231_INTCN | DS3231_A1IE);
// set Alarm2
DS3231_set_a2(wake2_MINUTE, wake2_HOUR, 0, flags2);
// activate Alarm2
DS3231_set_creg(DS3231_INTCN | DS3231_A2IE);
}
void setup() {
Serial.begin(9600);
Wire.begin();
pinMode(SQWpin, INPUT);
pinMode(relaypin, OUTPUT);
pinMode(tankSensePin, INPUT);
DS3231_init(DS3231_INTCN);
DS3231_clear_a1f();
DS3231_clear_a2f();
set_alarm();
}
void loop()
{
char buff[BUFF_MAX];
unsigned long now = millis();
struct ts t;
// once a while show what is going on
if ((now - prev > interval) && (Serial.available() <= 0)) {
DS3231_get(&t);
// display current time
snprintf(buff, BUFF_MAX, "%d.%02d.%02d %02d:%02d:%02d", t.year,
t.mon, t.mday, t.hour, t.min, t.sec);
Serial.println(buff);
int tankSenseReading = analogRead(tankSensePin);
Serial.println(tankSenseReading);
if (tankSenseReading>500){
Serial.println(tankSenseReading);
Serial.println("Tank OK");
if(DS3231_triggered_a1()== true|| DS3231_triggered_a2()== true){
StartTime=millis();
Serial.println(StartTime);
Serial.println(EndTime);
Serial.println("Timing Started");
digitalWrite(relaypin, HIGH);
}
if(now >=EndTime){
EndTime=StartTime+120000;
digitalWrite(relaypin, LOW);
Serial.println("Timing Stopped");
DS3231_clear_a1f();
}
}
prev = now;
}
}