/* * 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. */ // Setup for SD card datalogging #include // Files and variables for sms and email #include #include String startTime; String alarmTime; // Time marker for Bauer ON status String alertTime; // Time marker for SMS messages int flagBauer = 0; // Flag for Bauer on (1) and off (0) const int thresholdTimeOn = 300; // Time (in seconds) before Bauer ON status written const int thresholdTimeSMS = 900; // Time (in seconds) before SMS message is sent const int thresholdDistanceOn = 140; // Distance (in centimeters) before compressor starts const int thresholdDistanceOff = 200; // Distance (in centimeters) before compressor stops const int thresholdDistanceSMS = 90; // Distance (in centimeters) before SMS text is sent long hourcount = 0; // 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 bridge for linux module Bridge.begin(); /* // Initialize serial communication at 9600 bits per second Serial.begin(9600); // Starts the serial communication FileSystem.begin(); while (!SerialUSB); // 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 alarm time and report startup startTime += getEpochTime(); SerialUSB.print("System online at epoch "); SerialUSB.println(startTime); writeLog(hourcount,"STARTUP",startTime,0); alarmTime = startTime; alertTime = startTime; } void loop() { int measureDistance; String measureTime; measureDistance = getDistance(); // Safety to remove large errant measurements if (measureDistance > 300) { measureDistance = 170; } measureTime += getEpochTime(); // Prints the distance on the Serial Monitor Serial.print("Epoch: "); Serial.print(measureTime); Serial.print(" Distance: "); Serial.println(measureDistance); /* // Write to logfile every hour past start if (measureTime.toInt() > startTime.toInt() + hourcount*3600) { writeLog(hourcount,"MEASURE",measureTime,measureDistance); hourcount++; } */ /* * If the distance is lower than the threshold, set the bag relay to run the * compressor. Otherwise, set the bag relay to keep the compressor at idle. */ if (measureDistance < thresholdDistanceOn) { digitalWrite(bagPin, LOW); // Activate the relay, and start the compressor // Change Bauer flag to denote compressor is ON flagBauer = 1; // Write to logfile writeLog(hourcount,"BAUERON",measureTime,measureDistance); // Reset the alarm time alarmTime = measureTime; } else if (measureDistance > thresholdDistanceOff) { digitalWrite(bagPin, HIGH); // Deactivate the relay, and stop the compressor // Change Bauer flag to denote compressor is OFF flagBauer = 0; } else { if ((measureTime.toInt() > alarmTime.toInt() + thresholdTimeOn) && (flagBauer == 1)) { // Write to logfile writeLog(hourcount,"BAUERON",measureTime,measureDistance); // Reset the alarm time alarmTime = measureTime; } } /* * If the distance is lower than the threshold, and the thresholdTime has * passed since the last SMS message. Then send a SMS message as a warning. */ if ((measureDistance < thresholdDistanceSMS) && (measureTime.toInt() > alertTime.toInt() + thresholdTimeSMS)) { /* // Send the SMS text message Process p; p.runShellCommand("/usr/bin/python /mnt/sda1/python_sms_wylie"); //sms Wylie */ // Write to logfile writeLog(hourcount,"WARNING",measureTime,measureDistance); // Reset the alert time alertTime = measureTime; } 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 the count, status, epoch, and distance to a log file */ void writeLog (int entryCount, String systemStatus, 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(entryCount); dataFile.print(","); dataFile.print(systemStatus); 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 { SerialUSB.println("error opening datalog.csv"); } }