#include #include #include #include #define LOGFILE "LOGFILE.TXT" // ADC #define ADC_LEVELS 1024 #define ADC_REF_VOLTAGE 3.3 #define ADC_SCALE_FACTOR 2 // push button (ms) #define DEBOUNCE_TIME 200 // Temp & Humidity sensor #define DHTTYPE DHT11 #define DHTPIN A0 // timer intervals #define TIMER_MINUTE 1 #define TIMER_HOUR 60 // interrupt sources #define ISR_NUM_RTC 0 #define ISR_NUM_PIR 1 #define ISR_NUM_BTN 2 // create objects SdFat sd; File myFile; DHT dht(DHTPIN, DHTTYPE); Rtc_Pcf8563 rtc; // create variables in order to pass MCU pin numbers by reference int pinSD = 10; // digital pin connected to the chip select line for the SD card int pinLED = 4; // digital pin connected to the indicator LED int pinBatt = A6; // analogue pin connected to Vbatt // create variables in order to pass interrupt numbers by reference int intptNumRTC = ISR_NUM_RTC; int intptNumPIR = ISR_NUM_PIR; int intptNumBTN = ISR_NUM_BTN; // create ISR flags volatile int flagRTC = 0; volatile int flagPIR = 0; volatile int flagBTN = 0; void setup() { // initialise error indicator LED pinMode(pinLED, OUTPUT); digitalWrite(pinLED, LOW); cmd.begin(57600); Serial.println("Lab 4-0 - Tying it all together"); // Add some SD card related command line commands for testing purposes only cmd.add("fread", cmdFileRead); cmd.add("ls", cmdFileList); cmd.add("rm", cmdFileDelete); // initialise the SD card and DHT sensor objects sd.begin(pinSD); myFile.dateTimeCallback(sdDateTime); //associate the call back routine for setting file date/time dht.begin(); rtc.setTimer(TIMER_MINUTE, TMR_1MIN, false); // set timer frequency // attach the ISRs to their interrupt sources and define whether active on RISING/FALLING edge attachInterrupt(intptNumRTC, isrRtc, FALLING); attachInterrupt(intptNumPIR, isrPir, RISING); attachInterrupt(intptNumBTN, isrBtn, FALLING); pinMode(pinBatt, INPUT); // set the analogue pin mode for reading the battery voltage } void loop() { cmd.poll(); if (flagRTC == 1) { flagRTC = 0; rtc.resetTimer(); logData(ISR_NUM_RTC); Serial.println(" after Timer interrupt received."); } if (flagPIR == 1) { flagPIR = 0; logData(ISR_NUM_PIR); Serial.println(" after Motion interrupt received."); } if (flagBTN == 1) { delay(DEBOUNCE_TIME); flagBTN = 0; logData(ISR_NUM_BTN); Serial.println(" after Button interrupt received."); } } void logData(int type) { myFile = sd.open(LOGFILE, O_WRITE | O_CREAT | O_APPEND); myFile.print(rtc.formatDate(RTCC_DATE_ASIA)); myFile.print(","); myFile.print(rtc.formatTime()); myFile.print(","); myFile.print(dht.readTemperature()); myFile.print(","); myFile.print(dht.readHumidity()); myFile.print(","); myFile.print(getBatteryVoltage()); if (type == ISR_NUM_RTC) { myFile.println(",RTC"); } else if (type == ISR_NUM_PIR) { myFile.println(",PIR"); } else if (type == ISR_NUM_BTN) { myFile.println(",BTN"); } myFile.close(); Serial.println("Data logged to SD Card."); } float getBatteryVoltage() { int battAdc = analogRead(pinBatt); float VoltsPerUnit = ADC_REF_VOLTAGE / ADC_LEVELS; float battVoltage = battAdc * VoltsPerUnit * ADC_SCALE_FACTOR; return battVoltage; } // ISRs void isrRtc() { flagRTC = 1; } void isrPir() { flagPIR = 1; } void isrBtn() { flagBTN = 1; } // Command functions void cmdFileRead(int argCnt, char **args) { myFile = sd.open(args[1], O_READ); if (myFile == true) { while (myFile.available() > 0) { char c = myFile.read(); Serial.print(c); // Serial.print(myFile.read()); } myFile.close(); } } void cmdFileList(int argCnt, char **args) { sd.ls(LS_DATE | LS_SIZE); } void cmdFileDelete(int argCnt, char **args) { sd.remove(args[1]); Serial.print(args[1]); Serial.println(" deleted."); } // SD callback to add Date & Time to FAT files stored to the SD card void sdDateTime(unsigned int *date, unsigned int *time) { rtc.getDateTime(); // return date using FAT_DATE macro to format fields *date = FAT_DATE(2000+rtc.getYear(), rtc.getMonth(), rtc.getDay()); // return time using FAT_TIME macro to format fields *time = FAT_TIME(rtc.getHour(), rtc.getMinute(), rtc.getSecond()); }