Advertisement
pleasedontcode

"Arduino Distance Measurement" rev_01

Dec 29th, 2023
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "Arduino Distance Measurement"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2023-12-29 21:26:37
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* turn rgb led light to red and print "Out of Range" */
  21.     /* on display when sensor isn't detecting any objects */
  22.     /* and green when it detects objects with printing */
  23.     /* the distance of object from sensor in mm */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <Arduino.h>
  28. #include <Wire.h>
  29. #include <Ultrasonic.h>
  30. #include <LiquidCrystal_I2C.h>
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35.  
  36. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  37. const uint8_t sensor_HC_SR04_Echo_PIN_D3 = 3;
  38.  
  39. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  40. const uint8_t indicator_LEDRGB_Red_PIN_D2 = 2;
  41. const uint8_t indicator_LEDRGB_Green_PIN_D4 = 4;
  42. const uint8_t indicator_LEDRGB_Blue_PIN_D5 = 5;
  43.  
  44. /***** DEFINITION OF I2C PINS *****/
  45. const uint8_t display_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
  46. const uint8_t display_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
  47. const uint8_t display_LCD1602I2C_I2C_SLAVE_ADDRESS = 39;
  48.  
  49. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  50. Ultrasonic ultrasonic(sensor_HC_SR04_Echo_PIN_D3);
  51. LiquidCrystal_I2C lcd(display_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2);  // Initialize LCD object
  52.  
  53. void setup(void)
  54. {
  55.   pinMode(sensor_HC_SR04_Echo_PIN_D3, INPUT);
  56.   pinMode(indicator_LEDRGB_Red_PIN_D2, OUTPUT);
  57.   pinMode(indicator_LEDRGB_Green_PIN_D4, OUTPUT);
  58.   pinMode(indicator_LEDRGB_Blue_PIN_D5, OUTPUT);
  59.  
  60.   lcd.begin(16, 2);  // Initialize LCD
  61.   lcd.backlight();  // Turn on LCD backlight
  62. }
  63.  
  64. void loop(void)
  65. {
  66.   unsigned int distance = ultrasonic.read();  // Read the distance from the ultrasonic sensor
  67.  
  68.   if (distance == 0) {
  69.     // No object detected
  70.     digitalWrite(indicator_LEDRGB_Red_PIN_D2, HIGH);  // Turn on red LED
  71.     digitalWrite(indicator_LEDRGB_Green_PIN_D4, LOW);  // Turn off green LED
  72.     lcd.clear();  // Clear the LCD display
  73.     lcd.setCursor(0, 0);
  74.     lcd.print("Out of Range");
  75.   } else {
  76.     // Object detected
  77.     digitalWrite(indicator_LEDRGB_Red_PIN_D2, LOW);  // Turn off red LED
  78.     digitalWrite(indicator_LEDRGB_Green_PIN_D4, HIGH);  // Turn on green LED
  79.     lcd.clear();  // Clear the LCD display
  80.     lcd.setCursor(0, 0);
  81.     lcd.print("Distance: ");
  82.     lcd.print(distance);
  83.     lcd.print(" mm");
  84.   }
  85.  
  86.   delay(1000);  // Wait for 1 second before taking the next reading
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement