// ---------------------------------------------------------------- // // Arduino Parking Monitoring System using Ultrasonic Sensor // // Date: 10/28/2022 // // Author: Brayan Trejo // Email: brayan.trejo@colostate.edu // // Department of Electrical and Computer Engineering // Colorado State University // ---------------------------------------------------------------- // // 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_BUZZ 4 // Connect Arduino pin 4 ---> Positive terminal of Buzzer #define PIN_GLED 5 // Connect Arduino pin 5 ---> Positive terminal of Green LED #define PIN_BLED 6 // Connect Arduino pin 6 ---> Positive terminal of Blue LED #define PIN_RLED 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 int long_range = 30; // Variable that sets maximum detection distance int mid_range = 15; // Variable that sets mid-range detection distance int close_range = 7; // Variable that sets close-range detection distance // Setup code. Runs only one time. void setup() { pinMode(PIN_TRIG, OUTPUT); // Sets the Trig Pin as an OUTPUT pinMode(PIN_ECHO, INPUT); // Sets the Echo Pin as an INPUT pinMode(PIN_BUZZ, OUTPUT); // Sets the Buzzer Pin as an OUTPUT pinMode(PIN_RLED, OUTPUT); // Sets the Red LED Pin as an OUTPUT pinMode(PIN_BLED, OUTPUT); // Sets the Blue LED Pin as an OUTPUT pinMode(PIN_GLED, OUTPUT); // Sets the Green LED Pin as an OUTPUT Serial.begin(9600); // Starts the serial communication with the PC Serial.println("Parking Monitoring System With Arduino Uno R3"); // Prints text to the serial monitor // String user_name = "[PUT YOUR NAME HERE]"; Serial.println("Created by: " + user_name + "\n"); delay(2000); } // MAIN CODE: RUNS REPEATEDLY void loop() { distance = get_Distance(); classify_Distance(distance); delay(20); } // ****************************************** 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){ if(distance > long_range){ Serial.print("\nOBJECT OUT OF RANGE - "); // digitalWrite(PIN_GLED, ); // digitalWrite(PIN_BLED, ); // digitalWrite(PIN_RLED, ); } else if ((distance < long_range) && (distance > mid_range)){ Serial.print("\nOBJECT DETECTED - RANGE=FAR - "); // digitalWrite(PIN_GLED, ); // digitalWrite(PIN_BLED, ); // digitalWrite(PIN_RLED, ); } else if ((distance < mid_range) && (distance > close_range)){ Serial.print("\nOBJECT DETECTED - RANGE=MID - "); // digitalWrite(PIN_GLED, ); // digitalWrite(PIN_BLED, ); // digitalWrite(PIN_RLED, ); } else { Serial.print("\nOBJECT DETECTED - RANGE=CLOSE - "); // digitalWrite(PIN_GLED, ); // digitalWrite(PIN_BLED, ); // digitalWrite(PIN_RLED, ); make_sound(); } } // This function generates a sound on the buzzer void make_sound(){ tone(PIN_BUZZ, 500); // Starts sound delay(10); // Makes sound last 10 miliseconds noTone(PIN_BUZZ); // Stops sound }