/* For a VL53L0X laser rangefinder on an Arduino Pro Mini (or other) controlled by an external timer (TPL5110). The timer turns on the Arduino which reads the sensor, saves data to microSD card, and sends a signal to the timer to cut power to the Arduino. The logging interval is determined by the timer. A real-time clock provides timestamps which are saved with the data. September 2018, C Fastie */ #include #include "Adafruit_VL53L0X.h" // https://github.com/adafruit/Adafruit_VL53L0X #include // https://github.com/greiman/SdFat/ #include #include // https://github.com/adafruit/RTClib/ RTC_DS3231 rtc; // rtc will be the RTC_DS3231 object SdFat SD; // SD will be the SdFat object #define donePin A3 // This pin signals to the TPL51100 to cut power Adafruit_VL53L0X lox = Adafruit_VL53L0X(); // lox will be the VL53LOX object char TmeStrng[] = "0000/00/00,00:00:00"; // string template for RTC time stamp long utc; int loxCnt = 50; // __Enter__ number of rangefinder readings to average float loxSum = 0; float loxMean = 0; #define RESOLUTION 4096 // the highest resolution of the VL53LOX rangefinder void setup() { Serial.begin(9600); delay(100); Wire.begin(); // initialize the I2C interface rtc.begin(); // initialize the RTC delay(50); // RTC.adjust(DateTime((__DATE__), (__TIME__))); // uncomment once to set the time if (!SD.begin(10)) { // initialize the SD card Serial.println("No SD"); } else { Serial.println("SD OK"); } if (!lox.begin()) { Serial.println(F("lox Fail")); } else { Serial.println(F("lox OK")); } } // end setup void loop() { DateTime now = rtc.now(); // read the time from the RTC, then construct a data string: sprintf(TmeStrng, "%04d/%02d/%02d,%02d:%02d:%02d", now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second()); utc = (now.unixtime()); // time code (good for graphing) // Serial.println(TmeStrng); // for checking operation using UART adapter // Read the VL53LOX: for(int z = 0; z < loxCnt; z++){ VL53L0X_RangingMeasurementData_t measure; lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout! if (measure.RangeStatus != 4) { // phase failures have incorrect data loxSum = loxSum + (measure.RangeMilliMeter); } // end if } //end for loxMean = loxSum/loxCnt; loxSum = 0; // Serial.print(loxMean); // Serial.println(" mm"); // end read the VL53LOX // write the data to the SD card: File dataFile = SD.open("dataloxTPL.txt", FILE_WRITE); dataFile.print(TmeStrng);dataFile.print(","); dataFile.print(utc);dataFile.print(","); dataFile.print(loxMean);dataFile.print(","); dataFile.flush(); dataFile.close(); delay(300); // probably not needed while (1) // toggle donePin so TPL5110 will cut power and turn off logger { digitalWrite(donePin, HIGH); delay(1); digitalWrite(donePin, LOW); delay(1); } // end while } // end main loop