Hey there,
I'm trying to controll multiple LEDs with an arduino by using a light dependent resistor (LDR).
The general setup contains (obviously) an arduino pro mini and a Ds3231 RTC.
Why the Real Time Clock?
The Arduino should consume as few power as possible, that's why I chose the pro mini version of the arduino.
When it is dark enough the LDR becomes more resistant. In that case the Arduino emittes a current to activate a MOSFET which enlights the LEDs.
I'd like to power that setup with batteries, so I would let the arduino fall asleep during daylight.
My problem is to write the code properly to reach that goal.
If you have better ideas to save as much power as possible I would really appreciate that.
Thanks for your help, below the code I wrote (it is not complete cause I don't know how)
/* Libraries to download
- DS3231 RTC Library (https://github.com/JChristensen/DS3232RTC/archive/master.zip)
*/
#include <avr/sleep.h> //Including the sleep library
#include <DS3232RTC.h> //Including the RTC Library
#define interruptPin 2 //Pin to wake the Arduino up
int SensorSpannung = A4;
int sensorWert = 0;
int TransistorPin = 6;
void setup() {
Serial.begin(9600);
pinMode(interruptPin, INPUT_PULLUP); //set pin d2 to wakeup-input using the buildin pullup resistor
// initialize the alarms to known values, clear the alarm flags, clear the alarm interrupt flags
RTC.setAlarm(ALM1_MATCH_DATE, 0, 0, 0, 1);
RTC.setAlarm(ALM2_MATCH_DATE, 0, 0, 0, 1);
RTC.alarm(ALARM_1);
RTC.alarm(ALARM_2);
RTC.alarmInterrupt(ALARM_1, false);
RTC.alarmInterrupt(ALARM_2, false);
RTC.squareWave(SQWAVE_NONE);
tmElements_t tm;
tm.Hour = 00; // set the RTC to an arbitrary time
tm.Minute = 00;
tm.Second = 00;
tm.Day = 4;
tm.Month = 2;
tm.Year = 2019 - 1970; // tmElements_t.Year is the offset from 1970
RTC.write(tm); // set the RTC from the tm structure
RTC.setAlarm(ALM1_MATCH_HOURS , 0, 19, 16); //setting the wakeup-time
RTC.alarm(ALARM_1);
// configure the INT/SQW pin for "interrupt" operation (disable square wave output)
RTC.squareWave(SQWAVE_NONE);
// enable interrupt output for Alarm 1
RTC.alarmInterrupt(ALARM_1, true);
}
void loop() {
time_t t;// creates temp time variable
t=RTC.get(); //gets current time from rtc
if (t(00, 04, 08)){ //set arduino to sleep mode if it is 8:04 am
Going_to_sleep;
}
}
sensorWert = analogRead(SensorSpannung);
if(sensorWert < 200){
digitalWrite(TransistorPin, HIGH);
}
else {
digitalWrite(TransistorPin, LOW);
delay(120000);
}
}
void Going_to_Sleep (){
sleep_enable();//Enabling sleep mode
attachInterrupt(0, wakeUp, LOW);//attaching a interrupt to pin d2
set_sleep_mode(SLEEP_MODE_PWR_DOWN);//Setting the sleep mode, in our case full sleep
sleep_cpu();//activating sleep mode
RTC.setAlarm(ALM1_MATCH_HOURS , 0, 19, 16); //setting the wakeup-time
RTC.alarm(ALARM_1);
}
void wakeUp(){
sleep_disable();//Disable sleep mode
detachInterrupt(0); //Removes the interrupt from pin 2;
}