Advertisement
pleasedontcode

"Distance Display" rev_01

Jan 14th, 2024
70
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: "Distance Display"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-01-14 14:50:45
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* find the distance the distance between the device */
  21.     /* and opaque object */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <Arduino.h>
  26. #include <Wire.h>
  27. #include <Adafruit_SSD1306.h>
  28. #include <U8g2_for_Adafruit_GFX.h>
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t sensor_HC_SR04_Echo_PIN_D3 = 3;
  36.  
  37. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  38. const uint8_t sensor_HC_SR04_Trigger_PIN_D2 = 2;
  39.  
  40. /***** DEFINITION OF I2C PINS *****/
  41. const uint8_t oled_SSD1306OledDisplay_I2C_PIN_SDA_A4 = A4;
  42. const uint8_t oled_SSD1306OledDisplay_I2C_PIN_SCL_A5 = A5;
  43. const uint8_t oled_SSD1306OledDisplay_I2C_SLAVE_ADDRESS = 0x3C;
  44.  
  45. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  46. Adafruit_SSD1306 display(oled_SSD1306OledDisplay_I2C_SLAVE_ADDRESS, oled_SSD1306OledDisplay_I2C_PIN_SDA_A4, oled_SSD1306OledDisplay_I2C_PIN_SCL_A5);
  47. U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;
  48.  
  49. void setup(void)
  50. {
  51.   // put your setup code here, to run once:
  52.   pinMode(sensor_HC_SR04_Echo_PIN_D3, INPUT);
  53.   pinMode(sensor_HC_SR04_Trigger_PIN_D2, OUTPUT);
  54.  
  55.   display.begin(SSD1306_SWITCHCAPVCC);
  56.   u8g2_for_adafruit_gfx.begin(display);
  57. }
  58.  
  59. void loop(void)
  60. {
  61.   // put your main code here, to run repeatedly:
  62.  
  63.   // Measure distance using HC-SR04 sensor
  64.   digitalWrite(sensor_HC_SR04_Trigger_PIN_D2, LOW);
  65.   delayMicroseconds(2);
  66.   digitalWrite(sensor_HC_SR04_Trigger_PIN_D2, HIGH);
  67.   delayMicroseconds(10);
  68.   digitalWrite(sensor_HC_SR04_Trigger_PIN_D2, LOW);
  69.  
  70.   unsigned long duration = pulseIn(sensor_HC_SR04_Echo_PIN_D3, HIGH);
  71.   float distance = duration * 0.034 / 2;
  72.  
  73.   // Display distance on OLED display
  74.   display.clearDisplay();
  75.   u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR14_tf);
  76.   u8g2_for_adafruit_gfx.setCursor(0,20);
  77.   u8g2_for_adafruit_gfx.print("Distance: ");
  78.   u8g2_for_adafruit_gfx.print(distance);
  79.   u8g2_for_adafruit_gfx.print(" cm");
  80.   display.display();
  81.  
  82.   delay(1000); // Wait for 1 second before measuring distance again
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement