Devices & Components
Arduino Mega 2560 Rev3
SG90 Micro Servo
Resistors 220 ohm, 820 ohm, 10k, 15 ohm, 4k7, 220 ohm; 5k6
10kOhm potentiometer
10 Segment LED Bar Graph - Red
Ultrasonic Sensor HCSR04 Case Stand Mount
Software & Tools
wokwi
Project description
Code
HCSR04_Sweep_Range_Variation_Led_Bar
cpp
Code for the project and task to trace the circuit
1#include <Servo.h> 2 3#define trigPin 22 // Ultrasonic sensor trig pin 4#define echoPin 24 // Ultrasonic sensor echo pin 5#define servoPin 9 // Servo motor signal pin 6 7Servo myservo; 8int pos = 0; // Servo position (0-180 degrees) 9int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 10, 11, 12}; // LED pins 10int ledScanPos[] = {27, 29, 31, 33, 35, 37, 39, 41, 43, 45}; //LED Scans 11int ledDist[] = {26, 28, 30, 32, 34, 36, 38, 40, 42, 44}; 12int numLeds = sizeof(ledPins) / sizeof(ledPins[0]); 13int dt=200; 14int potPin=A0, mapped_value, distance_pot, distIndex; 15long duration, distance_cm; 16 17void setup() { 18 Serial.begin(9600); 19 myservo.attach(servoPin); 20 pinMode(trigPin, OUTPUT); 21 pinMode(echoPin, INPUT); 22 for (int i = 0; i < numLeds; i++) { 23 pinMode(ledPins[i], OUTPUT); 24 } 25} 26 27void loop() { 28 //One direction scan 29 for (pos = 0; pos <= 180; pos += 18) { // Scan in 18-degree increments 30 myservo.write(pos); 31 delay(dt); // Adjust scan speed as needed 32 33 digitalWrite(trigPin, LOW); 34 delayMicroseconds(2); 35 digitalWrite(trigPin, HIGH); 36 delayMicroseconds(10); 37 digitalWrite(trigPin, LOW); 38 39 duration = pulseIn(echoPin, HIGH); 40 distance_cm = duration * 0.034 / 2; 41 42 mapped_value = analogRead(potPin); 43 distance_pot = map(mapped_value, 0, 1023, 10, 100); 44 distIndex = map(mapped_value, 0, 1023, 0, 10); 45 46 for(int k=0; k<10; k++){ 47 digitalWrite(ledDist[k], k<distIndex ? HIGH:LOW); 48 } 49 50 51 int ledIndex = pos / 18; // Determine which LED corresponds to the servo angle 52 digitalWrite(ledScanPos[ledIndex], HIGH); //Tells the status of scan 53 54 if (distance_cm < distance_pot) { 55 digitalWrite(ledPins[ledIndex], HIGH); // Turn on LED if obstacle detected 56 } else { 57 digitalWrite(ledPins[ledIndex], LOW); // Turn off LED if no obstacle 58 } 59 60 Serial.print("Angle: "); 61 Serial.print(pos); 62 Serial.print(" degrees, Distance: "); 63 Serial.print(distance_cm); 64 Serial.println(" cm"); 65 } 66 67 //Other direction scan 68 for (pos = 180; pos >= 0; pos -= 18) { // Scan in 18-degree increments 69 myservo.write(pos); 70 delay(dt); // Adjust scan speed as needed 71 72 digitalWrite(trigPin, LOW); 73 delayMicroseconds(2); 74 digitalWrite(trigPin, HIGH); 75 delayMicroseconds(10); 76 digitalWrite(trigPin, LOW); 77 78 duration = pulseIn(echoPin, HIGH); 79 distance_cm = duration * 0.034 / 2; 80 81 mapped_value = analogRead(potPin); 82 distance_pot = map(mapped_value, 0, 1023, 10, 100); 83 distIndex = map(mapped_value, 0, 1023, 0, 10); 84 85 for(int k=0; k<10; k++){ 86 digitalWrite(ledDist[k], k<distIndex ? HIGH:LOW); 87 } 88 89 int ledIndex = pos / 18; // Determine which LED corresponds to the servo angle 90 digitalWrite(ledScanPos[ledIndex], LOW); //Tells the status of scan 91 92 if (distance_cm < distance_pot) { 93 digitalWrite(ledPins[ledIndex], HIGH); // Turn on LED if obstacle detected 94 } else { 95 digitalWrite(ledPins[ledIndex], LOW); // Turn off LED if no obstacle 96 } 97 98 Serial.print("Angle: "); 99 Serial.print(pos); 100 Serial.print(" degrees, Distance: "); 101 Serial.print(distance_cm); 102 Serial.println(" cm"); 103 } 104 105 // Turn off all LEDs at the end of scan 106 107 for (int i = 0; i < numLeds; i++) { 108 digitalWrite(ledScanPos[i], LOW); 109 } 110 111 delay(dt); // Delay between scans (adjust as needed) 112}
Downloadable files
Circuit Diagram
Those who are in a hurry, you're welcome
Screenshot 2024-03-20 232451.jpg

Comments
Only logged in users can leave comments