Advertisement
DanielKrastev-bit

Ultrasonic sensor

Apr 24th, 2024 (edited)
543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3.  
  4. // Define the pins for the ultrasonic sensor
  5. const int trigPin = 9;
  6. const int echoPin = 10;
  7.  
  8. // Define variables for duration and distance
  9. long duration;
  10. int distance;
  11.  
  12. // Initialize the LCD object with the address and dimensions of your LCD
  13. LiquidCrystal_I2C lcd(0x27, 16, 2);
  14.  
  15. // Define LED pins
  16. const int ledLow = 11;
  17. const int ledMid = 12;
  18. const int ledHigh = 13;
  19.  
  20. // Function declarations
  21. void initialize();
  22. void measureDistance();
  23. void displayDistance();
  24. void determineDistanceCategory();
  25. void turnOnLowLED();
  26. void turnOnMidLED();
  27. void turnOnHighLED();
  28.  
  29. void setup() {
  30.   initialize();
  31. }
  32.  
  33. void loop() {
  34.   measureDistance();
  35.   displayDistance();
  36.   determineDistanceCategory();
  37.   delay(500);
  38. }
  39.  
  40. void initialize() {
  41.   // Initialize serial communication
  42.   Serial.begin(9600);
  43.  
  44.   // Initialize LCD display
  45.   lcd.init();
  46.   lcd.backlight();
  47.   lcd.setCursor(0,0);
  48.   lcd.print("Distance: ");
  49.  
  50.   // Set LED pins as outputs
  51.   pinMode(ledLow, OUTPUT);
  52.   pinMode(ledMid, OUTPUT);
  53.   pinMode(ledHigh, OUTPUT);
  54.  
  55.   // Set trigPin as an output and echoPin as an input
  56.   pinMode(trigPin, OUTPUT);
  57.   pinMode(echoPin, INPUT);
  58. }
  59.  
  60. void measureDistance() {
  61.   // Clear the trigPin
  62.   digitalWrite(trigPin, LOW);
  63.   delayMicroseconds(2);
  64.  
  65.   // Send a pulse to the trigPin
  66.   digitalWrite(trigPin, HIGH);
  67.   delayMicroseconds(10);
  68.   digitalWrite(trigPin, LOW);
  69.  
  70.   // Measure the duration of the pulse on the echoPin
  71.   duration = pulseIn(echoPin, HIGH);
  72.  
  73.   // Calculate the distance in centimeters
  74.   distance = duration * 0.034 / 2;
  75. }
  76.  
  77. void displayDistance() {
  78.   // Print the distance to the Serial Monitor
  79.   Serial.print("Distance: ");
  80.   Serial.print(distance);
  81.   Serial.println(" cm");
  82.  
  83.   // Display the distance on the LCD display
  84.   lcd.setCursor(10,0);
  85.   lcd.print(distance);
  86.   lcd.print(" cm   ");
  87. }
  88.  
  89. void determineDistanceCategory() {
  90.   // Clear the second row of the LCD display
  91.   lcd.setCursor(0,1);
  92.   lcd.print("                  "); // Clear the row
  93.  
  94.   // Determine the distance category and display it on the LCD
  95.   lcd.setCursor(0,1);
  96.   if(distance < 20) {
  97.     lcd.print("Low");
  98.     turnOnLowLED();
  99.   } else if(distance >= 20 && distance < 50) {
  100.     lcd.print("Mid");
  101.     turnOnMidLED();
  102.   } else {
  103.     lcd.print("High");
  104.     turnOnHighLED();
  105.   }
  106. }
  107.  
  108. void turnOnLowLED() {
  109.   digitalWrite(ledLow, HIGH);
  110.   digitalWrite(ledMid, LOW);
  111.   digitalWrite(ledHigh, LOW);
  112. }
  113.  
  114. void turnOnMidLED() {
  115.   digitalWrite(ledLow, LOW);
  116.   digitalWrite(ledMid, HIGH);
  117.   digitalWrite(ledHigh, LOW);
  118. }
  119.  
  120. void turnOnHighLED() {
  121.   digitalWrite(ledLow, LOW);
  122.   digitalWrite(ledMid, LOW);
  123.   digitalWrite(ledHigh, HIGH);
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement