int lastTurn = 0; long duration; //Variable named duration stores the time between the //Ultrasonic sensor sending a sound wave and the wave returning to the sensor //It is a long variable because it may be a large number // The farther the object in front of the sensor, the longer the duration long distance; //Variable named distance stores the distance between the sensor and the object in front of it. //It is a long variable because it may be a large number //It is determined by a calculation that converts (changes) the duration into distance const int trigPin = 2; //The trigPin is connected to pin D2 - so 2. It is const because it never changes //It is an int (integer) because it is a small number const int echoPin = 3; //The echoPin is connected to pin D3 - so 3. It is const because it never changes void setup() { Serial.begin (9600); //For testing - so you can see the distance that the Nano is calculating on your Macintosh screens pinMode(trigPin, OUTPUT); //trigPin is an output because it sends the sound wave pinMode(echoPin, INPUT); //echoPin is an input because it receives the sound wave } void loop() { // The sensor is triggered by a HIGH pulse (sound) of 10 or more microseconds //(1/1000000 or 1/million of a second) //1 million microseconds = 1 second //Start with Low Pulse for clean High pulse digitalWrite(trigPin, LOW); //Tells the trigPin to do nothing; remember LOW is 0 delayMicroseconds(5); //The LOW or do nothing message lasts 5 microseconds //A microsecond is 1/1000 of a millisecond // A millisecond is 1/1,000,000 of a second digitalWrite(trigPin, HIGH); //Tells the trigPin to send a sound wave; remember HIGH is 1 delayMicroseconds(12); // The sending of the sound wave has to last longer than 10 microseconds (10/1,000,000 of a second) digitalWrite(trigPin, LOW); // Tells the trigPin to do nothing duration = pulseIn(echoPin, HIGH); //gets the time between the trigPin sending a sound wave //and the echoPin receiving the sound wave //this time period is stored in the variation duration distance = (duration/2) *0.0135; // this calculation converts the duration to distance and stores it in the variable named distance //it takes duration and divides by 2 because the duration is the time to the object and back //we only want the distance for one direction //then multiply that by speed of sound 0.0135 //For testing Serial.print(distance); //This is for testing - it prints the distance calculation on your computer Serial.print(" in "); //This is for testing - it print in so you know that it is inches Serial.println(); // This is for testing - it essentially prints a return so that the next distance is on a new line delay(1000); //delay alone refers to 1/1000 of a second so 1000 is 1 second then the loop runs again }