pleasedontcode

Ultrasonic Calibration rev_01

Dec 4th, 2025
14
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 Calibration
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-12-04 15:51:11
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Le projet consiste à réaliser un télémètre à */
  21.     /* ultrasons utilisant un capteur HC-SR04, un écran */
  22.     /* LCD, et 3 boutons poussoirs.  Le télémètre doit */
  23.     /* mesurer une distance et afficher la valeur */
  24.     /* corrigée en tenant compte de la longueur fixe de */
  25.     /* l’appareil : 12c */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28.  
  29. /* START CODE */
  30.  
  31. // Includes
  32. #include <Wire.h>
  33. #include <LiquidCrystal_I2C.h>
  34. #include <NewPing.h>
  35.  
  36. // Define pins for HC-SR04
  37. #define TRIGGER_PIN 9
  38. #define ECHO_PIN 10
  39. #define MAX_DISTANCE 200 // Max distance in cm
  40.  
  41. // Define pins for buttons
  42. #define BUTTON1_PIN 2
  43. #define BUTTON2_PIN 3
  44. #define BUTTON3_PIN 4
  45.  
  46. // Initialize LCD with I2C address 0x27, 16 columns and 2 rows
  47. LiquidCrystal_I2C lcd(0x27, 16, 2);
  48.  
  49. // Initialize ultrasonic sensor
  50. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
  51.  
  52. // Device's fixed length in centimeters
  53. const float device_length = 12.0;
  54.  
  55. // Variables to store button states
  56. int button1State = LOW;
  57. int button2State = LOW;
  58. int button3State = LOW;
  59.  
  60. // Measurement variables
  61. unsigned int distance_cm;
  62. float corrected_distance;
  63.  
  64. void setup() {
  65.   // Initialize serial communication
  66.   Serial.begin(9600);
  67.  
  68.   // Initialize LCD
  69.   lcd.init();
  70.   lcd.backlight();
  71.  
  72.   // Initialize button pins
  73.   pinMode(BUTTON1_PIN, INPUT_PULLUP);
  74.   pinMode(BUTTON2_PIN, INPUT_PULLUP);
  75.   pinMode(BUTTON3_PIN, INPUT_PULLUP);
  76.  
  77.   // Display initial message
  78.   lcd.setCursor(0, 0);
  79.   lcd.print("Ultrasound Te9le9me8tre");
  80.   delay(2000);
  81.   lcd.clear();
  82. }
  83.  
  84. void loop() {
  85.   // Read buttons
  86.   button1State = digitalRead(BUTTON1_PIN);
  87.   button2State = digitalRead(BUTTON2_PIN);
  88.   button3State = digitalRead(BUTTON3_PIN);
  89.  
  90.   // Trigger ultrasonic measurement
  91.   delay(50); // Small delay for sensor stability
  92.   distance_cm = sonar.ping_cm();
  93.  
  94.   // Calculate corrected distance
  95.   // Correction based on device length
  96.   corrected_distance = distance_cm - device_length;
  97.  
  98.   // Ensure corrected distance is not negative
  99.   if (corrected_distance < 0) {
  100.     corrected_distance = 0;
  101.   }
  102.  
  103.   // Display the corrected distance
  104.   lcd.setCursor(0, 0);
  105.   lcd.print("Dist: ");
  106.   lcd.print(corrected_distance);
  107.   lcd.print(" cm     ");
  108.  
  109.   // Optionally, handle button presses for additional functions
  110.   // For this example, no specific button function is implemented
  111.  
  112.   delay(500); // Measurement interval
  113. }
  114.  
  115. /* END CODE */
  116.  
Advertisement
Add Comment
Please, Sign In to add comment