/* * This sketch is to monitor the helium recover bag height * using an ultrasonic sensor suspended above the bag. * Rev 6 has a Quad Alphanumeric Display. * Rev 7 has less reporting and a lower threshold for distance ON. * Rev 8 runs the Bauer at chosen intervals and has the SMS removed. */ String lastONTime; // Time marker for ON status int flagBauer = 0; // Flag for Bauer on (1) and off (0) const int safeDisatance = 170; // Distance considered safe for default const int thresholdDistanceOn = 140; // Distance (in centimeters) before compressor starts const int thresholdDistanceOff = 200; // Distance (in centimeters) before compressor stops long hourcount = 24; // Minimum number of hours between Bauer ON status. Never set less than 2! // Setup for SD card datalogging #include // Setup pins for ultrasonic sensor const int trigPin = 9; const int echoPin = 10; // Setup for Quad Alphanumeric #include #include #include "Adafruit_LEDBackpack.h" Adafruit_AlphaNum4 alpha4 = Adafruit_AlphaNum4(); char displaybuffer[4] = {' ', ' ', ' ', ' '}; // Setup pin for relay for compressor control const int bagPin = 8; void setup() { /* // Initialize serial communication at 9600 bits per second Serial.begin(9600); // Starts the serial communication FileSystem.begin(); while (!Serial); // Wait for serial port to connect */ // Initialize the ultrasonic sensor pins pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input // Initialize the relay pin pinMode(bagPin, OUTPUT); digitalWrite(bagPin, HIGH); // Deactivate Relay // Initialize the Quad Alphanumeric alpha4.begin(0x70); // pass in the address alpha4.writeDigitRaw(3, 0x0); alpha4.writeDigitRaw(0, 0xFFFF); alpha4.writeDisplay(); delay(200); alpha4.writeDigitRaw(0, 0x0); alpha4.writeDigitRaw(1, 0xFFFF); alpha4.writeDisplay(); delay(200); alpha4.writeDigitRaw(1, 0x0); alpha4.writeDigitRaw(2, 0xFFFF); alpha4.writeDisplay(); delay(200); alpha4.writeDigitRaw(2, 0x0); alpha4.writeDigitRaw(3, 0xFFFF); alpha4.writeDisplay(); delay(200); alpha4.clear(); alpha4.writeDisplay(); // Initialize the time and report startup lastONTime += getEpochTime(); Serial.print("System online at epoch "); Serial.println(lastONTime); writeLog("GO","START_UP",lastONTime,0); } void loop() { int measureDistance; String measureTime; measureDistance = getDistance(); // Safety to remove large errant measurements if (measureDistance > 300) { measureDistance = safeDisatance; } measureTime += getEpochTime(); // Prints the distance on the Serial Monitor Serial.print("Epoch: "); Serial.print(measureTime); Serial.print(" Distance: "); Serial.println(measureDistance); if (flagBauer < 0.5) { /* * If more than the alotted time has passed and the distance is less than the safe distance, * then start the compressor. */ if ((measureTime.toInt() > lastONTime.toInt() + hourcount*3600) && (measureDistance < safeDisatance)) { // Activate the relay to start the compressor, and change the flag to denote compressor is ON digitalWrite(bagPin, LOW); flagBauer = 1; // Write to logfile writeLog("ON","DURATION",measureTime,measureDistance); // Update last time Bauer was ON lastONTime = measureTime; } /* * If the distance is less than the threshold, start the compressor. */ else if (measureDistance < thresholdDistanceOn) { // Activate the relay to start the compressor, and change the flag to denote compressor is ON digitalWrite(bagPin, LOW); flagBauer = 1; // Write to logfile writeLog("ON","DISTANCE",measureTime,measureDistance); // Update last time Bauer was ON lastONTime = measureTime; } } else { if (measureDistance > thresholdDistanceOff) { // Deactivate the relay to stop the compressor, and change the flag to denote compressor is OFF digitalWrite(bagPin, HIGH); flagBauer = 0; // Write to logfile writeLog("OFF","DISTANCE",measureTime,measureDistance); } } /* * Output the distance to the Quad Alphanumeric */ char c[4] = {' ',' ',' ',' '}; itoa(measureDistance,c,10); // Setup every digit to the buffer if(measureDistance > 999) { alpha4.writeDigitAscii(0, c[0]); alpha4.writeDigitAscii(1, c[1]); alpha4.writeDigitAscii(2, c[2]); alpha4.writeDigitAscii(3, c[3]); } else if (measureDistance > 99) { alpha4.writeDigitAscii(0, ' '); alpha4.writeDigitAscii(1, c[0]); alpha4.writeDigitAscii(2, c[1]); alpha4.writeDigitAscii(3, c[2]); } else if (measureDistance > 9) { alpha4.writeDigitAscii(0, ' '); alpha4.writeDigitAscii(1, ' '); alpha4.writeDigitAscii(2, c[0]); alpha4.writeDigitAscii(3, c[1]); } else { alpha4.writeDigitAscii(0, ' '); alpha4.writeDigitAscii(1, ' '); alpha4.writeDigitAscii(2, ' '); alpha4.writeDigitAscii(3, c[0]); } // Write buffer out to LCD alpha4.writeDisplay(); // Delay Between Measurements delay(5000); } // This function returns a string with the epoch time String getEpochTime() { String result; Process time; // Use the date command line utility to get the date and the time // The additional parameter is used to set the format time.begin("date"); time.addParameter("+%s"); // parameters: epoch time.run(); // run the command // Read the output of the command while (time.available() > 0) { char c = time.read(); if (c != '\n') { result += c; } } return result; } /* * This function returns the distance in centimeters * The Ultrasonic Sensor HC-SR04 code is by Dejan Nedelkovski, * www.HowToMechatronics.com */ int getDistance() { // Define sensor variables long duration; int distance; // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Calculating the distance in centimeters distance= duration*0.034/2; return distance; } /* * This function writes to the log file */ void writeLog (String systemStatus, String statusMethod, String recordTime, int recordDistance) { // Open the log file File dataFile = FileSystem.open("/mnt/sda1/datalog.csv", FILE_APPEND); // if the log file is available, write to it if (dataFile) { dataFile.print(systemStatus); dataFile.print(","); dataFile.print(statusMethod); dataFile.print(","); dataFile.print(recordTime); dataFile.print(","); dataFile.print(recordDistance); dataFile.print("\n"); dataFile.close(); } // If the file isn't open, pop up an error: else { Serial.println("error opening datalog.csv"); } }