how can I make my arduino mega sleep during 6pm-7am..and wakes or stay up during 7am-6pm and doing its job like activating the solenoid valve based on my soil moisture sensor reading.this is for my backyard farm.anyone can help me making a code for it? please i really need it.
here is my code
#include <DS3231.h>
#include <Wire.h>
#include <avr/sleep.h>
int wakePin = 2; // pin used for waking up
int led = 13;
DS3231 clock;
RTCDAteTime dt;
void setup ()
{
Serial.begin(9600);
// initialize DS3231
Serial.println("Initailize DS3231");
delay(100);
clock.begin();
pinMode(wakePin,INPUT_PULLUP);
pinMode(led,OUTPUT);
//Disarm alarms and clear alarms for this example, beacuse alarms is battery backed.
//Under normal conditions, the settings should be reset after power and restart microcontroller
clock.armAlarm(false);
clock.clearAlarm();
//Set Alarm1 - Every 20s in each minute
// setAlarm1(date or day,hour,minute,second,Mode,Arrmed=True)
clock.setAlarm1(0,0,0,20,DS3231_MATCH_S);
attachInterrupt(0,wakeUpNow,LOW);
//check alarm settings
checkAlarms();
}
void checkAlarms()
{
RTCAlarmTimea1;
RTCAlarmTimea2;
if (clock.isArmed1())
{
a1 = clock.getAlarm1();
Serial.print("Alarm1 is triggered:);
switch(clock.getAlarmType1())
{
case DS3231_MATCH_S:
Serial.print("When seconds match: ');
Serial.println(clock.dateFormat("_ _:_:s",a1));
delay(100);
break;
default:
Serial.println("UNKNOWN RULE");
break;
}
}
}
void wakeUpNow()
{
//execute code here after wake-up before returning to the loop() function
//timers and code using timers(serial.print and more...)will not work here.
//we don`t really need to execute any special functions here, since we
//just want the thing to wake up
Serial.print("Woke up");
delay(100);
}
void sleepNow()
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN); //sleep mode is set here
sleep_enable(); //enables the sleep bit in the mcucr register
attachInterrupt(0,wakeUpNow,FALLING); //use interrupt 0(pin2) and run function
sleep_mode();
// THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
sleep_disable(); //first thing after waking from sleep: disable sleep
detachInterrupt(0); //disables interrupt 0 on pin 2 so the wakeUpNow code will not be executed during normal running
time.
}
void loop()
{
dt=clock.getDateTime();
Serial.println(clock.dateFormat("d-m-Y H:i:s-I",dt));
delay(100);
//Call isAlarm1(false)if you want clear alarm1 flag manualy by cleaAlarm1();
if(clock.isAlarm1())
{
Serial.println("ALARM 1 TRIGGERED!");
digitalWrite(led,HIGH);
delay(1000);
digitalWrite(led,LOW);
Serial.println("Entering Sleep");
delay(100); //sleep fucntion called here
sleepNow();
}
delay(900);
}
-
It depends what you mean by sleep here. There are methods for putting the chip into an inactive sleep state for extreme power savings in battery powered applications. I guess here you simply mean that during a certain period of the day, as determined from the real time clock, you want to suppress the sensor monitoring and the solenoid valve activity. Is that correct or is your goal to minimise power consumption in a battery powered application ?6v6gt– 6v6gt2017年02月24日 08:03:38 +00:00Commented Feb 24, 2017 at 8:03
-
both..is it possible??renzo– renzo2017年02月24日 08:17:49 +00:00Commented Feb 24, 2017 at 8:17
-
1Yes it's possible. For extreme power saving techniques with AVR (which Arduino Mega) look at gammon.com.au/power . However, if your application includes a solenoid valve then shaving a few microamps from the power consumption through cpu sleep/wakeup cycles may not make a big difference to the overall power consumption. I'd start by reading the RTC in the loop, determining if is day or night, reading the sensor and switching the valves as necessary, then as a later activity, optimising the power consumption.6v6gt– 6v6gt2017年02月24日 09:14:19 +00:00Commented Feb 24, 2017 at 9:14
-
i have separate power supply for my solenoid valverenzo– renzo2017年02月24日 09:19:38 +00:00Commented Feb 24, 2017 at 9:19
-
can you help me fixing a code for it? I will send you my sketch and you can change it and send it back. please help me.renzo– renzo2017年02月24日 09:24:40 +00:00Commented Feb 24, 2017 at 9:24
1 Answer 1
First, Arduino Mega does not have an RTC (Real Time Clock) built-in. So, to program it the way you like, would mean either using a module like DS3231 or similar, or have the Mega connected to internet, with a network card module like ENC28J60 and getting the time from there. If you don't use any of those methods basing your scheduler only on the Arduino internal timer (using sleep for example) will result in program needing to set the correct time each time Arduino is powered.
-
i have RTC ds3231 and Arduino mega 2560, solenoid valve, soil mosure sensorrenzo– renzo2017年02月24日 08:18:12 +00:00Commented Feb 24, 2017 at 8:18
Explore related questions
See similar questions with these tags.