//Lab 4 //ME 208 // Sara Wilson // Ultrasound Distance collector int trigPin = 12; //connects to the trigger pin on the distance sensor int echoPin = 13; //connects to the echo pin on the distance sensor float distance[100]; int push_button_pin = 2; int push_button_value; int i; void setup() { Serial.begin (9600); //set up a serial connection with the computer pinMode(trigPin, OUTPUT); //the trigger pin will output pulses of electricity pinMode(echoPin, INPUT); //the echo pin will measure the duration of pulses coming back from the distance sensor pinMode(push_button_pin,INPUT_PULLUP); } void loop() { push_button_value = digitalRead(push_button_pin); i=0; while(push_button_value==LOW) { distance[i] = getDistance(); //variable to store the distance measured by the sensor timecl = millis(); Serial.print(timecl); Serial.print(" "); //print units after the distance Serial.println(distance[i]); //print the distance that was measured delay(1000); push_button_value = digitalRead(push_button_pin); i = i+1; } delay(100); } //------------------FUNCTIONS------------------------------- //RETURNS THE DISTANCE MEASURED BY THE HC-SR04 DISTANCE SENSOR float getDistance() { float echoTime; //variable to store the time it takes for a ping to bounce off an object float calculatedDistance; //variable to store the distance calculated from the echo time //send out an ultrasonic pulse that's 10ms long digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); echoTime = pulseIn(echoPin, HIGH); //use the pulsein command to see how long it takes for the //pulse to bounce back to the sensor calculatedDistance = echoTime / 148.0; //calculate the distance of the object that reflected the pulse (half the bounce time multiplied by the speed of sound) return calculatedDistance; //send back the distance that was calculated }