|
大家好
目前使用UART 與sensor arduino 通訊 我想要使用watchdog 方式讓arduino 進入睡眠模式
但因為使用到#include <SoftwareSerial.h>而導致arduino 無法進入睡眠 想請問要如何更改
才能讓arduion 進入睡眠15分鐘並且醒來
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include "Oxygen.h"
#include <avr/sleep.h>
volatile int sleep_count = 0; // Keep track of how many sleep
const int sleep_total = 103; // ~ 15 min
#include <SoftwareSerial.h>
SoftwareSerial mySerial(8, 9); // RX, TX pins on Ardunio
#define SampleNum 20
int co2 =0;
double multiplier = 10;// 1 for 2% =20000 PPM, 10 for 20% = 200,000 PPM
uint8_t buffer[25];
uint8_t ind =0;
uint8_t index =0;
int fill_buffer(); // function prototypes here
String format_output();
RTC_DS3231 rtc;
const int chipSelect = 4;
int checkstate = 0;
//analogReference(INTERNAL2v56);
void setup(void) {
Serial.begin(9600);
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
deviceINIT();
watchdogOn(); // Turn on the watch dog timer.
mySerial.begin(9600); // Start serial communications with sensor
mySerial.println("M 6"); // send Mode for Z and z outputs
mySerial.println("K 1"); // set streaming mode
}
void loop(void) {
if (sleep_count % sleep_total == 0) {
// CODE TO BE EXECUTED PERIODICALLY
Serial.println("wake!");
String dataString = "";
// read RTC
DateTime now = rtc.now();
dataString = String(now.year()) + "/" + String(now.month()) + "/" + String(now.day()) + " " + String(now.hour()) + ":" + String(now.minute()) + ":" + String(now.second()) + " ";
// read O2
float O2 = 0;
O2 = O2_value();
dataString += "O2 ";
dataString += String(O2);
dataString += " ";
delay(500);
// read CO2
fill_buffer();
index = 8; // In ASCII buffer, filtered value is offset from raw by 8 bytes
String CO2=format_output();
dataString += "CO2 ";
dataString += String(CO2);
dataString += " ";
delay(500);
// write SD
File dataFile = SD.open("Dlog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString);
}
else {
Serial.println("error opening Dlog.txt");
}
Serial.println("go to sleep");
// delay to print out serial
delay(2900);
// reset count
sleep_count = 0;
}
goToSleep();
}
void goToSleep()
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Set sleep mode.
sleep_enable(); // Enable sleep mode.
sleep_mode(); // Enter sleep mode.
sleep_disable(); // Disable sleep mode after waking.
}
void watchdogOn() {
MCUSR = MCUSR & B11110111;
WDTCSR = WDTCSR | B00011000;
WDTCSR = B00100001;
WDTCSR = WDTCSR | B01000000;
MCUSR = MCUSR & B11110111;
}
ISR(WDT_vect)
{
sleep_count ++; // keep track of how many sleep cycles
}
void deviceINIT(){
// SD module init
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
return;
}
Serial.println("card initialized.");
//RTC init
delay(3000); // wait for console opening
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
O2_value();
}
void StartTimeCorrect(){
DateTime now = rtc.now();
int c_time = (60-now.minute())%15;
complement_delay(c_time);
Serial.println("end wait");
delay(100);
}
void delay1min(){
delay(30000);
delay(30000);
}
void complement_delay(int c){
for (int i=0;i<c;i++){
delay1min();
}
}
int fill_buffer(void){
// Fill buffer with sensor ascii data
ind = 0;
while(buffer[ind-1] != 0x0A){ // Read sensor and fill buffer up to 0XA = CR
if(mySerial.available()){
buffer[ind] = mySerial.read();
ind++;
}
}
// buffer() now filled with sensor ascii data
// ind contains the number of characters loaded into buffer up to 0xA = CR
ind = ind -2; // decrement buffer to exactly match last numerical character
}
String format_output(){ // read buffer, extract 6 ASCII chars, convert to PPM and print
int co2 =0;
int index=8; double multiplier = 10;
co2 = buffer[15-index]-0x30;
co2 = co2+((buffer[14-index]-0x30)*10);
co2 +=(buffer[13-index]-0x30)*100;
co2 +=(buffer[12-index]-0x30)*1000;
co2 +=(buffer[11-index]-0x30)*10000;
return String(co2*multiplier/10000,4);
} |
|