int countTen = 0; unsigned long timeTen = 0; int maxP = 0; int minP = 1000; const int trigPin = 32; const int echoPin = 33; //define sound speed in cm/uS #define SOUND_SPEED 0.034 #define CM_TO_INCH 0.393701 long duration; float distanceA = 0.0; float distanceB = 0.0; #include "FS.h" #include "SD.h" #include "SPI.h" //This creates a file if it doesn't already exist or overwrites whatever is in the file void writeFile(fs::FS &fs, const char * path, const char * message){ Serial.printf("Writing file: %s\n", path); File file = fs.open(path, FILE_WRITE); if(!file){ return; } if(file.print(message)){ } else { } file.close(); } //This adds integers to the file void appendFile(fs::FS &fs, const char * path, int message) { Serial.printf("Appending to file: %s\n", path); File file = fs.open(path, FILE_APPEND); if(!file){ //Serial.println("Failed to open file for appending"); return; } if(file.print(message, DEC)){ //Serial.println("Message appended"); } else { //Serial.println("Append failed"); } file.close(); } //This makes the file go to a new line void newLine(fs::FS &fs, const char * path) { Serial.printf("Going to a new line\n"); File file = fs.open(path, FILE_APPEND); if(!file){ return; } if(file.print('\n')){ } else { } file.close(); } //This deletes a file void deleteFile(fs::FS &fs, const char * path){ Serial.printf("Deleting file: %s\n", path); if(fs.remove(path)){ Serial.println("File deleted"); } else { Serial.println("Delete failed"); } } void setup() { // put your setup code here, to run once: Serial.begin(115200); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); if(!SD.begin(5)){ Serial.println("Card Mount Failed"); while(1); } uint8_t cardType = SD.cardType(); if(cardType == CARD_NONE){ Serial.println("No SD card attached"); while(1); } Serial.print("SD Card Type: "); if(cardType == CARD_MMC){ Serial.println("MMC"); } else if(cardType == CARD_SD){ Serial.println("SDSC"); } else if(cardType == CARD_SDHC){ Serial.println("SDHC"); } else { Serial.println("UNKNOWN"); } uint64_t cardSize = SD.cardSize() / (1024 * 1024); Serial.printf("SD Card Size: %lluMB\n", cardSize); //Deletes file to prevent multiple copies and then creates a new one deleteFile(SD, "/results.txt"); delay(100); writeFile(SD, "/results.txt", "Results: \n"); } void loop() { // put your main code here, to run repeatedly: //Checks if the distance has changed i.e. someone has passed by if(distanceA < 145.0 || distanceA > 190.0) { Serial.println("Someone passed the sensor"); //Marks down that someone has passed countTen++; delay(500); //This checks to see if the person is no longer standing in front of the sensor //It prevents counting someone who stands in front multiple times while(distanceA < 145.0 || distanceA > 190.0) { distanceA = 0; delay(200); Serial.println("Waiting"); // 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); duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds for(int i = 0; i < 5; i++) { distanceA = distanceA + (duration * SOUND_SPEED/2); delay(100); } distanceA = distanceA/5; } } //Gets the distance every half of a second // Calculate the distance for(int i = 0; i < 5; i++) { // 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); distanceA = distanceA + (duration * SOUND_SPEED/2); delay(100); } distanceA = distanceA/5; if(millis() > (timeTen + 600000)) { //do every 10 minutes Serial.print(countTen); Serial.println(" people have passed."); //Checks if there is a new max or min if(countTen > maxP) maxP = countTen; else if(countTen < minP) minP = countTen; //Send count to SD card appendFile(SD, "/results.txt", countTen); newLine(SD, "/results.txt"); countTen = 0; timeTen = millis(); } if(Serial.available() > 0) { //Outputs a summary if I input s into the serial port char input = Serial.read(); if(input == 's') { //Code to output max, min and summary Serial.print("The maximum people who passed in a 10 minute period is "); Serial.println(maxP); Serial.print("The minimum people who passed in a 10 minute period is "); Serial.println(minP); Serial.print("Time Elapsed: "); Serial.println(millis()/60000); } } }