Home › Forums › Mayfly Data Logger › Sleep code not working with Maxbotix 7389 › Reply To: Sleep code not working with Maxbotix 7389
2019-12-09 at 3:12 PM
#13453
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 |
#include <SoftwareSerial_PCINT12.h> #include <Sodaq_PcInt_PCINT0.h> //sketch to log sonar data on Mayfly //this one uses either board power or a battery pack to continuously power Maxbotix #include <Wire.h> #include <avr/sleep.h> #include <avr/wdt.h> #include <SPI.h> #include <SdFat.h> SdFat SD; #include <RTCTimer.h> #include <Sodaq_DS3231.h> // const int SonarExcite = 11; // turns on switched power SoftwareSerial sonarSerial(19, -1); boolean stringComplete = false; #define READ_DELAY 1 // RTC Timer RTCTimer timer; String dataRec = ""; int currentminute; long currentepochtime = 0; float boardtemp = 0.0; int batteryPin = A6; // select the input pin for the potentiometer int batterysenseValue = 0; // variable to store the value coming from the sensor float batteryvoltage; int range_mm1; int range_mm2; int range_mm3; int range_mm4; int range_mm5; int range_mm6; int range_mm7; int range_mm8; int range_mm9; int range_mm10; int range_mm; // RTC Interrupt pin #define RTC_PIN A7 #define RTC_INT_PERIOD EveryMinute #define SD_SS_PIN 12 // The data log file #define FILE_NAME "SonicLog.txt" // Data header #define LOGGERNAME "Ultrasonic Maxbotix Sensor Datalogger" #define DATA_HEADER "DateTime,Loggertime,BoardTemp,Battery_V,SonarRange_mm 1 to 10" void setup() { // Initialise the serial connection // Serial.begin(57600); sonarSerial.begin(9600); rtc.begin(); delay(100); pinMode(8, OUTPUT); pinMode(9, OUTPUT); pinMode(18, OUTPUT); greenred4flash(); //blink the LEDs to show the board is on setupLogFile(); // Setup timer events setupTimer(); // Setup sleep mode setupSleep(); // Serial.println("Power On, running: ultrasonic_logger_example_1.ino"); // Serial.println("Power On, running: ultrasonic_logger_example_1.ino"); digitalWrite(8, HIGH); stringComplete = false; digitalWrite(18, HIGH); delay(500);//wait for board to stabilize range_mm = SonarRead();//does not add data to a string, included only for debugging digitalWrite(18, LOW); digitalWrite(8,LOW); delay (2000); } void loop() { //Update the timer timer.update(); if(currentminute % 5 == 0) { digitalWrite(8, HIGH); dataRec = createDataRecord(); stringComplete = false; delay(100); digitalWrite(18, HIGH);//turns on switched power delay(10000);//wait 15 sec for board to stabilize after startup range_mm1 = SonarRead(); stringComplete = false; delay (1000); range_mm2 = SonarRead(); stringComplete = false; delay (1000); range_mm3 = SonarRead(); stringComplete = false; delay (1000); range_mm4 = SonarRead(); stringComplete = false; delay (1000); range_mm5 = SonarRead(); stringComplete = false; delay (1000); range_mm6 = SonarRead(); stringComplete = false; delay (1000); range_mm7 = SonarRead(); stringComplete = false; delay (1000); range_mm8 = SonarRead(); stringComplete = false; delay (1000); range_mm9 = SonarRead(); stringComplete = false; delay (1000); range_mm10 = SonarRead(); stringComplete = false; delay (1000); digitalWrite(8, LOW); //following records ten data reads. dataRec += ","; dataRec += range_mm1; dataRec += ","; dataRec += range_mm2; dataRec += ","; dataRec += range_mm3; dataRec += ","; dataRec += range_mm4; dataRec += ","; dataRec += range_mm5; dataRec += ","; dataRec += range_mm6; dataRec += ","; dataRec += range_mm7; dataRec += ","; dataRec += range_mm8; dataRec += ","; dataRec += range_mm9; dataRec += ","; dataRec += range_mm10; //Save the data record to the log file logData(dataRec); //Echo the data to the serial connection //Serial.println(); //Serial.print("Data Record: "); //Serial.println(dataRec); String dataRec = ""; } systemSleep(); } void showTime(uint32_t ts) { //Retrieve and display the current date/time String dateTime = getDateTime(); //Serial.println(dateTime); } void setupTimer() { //Schedule the wakeup every minute timer.every(READ_DELAY, showTime); //Instruct the RTCTimer how to get the current time reading timer.setNowCallback(getNow); } void wakeISR() { //Leave this blank } void setupSleep() { pinMode(RTC_PIN, INPUT_PULLUP); PcInt::attachInterrupt(RTC_PIN, wakeISR); //Setup the RTC in interrupt mode rtc.enableInterrupts(RTC_INT_PERIOD); //Set the sleep mode set_sleep_mode(SLEEP_MODE_PWR_DOWN); } void systemSleep() { //Wait until the serial ports have finished transmitting Serial.flush(); Serial1.flush(); //The next timed interrupt will not be sent until this is cleared rtc.clearINTStatus(); //Disable ADC ADCSRA &= ~_BV(ADEN); //Sleep time noInterrupts(); sleep_enable(); interrupts(); sleep_cpu(); sleep_disable(); //Enbale ADC ADCSRA |= _BV(ADEN); } String getDateTime() { String dateTimeStr; //Create a DateTime object from the current time DateTime dt(rtc.makeDateTime(rtc.now().getEpoch())); currentepochtime = (dt.get()); //Unix time in seconds currentminute = (dt.minute()); //Convert it to a String dt.addToString(dateTimeStr); return dateTimeStr; } uint32_t getNow() { currentepochtime = rtc.now().getEpoch(); return currentepochtime; } void greenred4flash() { for (int i=1; i <= 4; i++){ digitalWrite(8, HIGH); digitalWrite(9, LOW); delay(50); digitalWrite(8, LOW); digitalWrite(9, HIGH); delay(50); } digitalWrite(9, LOW); } void setupLogFile() { // initialize the SD card at SPI_FULL_SPEED for best performance. // try SPI_HALF_SPEED if bus errors occur. if (!SD.begin(SD_SS_PIN, SPI_HALF_SPEED)) { SD.initErrorHalt(); } //Check if the file already exists bool oldFile = SD.exists(FILE_NAME); //Open the file in write mode File logFile = SD.open(FILE_NAME, FILE_WRITE); //Add header information if the file did not already exist if (!oldFile) { logFile.println(LOGGERNAME); logFile.println(DATA_HEADER); } //Close the file to save it logFile.close(); } void logData(String rec) { //Re-open the file File logFile = SD.open(FILE_NAME, FILE_WRITE); //Write the CSV data logFile.println(rec); //Close the file to save it logFile.close(); } String createDataRecord() { //Create a String type data record in csv format //TimeDate, Loggertime,Temp_DS, Diff1, Diff2, boardtemp String data = getDateTime(); data += ","; rtc.convertTemperature(); //convert current temperature into registers boardtemp = rtc.getTemperature(); //Read temperature sensor value batterysenseValue = analogRead(batteryPin); batteryvoltage = (3.3/1023.) * (12.7/2.7) * batterysenseValue; data += currentepochtime; data += ","; addFloatToString(data, boardtemp, 3, 1); //float data += ","; addFloatToString(data, batteryvoltage, 4, 2); return data; } static void addFloatToString(String & str, float val, char width, unsigned char precision) { char buffer[10]; dtostrf(val, width, precision, buffer); str += buffer; } int SonarRead() { int result; char inData[5]; //char array to read data into int index = 0; while (sonarSerial.read() != -1) {} while (stringComplete == false) { if (sonarSerial.available()) { char rByte = sonarSerial.read(); //read serial input for "R" to mark start of data if(rByte == 'R') { //Serial.println("rByte set"); while (index < 4) //read next three character for range from sensor { if (sonarSerial.available()) { delay(50); inData[index] = sonarSerial.read(); //Serial.println(inData[index]); //Debug line index++; // Increment where to write next } } inData[index] = 0x00; //add a padding byte at end for atoi() function } rByte = 0; //reset the rByte ready for next reading index = 0; // Reset index ready for next reading stringComplete = true; // Set completion of read to true result = atoi(inData); // Changes string data into an integer for use } } return result; } |