Hi everyone,
I intend to make my code that after meet the certain requirement (after the mosfet go to HIGH at first time) then go back to sleep mode but not sure how to. At the moment I use the SQW wake up pin to wake up the device at certain time. Is there any way that I can break the while loop and straight away put my device back to sleep mode when the first time the MOSFET go to HIGH??
Many thanks.
#include <NewPing.h>
#include <RTClibExtended.h>
#include <Wire.h>
#include <LowPower.h>
#include <SD.h>
#include <stdlib.h>
#include <avr/interrupt.h> // library for interrupts handling
#include <avr/sleep.h> // library for sleep
#include <avr/power.h> // library for power control
#define wakePin 2 //use interrupt 0 (pin 2) and run function wakeUp when pin 2 gets LOW
#define ledPin A0
#define mosfet 9
#define SONAR_NUM 2 // Number of sensors.
#define MAX_DISTANCE 70 // Maximum distance (in cm) to ping.
#define power 8
const int trigPin1 = 4;
const int echoPin1 = 5;
long duration1;
int distance1;
const int trigPin2 = 6;
const int echoPin2 = 7;
long duration2;
int distance2;
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
int interval_sec=10; //An alarm every 10 sec
int i;
void setup () {
Serial.begin(9600);
delay(3000); // wait for console opening
/**RTC**/
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
/**INTERRUPT**/
//Set pin D2 as INPUT for accepting the interrupt signal from DS3231
pinMode(wakePin, INPUT);
//set mosfet pin to output
pinMode(power, OUTPUT);
pinMode(mosfet, OUTPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(mosfet, LOW);
//ultrasonic
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
Wire.begin();
//clear any pending alarms
rtc.armAlarm(1, false);
rtc.clearAlarm(1);
rtc.alarmInterrupt(1, false);
rtc.armAlarm(2, false);
rtc.clearAlarm(2);
rtc.alarmInterrupt(2, false);
//Set SQW pin to OFF
//The output of the DS3231 INT pin is connected to this pin
//connected to arduino D2 pin for wake-up
rtc.writeSqwPinMode(DS3231_OFF);
Serial.println("Initialisation complete.");
delay(100); //Allow for serial print to complete.
}
void loop () {
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
/**INTURRUPT**/
DateTime nextAlarm = now + TimeSpan(0, 0, 0, interval_sec);
Serial.print("Alarm at:");
Serial.println(nextAlarm.hour());
Serial.println(nextAlarm.minute());
Serial.println(nextAlarm.second());
rtc.setAlarm(ALM1_MATCH_HOURS, nextAlarm.second(), nextAlarm.minute(), nextAlarm.hour(), 1); //set your wake-up time here
rtc.alarmInterrupt(1, true);
attachInterrupt(0, wakeUp, LOW); //use interrupt 0 (pin 2) and run function wakeUp when pin 2 gets LOW
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); //arduino enters sleep mode here
detachInterrupt(0); //execution resumes from here after wake-up
//When exiting the sleep mode we clear the alarm
rtc.armAlarm(1, false);
rtc.clearAlarm(1);
rtc.alarmInterrupt(1, false);
while ( now.minute()>0 && now.minute() < 55)
{
distance1 = range(trigPin1, echoPin1, power);
distance2 = range(trigPin2, echoPin2, power);
Serial.print("Distance 1:");
Serial.print( distance1);
Serial.print(" Distance 2:");
Serial.println( distance2);
if(distance1<10 && distance2<10)
{
digitalWrite(mosfet, HIGH);
delay(50);
}
else
{
digitalWrite(mosfet, LOW);
delay(50);
}
digitalWrite(ledPin, HIGH);
delay(50);
digitalWrite(ledPin, LOW);
delay(50);
now = rtc.now();//check time again
}
}
unsigned long range(byte trigPin, byte echoPin, byte vcc)
{
digitalWrite(vcc, LOW);
delay(50);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
unsigned long duration = pulseIn(echoPin, HIGH);
digitalWrite(vcc, HIGH);
delay(100);
return duration * 0.034 / 2;
}
void wakeUp() // here the interrupt is handled after wakeup
{
}