Hey! Okey I got everything lined up but I still got one problem with the Arduino time thing. I got the 2 alarms from the RTC attached to the code but when the alarm triggers, I don't know how to power the water valve for 5 minutes and then go back to sleep. Maybe my sleep mode isn't the best but that doesn't really matter here, any advices on the 5 minutes problem and maybe on sleep modes? I would like to have one since the system will only be on for about 10-15 min totally a day.
I have some suggestions and thoughts about doing something with a loop but the the power will go like on and off, on and off all the time during the 5 min?
Maybe something like, but how to code it?:
///////////////////////////////////////////
set Pin 13 to high (for example)
while (the time read NOT equls to readRTCtime + 5min)
{
do nothing, just to keep the arduino in work until we set the pin to low next.
}
set Pin 13 to LOW
/////////////////////////////////////////
The code I got this far is:
#include <DS3232RTC.h>
#include <Streaming.h>
#include <Time.h>
#include <Wire.h>
#include <LowPower.h>
#define SQW_PIN 2
#define DS3231_I2C_ADDRESS 0x68
volatile boolean alarmIsrWasCalled = false;
int led_pin = 13;
int rtc_power_pin = 9;
void setup(void)
{
pinMode(led_pin, OUTPUT);
digitalWrite(led_pin, LOW);
pinMode(rtc_power_pin, OUTPUT);
digitalWrite(rtc_power_pin, HIGH);
Wire.begin();
Serial.begin(115200);
setSyncProvider(RTC.get);
Serial << "RTC Sync";
if (timeStatus() != timeSet)
{
Serial << " FAIL!";
}
Serial << endl;
RTC.squareWave(SQWAVE_NONE);
pinMode(SQW_PIN, INPUT_PULLUP);
attachInterrupt(INT0, alarmIsr, FALLING);
//Set an alarm at every 20th second of every minute.
RTC.setAlarm(ALM1_MATCH_HOURS, 0, 49, 6, 1);
RTC.alarm(ALARM_1);
RTC.alarmInterrupt(ALARM_1, true);
//Set an alarm every minute.
RTC.setAlarm(ALM2_MATCH_HOURS, 0, 54, 6, 1);
RTC.alarm(ALARM_2);
RTC.alarmInterrupt(ALARM_2, true);
displayTime(); // display the real-time clock data on the Serial Monitor,
delay(1000); // every second
}
void alarmIsr()
{
alarmIsrWasCalled = true;
}
void loop(void)
{
if (alarmIsrWasCalled) {
if (RTC.alarm(ALARM_1)) {
displayTime(); THIS ONE SHOULD NOT BE HERE; JUST FOR TEST!
digitalWrite(led_pin, HIGH);
delay(5000);
digitalWrite(led_pin, LOW);
delay(3000);
digitalWrite(led_pin, HIGH);
delay(3000);
digitalWrite(led_pin, LOW);
delay(3000);
}
if (RTC.alarm(ALARM_2)) {
//need to write some logic
displayTime();// THIS ONE SHOULD NOT BE HERE; JUST FOR TEST!
while () // MAYBE HERE?
digitalWrite(led_pin, HIGH);
delay(5000);
digitalWrite(led_pin, LOW);
delay(3000);
digitalWrite(led_pin, HIGH);
delay(3000);
digitalWrite(led_pin, LOW);
delay(3000);
}
alarmIsrWasCalled = false;
//digitalWrite(rtc_power_pin, LOW); //if this line is commented it works as expected.
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
}
}
///////////////////////////////////////////////////////////
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return ( (val / 10 * 16) + (val % 10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val / 16 * 10) + (val % 16) );
}
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
void displayTime()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// retrieve data from DS3231
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
&year);
// send it to the serial monitor
Serial.print(hour, DEC);
// convert the byte variable to a decimal number when displayed
Serial.print(":");
if (minute < 10)
{
Serial.print("0");
}
Serial.print(minute, DEC);
Serial.print(":");
if (second < 10)
{
Serial.print("0");
}
Serial.print(second, DEC);
Serial.print(" ");
Serial.print(dayOfMonth, DEC);
Serial.print("/");
Serial.print(month, DEC);
Serial.print("/");
Serial.print(year, DEC);
Serial.print(" Day of week: ");
switch (dayOfWeek) {
case 1:
Serial.println("Sunday");
break;
case 2:
Serial.println("Monday");
break;
case 3:
Serial.println("Tuesday");
break;
case 4:
Serial.println("Wednesday");
break;
case 5:
Serial.println("Thursday");
break;
case 6:
Serial.println("Friday");
break;
case 7:
Serial.println("Saturday");
break;
}
}