Hi everyone
I am a noobs that is building a device that has to be mostly in sleep mode, except for when it has to activate a motor in one direction, sleep again, activate the motor in the other direction, and sleep again until the cycle repeats.
I was lucky to find a tutorial on how sleeping and waking up with the DS3231, and I get a power consumption in my system of 5.5mA in sleep mode, which is OK with my needs at a protoype level.
I have a large code with a menu, but I have extracted where the problem is, and tried it with 2 LEDs. I want the program to turn one LED, go to sleep for 10 seconds, wake up with the alarm, g turn on LED2, go to sleep for 10 seconds, and start all over.
//20180914_IYV: this code for asking in forum in order to shed some light on the alarm problem
#include <RTClibExtended.h>
#include <LowPower.h>
RTC_DS3231 rtc;
const byte LED1 = 8;
const byte LED2 = 9;
byte t_1 = 10;
byte t_2 = 5;
volatile boolean light_1 = true;
volatile boolean light_2 = false;
void setup() {
pinMode (LED1, OUTPUT); // so we can update the LED
pinMode (LED2, OUTPUT); // so we can update the LED
rtc.writeSqwPinMode(DS3231_OFF);
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
if (rtc.lostPower()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
if (light_1 == true){
digitalWrite(LED1, HIGH);
delay(1000);
digitalWrite(LED1, LOW);
light_1 = false;
byte seconds = rtc.now().second() + t_1;
if (seconds > 60){
seconds = seconds - 60;
}
rtc.setAlarm(ALM1_MATCH_SECONDS, seconds, 0 , 0, 0);
attachInterrupt(0, lights, LOW);
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
light_2 = true;
detachInterrupt(0);
}
if (light_2 == true){
digitalWrite(LED2, HIGH);
delay(1000);
digitalWrite(LED2, LOW);
delay(50);
light_2 = false;
byte seconds = rtc.now().second() + t_2;
if (seconds > 60){
seconds = seconds - 60;
}
rtc.setAlarm(ALM1_MATCH_SECONDS, seconds , 0, 0, 0);
attachInterrupt(0, lights, LOW);
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
detachInterrupt(0);
light_1 = true;
}
}
void lights() // here the interrupt is handled after wakeup
{
}
Well, for this simplification of my code, sometimes I do not even get the alarm armed (the INT pin of the alarm does not go HIGH when the arduino goes to sleep)! The DS3231 is simply not giving me a reliable LOW at the set time thorugh the INT pin...
What on earth am I doing wrong? I am going nuts about this.
-I have an Arduino Nano powered through usb
-I have a DS3231 with a battery, but also connected to the Nano 3.3V
-I2C scanner detects the DS3231
-DS3231 works fine with JChristensen library
-I have a 10k pull up resistor on Arduin D2/DS3231 INT
-I intend to power the device with a 4P2S Liion battery at 14.8V through Vin
EDIT: I changed the DS3231 to a new one, and I got the alternating alarm to work. Unfortunately, the it stops at a certain random point in time, until I manually trigger the LOW again. But I need an alarm that is running forever :-(. And besides, in the periods when it is working fine, if I measure the DS3231 battery voltage, or the voltage between nano D2 and GND, or if I just touch the DS3231 battery, the interrupt gets triggered, and the arduino awaken out of the sleep state.