Hello,
I'm working on a project and after few hours my arduino nano is still ON but he seem stuck. When i turn off and turn on everything become ok for few hours.
My project is a chicken door with :
=> 1 arduino nano
=> 2 relay
=> 2 contactor
=> 1 rtc
I think it's a code problem ( out of memory or somthing ).
My code :
#include <LowPower.h>
#include <DS3232RTC.h> // https://github.com/JChristensen/DS3232RTC
#include <Streaming.h> // http://arduiniana.org/libraries/streaming/
#include <Wire.h>
const int wakeupPin = 2 ;
const bool PRINTENV = false;
volatile int woken = 0 ;
void
wakeUp()
{
woken = 1 ;
}
void
setup()
{
pinMode(wakeupPin, INPUT_PULLUP) ;
pinMode(LED_BUILTIN, OUTPUT) ;
//relay
pinMode(6,OUTPUT); //On indique à l’Arduino le mode du pin (entrée)
pinMode(7,OUTPUT); //On indique à l’Arduino le mode du pin (entrée)
//Contactor
pinMode(8,OUTPUT); //On indique à l’Arduino le mode du pin (entrée)
pinMode(9,OUTPUT); //On indique à l’Arduino le mode du pin (entrée)
Serial.begin(115200) ;
delay(50) ;
Serial.println(F("WAKING UP...")) ;
Wire.setClock(400000) ;
setSyncProvider(RTC.get); // the function to get the time from the RTC
if(timeStatus() != timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
Serial.println() ;
delay(50) ;
// initialize the alarms to known values,
// clear the alarm flags, clear the alarm interrupt flags
// ALARM_1 will trigger at 30 seconds into each minute
// ALARM_2 will trigger every minute, at 0 seconds.
RTC.setAlarm(ALM1_MATCH_SECONDS, 30, 0, 0, 0);
RTC.setAlarm(ALM2_EVERY_MINUTE, 0, 0, 0, 0);
// clear both alarm flags
RTC.alarm(ALARM_1); RTC.alarm(ALARM_2);
// We are going to output the alarm by going low onto the
// SQW output, which should be wired to wakeupPin
RTC.squareWave(SQWAVE_NONE);
// Both alarms should generate an interrupt
RTC.alarmInterrupt(ALARM_1, true);
RTC.alarmInterrupt(ALARM_2, true);
}
void
loop()
{
boolean isOpen=digitalRead(8);// et on l’affecte à la variable "a"
boolean isClosed=digitalRead(9);// et on l’affecte à la variable "a"
time_t t = RTC.get();
if(hour(t) > 8 && hour(t) < 21 && isOpen == 0){
//so we are on a day and it's closed so we open the gate
printenv( isClosed, isOpen, "Open the gate", t);
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
}else if(hour(t) >= 21 && isClosed == 0){
//so we are on a night and it's open so we open the gate
printenv( isClosed, isOpen, "Close the gate", t);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
}else{
digitalWrite(6, LOW);
digitalWrite(7, LOW);
// The INT/SQW pin from the DS3231 is wired to the wakeup pin, and
// will go low when the alarm is triggered. We are going to trigger
// on the falling edge of that pulse.
attachInterrupt(digitalPinToInterrupt(wakeupPin), wakeUp, FALLING) ;
// Go into powerdown mode, waiting to be woken up by INT pin...
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF) ;
// For now, let's just ignore transitions on this pin...
detachInterrupt(digitalPinToInterrupt(wakeupPin)) ;
// We have to clear the alarm condition to make the pin go back to
// the high state. I'm clearing both of them, because we are
// triggering every 30 seconds (in this example)
RTC.alarm(ALARM_1) ; RTC.alarm(ALARM_2) ;
printenv( isClosed, isOpen, "Wait now", t);
}
delay(500) ;
}
void
printenv(bool isClosed,bool isOpen, const String& action, time_t t)
{
if(PRINTENV){
Serial.print(action);
Serial.print(" => ");
Serial.print("isClosed : ");
Serial.print(isClosed);
Serial.print(" - isOpen :");
Serial.print(isOpen);
Serial.print(" - Time:");
Serial.print(hour(t));
Serial.print(" : ");
Serial.print(minute(t));
Serial.println("");
}
}
Thanks !