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: Ultrasonic Distance
- - Source Code compiled for: Arduino Pro Mini 5V
- - Source Code created on: 2024-02-16 14:48:51
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* The drone should have a trigger pin (D2) and an */
- /* echo pin (D3) connected to the HC-SR04 sensor for */
- /* accurate distance measurement. */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <Ultrasonic.h>
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void updateOutputs(void);
- void printDistance(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t sensor_HC_SR04_Echo_PIN_D3 = 3;
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t sensor_HC_SR04_Trigger_PIN_D2 = 2;
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- bool sensor_HC_SR04_Trigger_PIN_D2_rawData = false;
- /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
- float sensor_HC_SR04_Trigger_PIN_D2_phyData = 0.0;
- /****** DEFINITION OF CLASS INSTANCES*****/
- Ultrasonic ultrasonic(sensor_HC_SR04_Trigger_PIN_D2, sensor_HC_SR04_Echo_PIN_D3);
- void setup(void)
- {
- // put your setup code here, to run once:
- pinMode(sensor_HC_SR04_Echo_PIN_D3, INPUT);
- pinMode(sensor_HC_SR04_Trigger_PIN_D2, OUTPUT);
- Serial.begin(9600); // Initialize serial communication
- // Print the header of the distance data
- Serial.println("Distance Measurement");
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- updateOutputs(); // Refresh output data
- printDistance(); // Print the distance to the serial monitor
- delay(1000); // Delay for 1 second
- }
- void updateOutputs()
- {
- digitalWrite(sensor_HC_SR04_Trigger_PIN_D2, sensor_HC_SR04_Trigger_PIN_D2_rawData);
- }
- void printDistance()
- {
- unsigned int distance = ultrasonic.read();
- Serial.print("Distance: ");
- Serial.print(distance);
- Serial.println(" cm");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement