pleasedontcode

Ultrasonic Detection rev_01

Nov 5th, 2025
553
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 Detection
  13.     - Source Code NOT compiled for: Arduino Nano 33 BLE
  14.     - Source Code created on: 2025-11-05 11:48:22
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Use the Ultrasonic library with HC-SR04 sensor */
  21.     /* connected to digital pins D2 (Trigger) and D3 */
  22.     /* (Echo) on Arduino Nano 33 BLE for obstacle */
  23.     /* detection, allowing system actions based on */
  24.     /* measured distance values. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27.  
  28. /* START CODE */
  29.  
  30. // Project code for obstacle detection using HC-SR04 ultrasonic sensor
  31. // connected to digital pins D2 (Trigger) and D3 (Echo) on Arduino Nano 33 BLE
  32. // System Requirements:
  33. // Use the Ultrasonic library with HC-SR04 sensor at pins D2 and D3
  34. // Detect obstacles and perform system actions based on distance measurements
  35.  
  36. #include <Ultrasonic.h>
  37.  
  38. // Define the ultrasonic sensor pins
  39. const uint8_t ultrasonic_sensor_HC_SR04_Echo_PIN_D3 = 3;
  40. const uint8_t ultrasonic_sensor_HC_SR04_Trigger_PIN_D2 = 2;
  41.  
  42. // Create an instance of Ultrasonic class with echo and trigger pins
  43. Ultrasonic ultrasonicSensor(ultrasonic_sensor_HC_SR04_Trigger_PIN_D2, ultrasonic_sensor_HC_SR04_Echo_PIN_D3);
  44.  
  45. // Threshold distance in centimeters to consider an obstacle
  46. const float obstacleThresholdCm = 20.0; // 20 cm
  47.  
  48. void setup() {
  49.   // Initialize serial communication at 9600 baud
  50.   Serial.begin(9600);
  51.  
  52.   // Configure the pin modes
  53.   pinMode(ultrasonic_sensor_HC_SR04_Echo_PIN_D3, INPUT);
  54.   pinMode(ultrasonic_sensor_HC_SR04_Trigger_PIN_D2, OUTPUT);
  55. }
  56.  
  57. void loop() {
  58.   // Read the distance from the ultrasonic sensor
  59.   float distanceCm = ultrasonicSensor.read();
  60.  
  61.   // Output the distance value
  62.   Serial.print("Distance: ");
  63.   Serial.print(distanceCm);
  64.   Serial.println(" cm");
  65.  
  66.   // Check if an obstacle is detected within the threshold distance
  67.   if (distanceCm > 0 && distanceCm < obstacleThresholdCm) {
  68.     // Obstacle detected - perform system action
  69.     Serial.println("Obstacle detected! Taking action.");
  70.     // Example action: turn on an LED or trigger other system responses
  71.     // digitalWrite(LED_BUILTIN, HIGH); // Uncomment if using an onboard LED
  72.   } else {
  73.     // No obstacle - system is clear
  74.     // digitalWrite(LED_BUILTIN, LOW); // Uncomment if using an onboard LED
  75.   }
  76.  
  77.   // Wait for 200 milliseconds before next reading
  78.   delay(200);
  79. }
  80.  
  81. /* END CODE */
  82.  
Advertisement
Add Comment
Please, Sign In to add comment