// ---------------------------------------------------------------- // // Arduino Distance Classification Using Ultrasonic Sensor // // Date: 10/28/2022 // // Author: Brayan Trejo // Email: brayan.trejo@colostate.edu // // Department of Electrical and Computer Engineering // Colorado State University // // Revised 11/1/2023 with suggestions from Ross and Fatimah // ---------------------------------------------------------------- // // Define Arduino Pinout connections #define PIN_ECHO 2 // Connect Arduino pin 2 ---> Echo pin of Ultrasonic Sensor #define PIN_TRIG 3 // Connect Arduino pin 3 ---> Trig pin of Ultrasonic Sensor #define PIN_BUZZER 11 // Connect Arduino pin 11 ---> Positive terminal of Buzzer. Fun fact- certain pins have PWM capabilities. #define PIN_GREEN_LED 5 // Connect Arduino pin 5 ---> Positive terminal of Green LED #define PIN_BLUE_LED 6 // Connect Arduino pin 6 ---> Positive terminal of Blue LED #define PIN_RED_LED 7 // Connect Arduino pin 7 ---> Positive terminal of Red LED // Define variables used for calculating and classifying distance double duration; // Variable for the duration of sound wave round-trip travel double distance; // Variable for the distance measurement // ***TO DO***: ENTER THRESHOLD VALUES (in centimeters), MAXIMUM VALUE = 50 int long_range = ; // Variable that sets maximum detection distance int mid_range = ; // Variable that sets mid-range detection distance int close_range = ; // Variable that sets close-range detection distance // ***TO DO***: Setup code. Runs only one time.Setup the respective pins to be either Outputs or Inputs. void setup() { pinMode(PIN_TRIG, ); // Sets the Trig Pin as an OUTPUT pinMode(PIN_ECHO, ); // Sets the Echo Pin as an INPUT pinMode(PIN_BUZZER, ); // Sets the Buzzer Pin as an OUTPUT pinMode(PIN_RED_LED, ); // Sets the Red LED Pin as an OUTPUT pinMode(PIN_BLUE_LED, ); // Sets the Blue LED Pin as an OUTPUT pinMode(PIN_GREEN_LED, ); // Sets the Green LED Pin as an OUTPUT Serial.begin(9600); // Starts the serial communication with the PC Serial.println("Distance Classification System With Arduino Uno R3"); // Prints text to the serial monitor } // MAIN CODE: RUNS REPEATEDLY void loop() { distance = get_Distance(); classify_Distance(distance); delay(5); } // ********************************************** PROGRAM FUNCTIONS ********************************************* // This function computes the distance of an object using the ultrasonic sensor module. double get_Distance(){ digitalWrite(PIN_TRIG, LOW); delayMicroseconds(2); digitalWrite(PIN_TRIG, HIGH); delayMicroseconds(10); digitalWrite(PIN_TRIG, LOW); duration = pulseIn(PIN_ECHO, HIGH); // Returns the soundwave travel time in microseconds distance = duration * 0.034 / 2.0; // Calculates the distance Serial.print("Distance: "); // Prints the distance to the PC Serial.print(distance); Serial.println(" cm"); return distance; } // This function accepts the distance calculation and classifies its range (i.e., FAR, MID, CLOSE). void classify_Distance(double distance){ // ***TO DO***: ENTER HIGH or LOW for all digitalWrite statements below. HIGH= Turn on, LOW=turn off. if(distance > long_range){ Serial.print("\nOBJECT OUT OF RANGE - "); digitalWrite(PIN_GREEN_LED, ); digitalWrite(PIN_BLUE_LED, ); digitalWrite(PIN_RED_LED, ); } else if ((distance < long_range) && (distance > mid_range)){ Serial.print("\nOBJECT DETECTED - RANGE=FAR - "); digitalWrite(PIN_GREEN_LED, ); digitalWrite(PIN_BLUE_LED, ); digitalWrite(PIN_RED_LED, ); } else if ((distance < mid_range) && (distance > close_range)){ Serial.print("\nOBJECT DETECTED - RANGE=MID - "); digitalWrite(PIN_GREEN_LED, ); digitalWrite(PIN_BLUE_LED, ); digitalWrite(PIN_RED_LED, ); } else { Serial.print("\nOBJECT DETECTED - RANGE=CLOSE - "); digitalWrite(PIN_GREEN_LED, ); digitalWrite(PIN_BLUE_LED, ); digitalWrite(PIN_RED_LED, ); make_sound(); } } // ***TO DO***: This function generates a sound on the buzzer and uses PWM to change the audio signal. Enter a value between 0 and 255. void make_sound(){ analogWrite(PIN_BUZZER, ); //This value is the max % of the dutyCycle. delay(500); analogWrite(PIN_BUZZER, 0); }