Advertisement
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: "Distance Measurement"
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2025-02-09 12:21:42
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* the system will utilize data from the ultrasonic */
- /* sensor and ouput in serial monitor */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Ultrasonic.h> //https://github.com/ErickSimoes/Ultrasonic
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void updateOutputs(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t ultrasonicsensor_HC_SR04_Echo_PIN_D3 = 3;
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t ultrasonicsensor_HC_SR04_Trigger_PIN_D2 = 2;
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- /***** used to store raw data *****/
- bool ultrasonicsensor_HC_SR04_Trigger_PIN_D2_rawData = 0;
- /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
- /***** used to store data after characteristic curve transformation *****/
- float ultrasonicsensor_HC_SR04_Trigger_PIN_D2_phyData = 0.0;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- // Create an instance of the Ultrasonic class
- Ultrasonic ultrasonic(ultrasonicsensor_HC_SR04_Trigger_PIN_D2, ultrasonicsensor_HC_SR04_Echo_PIN_D3);
- void setup(void)
- {
- // Initialize serial communication
- Serial.begin(9600);
- // Set pin modes
- pinMode(ultrasonicsensor_HC_SR04_Echo_PIN_D3, INPUT);
- pinMode(ultrasonicsensor_HC_SR04_Trigger_PIN_D2, OUTPUT);
- }
- void loop(void)
- {
- // Refresh output data
- updateOutputs();
- // Read distance from the ultrasonic sensor
- ultrasonic.distanceRead(); // Get the distance in cm
- // Print the distance to the serial monitor
- Serial.print("Distance: ");
- Serial.print(ultrasonic.read()); // Read and print the distance
- Serial.println(" cm");
- // Delay for a short period before the next reading
- delay(500);
- }
- void updateOutputs()
- {
- // Trigger the ultrasonic sensor to take a reading
- digitalWrite(ultrasonicsensor_HC_SR04_Trigger_PIN_D2, HIGH);
- delayMicroseconds(10);
- digitalWrite(ultrasonicsensor_HC_SR04_Trigger_PIN_D2, LOW);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement