Advertisement
pleasedontcode

Ultrasonic Display rev_01

Jul 24th, 2024
537
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: Ultrasonic Display
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-07-25 04:13:25
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Arduino program calculates distance with SR04 */
  21.     /* displayed in LCD I2C in the form of numbers and */
  22.     /* bar graphs */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <Wire.h>
  27. #include <LiquidCrystal_I2C.h>  //https://github.com/marcoschwartz/LiquidCrystal_I2C
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32. void updateOutputs(void);
  33. float readDistance(void);
  34. void displayDistance(float distance);
  35.  
  36. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  37. const uint8_t sr04_HC_SR04_Echo_PIN_D3      = 3;
  38.  
  39. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  40. const uint8_t sr04_HC_SR04_Trigger_PIN_D2       = 2;
  41.  
  42. /***** DEFINITION OF I2C PINS *****/
  43. const uint8_t lcd_LCD1602I2C_I2C_PIN_SDA_A4     = A4;
  44. const uint8_t lcd_LCD1602I2C_I2C_PIN_SCL_A5     = A5;
  45. const uint8_t lcd_LCD1602I2C_I2C_SLAVE_ADDRESS      = 39;
  46.  
  47. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  48. // Initialize the LiquidCrystal_I2C object with the I2C address and dimensions
  49. LiquidCrystal_I2C lcd(lcd_LCD1602I2C_I2C_SLAVE_ADDRESS, 20, 4); // 20 columns and 4 rows
  50.  
  51. void setup(void)
  52. {
  53.     // Set up the pins for the ultrasonic sensor
  54.     pinMode(sr04_HC_SR04_Echo_PIN_D3, INPUT);
  55.     pinMode(sr04_HC_SR04_Trigger_PIN_D2, OUTPUT);
  56.  
  57.     // Initialize the LCD
  58.     lcd.init(); // Initialize the LCD
  59.     lcd.backlight(); // Turn on the backlight
  60. }
  61.  
  62. void loop(void)
  63. {
  64.     // Read the distance from the ultrasonic sensor
  65.     float distance = readDistance();
  66.    
  67.     // Update the LCD display with the distance
  68.     displayDistance(distance);
  69.  
  70.     // Wait for a short period before the next reading
  71.     delay(1000); // Delay for 1 second
  72. }
  73.  
  74. float readDistance()
  75. {
  76.     // Clear the trigger pin
  77.     digitalWrite(sr04_HC_SR04_Trigger_PIN_D2, LOW);
  78.     delayMicroseconds(2);
  79.    
  80.     // Set the trigger pin high for 10 microseconds
  81.     digitalWrite(sr04_HC_SR04_Trigger_PIN_D2, HIGH);
  82.     delayMicroseconds(10);
  83.     digitalWrite(sr04_HC_SR04_Trigger_PIN_D2, LOW);
  84.    
  85.     // Read the echo pin
  86.     long duration = pulseIn(sr04_HC_SR04_Echo_PIN_D3, HIGH);
  87.    
  88.     // Calculate the distance in cm (speed of sound is 34300 cm/s)
  89.     float distance = (duration * 0.0343) / 2; // Divide by 2 for the round trip
  90.     return distance;
  91. }
  92.  
  93. void displayDistance(float distance)
  94. {
  95.     // Clear the LCD before displaying new data
  96.     lcd.clear();
  97.    
  98.     // Display the distance in cm
  99.     lcd.setCursor(0, 0);
  100.     lcd.print("Distance: ");
  101.     lcd.print(distance);
  102.     lcd.print(" cm");
  103.  
  104.     // Display a simple bar graph based on the distance
  105.     lcd.setCursor(0, 1);
  106.     lcd.print("Bar: ");
  107.     int barLength = map(distance, 0, 200, 0, 16); // Map distance to bar length (max 16)
  108.     for (int i = 0; i < barLength; i++) {
  109.         lcd.write(255); // Print a filled block character
  110.     }
  111. }
  112.  
  113. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement