/*
Ultrasonic Tape Measure
Adapted by Kurt Kara and Nathan Young
created 3 Nov 2008
by David A. Mellis
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Ping
*/
// Constant input and output required brcause of 4-pin ultrasonic sensor. Also we need a separate input and output sensor in order to stop ONLY the output, with a switch, so that the last measurement is held, like a tape measure.
// Because of this, instead of pingPin, we will have to use trigPin for the output from the Arduino board, and echoPin as the input to the Arduino board:
const int echoPin = 2;
const int trigPin = 3;
#include <SD.h>
#include <SPI.h>
#define CS_PIN 53
int loop_count;
Sd2Card card;
SdVolume volume;
SdFile root;
//File textFile;
void save_distance(long save_cm) {
Serial.println("Saving data to file");
File textFile = SD.open("Dist.txt", FILE_WRITE);
root.print("Measurement (cm): ");
//root.println(microsecondsToCentimeters(delayMicroseconds));
root.println(save_cm);
textFile.close();
}
void setup() {
// initialize serial communication:
Serial.begin(9600);
Serial.print("Initializing SD card...");
if (!SD.begin(CS_PIN)) {
Serial.println("Card initialization failed!");
while(true);
}
Serial.println("Initialization done.");
Serial.println("Files in the card:");
File textFile = SD.open("Dist.txt");
if (textFile) {
Serial.print("Dist.txt: ");
while (textFile.available()) {
Serial.write(textFile.read());
}
textFile.close();
} else {
Serial.println("Error in opening Dist.txt!");
}
}
void loop() {
// establishs the variables for duration of the ping, and the distance result in centimeters. we are using only cm because in Engineering, we prefer base 10 measurement:
long duration, cm;
//File textFile;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
// This Ping goes throught the Trig pin, while the return signal goes through the Echo pin because of the previously mentioned 4-pin setup
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
// Because of the 4-pin setup, the Echo pin is used to read the signal from the PING))): a HIGH pulse
// whose duration is the time (in microseconds) from the sending of the ping
// to the reception of its echo off of an object.
// We need to define the echoPin as the input of the Arduino board, since we have separate input and output pins
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
Serial.print(cm); // print value on monitor
Serial.print("cm");
Serial.println();
if (loop_count <= 20) {
Serial.print(loop_count);
Serial.println(" loop iterations");
loop_count++;
save_distance(cm); // save the value of cm to the file on the SD card
}
// Delay pauses the function for a period of time
// Smaller number means more measurements in a shorter period of time, delay is in milliseconds.
// Making the delay shorter allows for measuring while moving, for instance along a curve, since it will return values fairly quickly, and at the end of the curve you can flip the switch to turn it off, retaining the values.
delay(500);
if (loop_count == 21) {
// read out the contents of the SD card and print to monitor
Serial.println("Read back the contents of the SD card");
// Example of reading file contents from the card:
File textFile = SD.open("Dist.txt");
if (textFile) {
Serial.print("Dist.txt: ");
while (textFile.available()) {
Serial.write(textFile.read());
Serial.write("cm");
}
textFile.close();
} else {
Serial.println("error opening Dist.txt!");
}
}
}
// Function that converts microseconds to cm
long microsecondsToCentimeters(long microseconds) {
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the object we
// take half of the distance travelled.
return microseconds / 29 / 2;
}