Advertisement
pleasedontcode

Ultrasonic Control rev_03

Jan 22nd, 2024
73
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 Control
  13.     - Source Code compiled for: Arduino Nano
  14.     - Source Code created on: 2024-01-22 14:32:19
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* if the distance to the detector is 20 cm, 1 LED */
  21.     /* lights up, if the distance is 15 cm, 2 LEDs light */
  22.     /* up, if the distance is 10 cm, 3 LEDs light up, if */
  23.     /* the distance is 5 cm, 4 LEDs light up, if the */
  24.     /* distance is 2 cm, all 5 LEDs light up and the */
  25.     /* piezo sque */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <Arduino.h>
  30. #include <Ultrasonic.h>
  31.  
  32. /****** SYSTEM REQUIREMENTS *****/
  33. /****** SYSTEM REQUIREMENT 1 *****/
  34. /* if the distance to the detector is 20 cm, 1 LED */
  35. /* lights up, if the distance is 15 cm, 2 LEDs light */
  36. /* up, if the distance is 10 cm, 3 LEDs light up, if */
  37. /* the distance is 5 cm, 4 LEDs light up, if the */
  38. /* distance is 2 cm, all 5 LEDs light up and the */
  39. /* piezo sque */
  40.  
  41. /****** FUNCTION PROTOTYPES *****/
  42. void setup(void);
  43. void loop(void);
  44. void lightUpLED(int ledCount);
  45. void turnOffLEDs();
  46.  
  47. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  48. const uint8_t distance_HC_SR04_Echo_PIN = 5;
  49.  
  50. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  51. const uint8_t svitlo_LED_PIN_D3 = 3;
  52. const uint8_t distance_HC_SR04_Trigger_PIN_D4 = 4;
  53.  
  54. /***** CONSTANTS FOR DISTANCE RANGES *****/
  55. const int DISTANCE_RANGE_1 = 20; // Distance range for 1 LED
  56. const int DISTANCE_RANGE_2 = 15; // Distance range for 2 LEDs
  57. const int DISTANCE_RANGE_3 = 10; // Distance range for 3 LEDs
  58. const int DISTANCE_RANGE_4 = 5;  // Distance range for 4 LEDs
  59. const int DISTANCE_RANGE_5 = 2;  // Distance range for all 5 LEDs
  60.  
  61. /****** DEFINITION OF LIBRARY CLASS INSTANCES*****/
  62. Ultrasonic ultrasonic(distance_HC_SR04_Trigger_PIN_D4, distance_HC_SR04_Echo_PIN);
  63.  
  64. void setup(void)
  65. {
  66.     // put your setup code here, to run once:
  67.     pinMode(distance_HC_SR04_Echo_PIN, INPUT);
  68.     pinMode(svitlo_LED_PIN_D3, OUTPUT);
  69.     pinMode(distance_HC_SR04_Trigger_PIN_D4, OUTPUT);
  70. }
  71.  
  72. void loop(void)
  73. {
  74.     // put your main code here, to run repeatedly:
  75.     int distance = ultrasonic.read();
  76.  
  77.     if (distance >= DISTANCE_RANGE_1)
  78.     {
  79.         lightUpLED(1);
  80.     }
  81.     else if (distance >= DISTANCE_RANGE_2)
  82.     {
  83.         lightUpLED(2);
  84.         // Add code to light up the second LED
  85.     }
  86.     else if (distance >= DISTANCE_RANGE_3)
  87.     {
  88.         lightUpLED(3);
  89.         // Add code to light up the third LED
  90.     }
  91.     else if (distance >= DISTANCE_RANGE_4)
  92.     {
  93.         lightUpLED(4);
  94.         // Add code to light up the fourth LED
  95.     }
  96.     else if (distance >= DISTANCE_RANGE_5)
  97.     {
  98.         lightUpLED(5);
  99.         // Add code to light up all five LEDs
  100.         // Add code for the piezo sound
  101.     }
  102.     else
  103.     {
  104.         turnOffLEDs();
  105.         // Add code to turn off the piezo sound
  106.     }
  107. }
  108.  
  109. void lightUpLED(int ledCount)
  110. {
  111.     digitalWrite(svitlo_LED_PIN_D3, HIGH);
  112.     // Add code to light up the specified number of LEDs
  113. }
  114.  
  115. void turnOffLEDs()
  116. {
  117.     digitalWrite(svitlo_LED_PIN_D3, LOW);
  118.     // Add code to turn off all LEDs
  119. }
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement