//initialize constants int trigPin = 17; int echoPin= 18; #define SOUND_SPEED 0.034 #define CM_TO_INCH 0.393701 //create variables long duration; double count; double totalaverage; double number; unsigned long longTime; //TA recomendded using a struct //Is this struct nessecary? What is the point of this as opposed to just normaly declaring it like the others above? //Pros: Looks cool, all in one place //Cons: Unfamiliar, extra code that requires more thought and understanding //typedef struct{ double distanceCm; double distanceInch; //} measurement; //measurement data; double allaverage(double y, double x){ double result; result = y/x; return result; } void setup() { Serial.begin(9600); //Configures the serial comunication baud rate pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input Serial.println("Starting Program"); } void loop() { // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(5); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(5); digitalWrite(trigPin, LOW); //This block below is just for my sake, does not need to be in final draft number++; Serial.print("number of loops: "); Serial.println(number); ////////////////////////////////// // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Calculate the distance distanceCm = duration * SOUND_SPEED/2; // Convert to inches /*data*/ distanceInch = distanceCm * CM_TO_INCH; // Prints the distance in the Serial Monitor in cm and inches Serial.print("Distance (cm): "); Serial.println(distanceCm); Serial.print("Distance (inch): "); Serial.println(distanceInch); if(distanceCm < 3.16){ count++; Serial.print("Count: "); Serial.println(count); } //Function Parameters: //totalaverage = count/number; allaverage(count, number); //Reduce delay delay(500); //system timer for 10 minutes if(millis() < longTime + 600000){ //collect an average somehow //Is there a way to have i increase infinitley? - Dynamic Array? if(count); count i++; //averagedata[i] //delay(10000) millis() = longTime; } if(Serial.available() > 0){ char message = Serial.read(); if (message == 'p'){ Serial.println("--------------------You input the command 'p' --------------------------"); //Should print a list of 10 minute averages //Should print the highest value measured //Should print the lowest value measured //Should print one overall average since the start of time Serial.print("Average amount of disturbances by number loops: "); Serial.println(totalaverage); } //This is for convenience when testing else if(message == 'a'){ //Should print a list of 10 minute averages Serial.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"); //number of loops Serial.print("number of loops: "); Serial.println(number); } } }