Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Ultrasonic Calibration
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2025-12-04 15:51:11
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Le projet consiste à réaliser un télémètre à */
- /* ultrasons utilisant un capteur HC-SR04, un écran */
- /* LCD, et 3 boutons poussoirs. Le télémètre doit */
- /* mesurer une distance et afficher la valeur */
- /* corrigée en tenant compte de la longueur fixe de */
- /* l’appareil : 12c */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- // Includes
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- #include <NewPing.h>
- // Define pins for HC-SR04
- #define TRIGGER_PIN 9
- #define ECHO_PIN 10
- #define MAX_DISTANCE 200 // Max distance in cm
- // Define pins for buttons
- #define BUTTON1_PIN 2
- #define BUTTON2_PIN 3
- #define BUTTON3_PIN 4
- // Initialize LCD with I2C address 0x27, 16 columns and 2 rows
- LiquidCrystal_I2C lcd(0x27, 16, 2);
- // Initialize ultrasonic sensor
- NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
- // Device's fixed length in centimeters
- const float device_length = 12.0;
- // Variables to store button states
- int button1State = LOW;
- int button2State = LOW;
- int button3State = LOW;
- // Measurement variables
- unsigned int distance_cm;
- float corrected_distance;
- void setup() {
- // Initialize serial communication
- Serial.begin(9600);
- // Initialize LCD
- lcd.init();
- lcd.backlight();
- // Initialize button pins
- pinMode(BUTTON1_PIN, INPUT_PULLUP);
- pinMode(BUTTON2_PIN, INPUT_PULLUP);
- pinMode(BUTTON3_PIN, INPUT_PULLUP);
- // Display initial message
- lcd.setCursor(0, 0);
- lcd.print("Ultrasound T e9l e9m e8tre");
- delay(2000);
- lcd.clear();
- }
- void loop() {
- // Read buttons
- button1State = digitalRead(BUTTON1_PIN);
- button2State = digitalRead(BUTTON2_PIN);
- button3State = digitalRead(BUTTON3_PIN);
- // Trigger ultrasonic measurement
- delay(50); // Small delay for sensor stability
- distance_cm = sonar.ping_cm();
- // Calculate corrected distance
- // Correction based on device length
- corrected_distance = distance_cm - device_length;
- // Ensure corrected distance is not negative
- if (corrected_distance < 0) {
- corrected_distance = 0;
- }
- // Display the corrected distance
- lcd.setCursor(0, 0);
- lcd.print("Dist: ");
- lcd.print(corrected_distance);
- lcd.print(" cm ");
- // Optionally, handle button presses for additional functions
- // For this example, no specific button function is implemented
- delay(500); // Measurement interval
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment