Advertisement
pleasedontcode

"Ultrasonic LED Control" rev_04

Jan 22nd, 2024
70
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 LED Control"
  13.     - Source Code compiled for: Arduino Nano
  14.     - Source Code created on: 2024-01-22 14:50:30
  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. /****** SYSTEM REQUIREMENT 2 *****/
  27.     /* 500 millisecond distance update */
  28. /****** END SYSTEM REQUIREMENTS *****/
  29.  
  30. /****** DEFINITION OF LIBRARIES *****/
  31. #include <Arduino.h>
  32. #include <Ultrasonic.h>
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37.  
  38. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  39. const uint8_t distance_HC_SR04_Echo_PIN_D5 = 5;
  40.  
  41. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  42. const uint8_t svitlo_LED_PIN_D3 = 3;
  43. const uint8_t distance_HC_SR04_Trigger_PIN_D4 = 4;
  44.  
  45. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  46. Ultrasonic ultrasonic(distance_HC_SR04_Trigger_PIN_D4, distance_HC_SR04_Echo_PIN_D5);
  47.  
  48. void setup(void)
  49. {
  50.   // put your setup code here, to run once:
  51.   pinMode(distance_HC_SR04_Echo_PIN_D5, INPUT);
  52.   pinMode(svitlo_LED_PIN_D3, OUTPUT);
  53.   pinMode(distance_HC_SR04_Trigger_PIN_D4, OUTPUT);
  54. }
  55.  
  56. void loop(void)
  57. {
  58.   // put your main code here, to run repeatedly:
  59.   unsigned int distance = ultrasonic.read(CM); // Read the distance in centimeters
  60.  
  61.   // System Requirement 1
  62.   if (distance >= 20)
  63.   {
  64.     digitalWrite(svitlo_LED_PIN_D3, LOW); // Turn off all LEDs
  65.   }
  66.   else if (distance >= 15)
  67.   {
  68.     digitalWrite(svitlo_LED_PIN_D3, HIGH); // Turn on 1 LED
  69.   }
  70.   else if (distance >= 10)
  71.   {
  72.     digitalWrite(svitlo_LED_PIN_D3, HIGH); // Turn on 2 LEDs
  73.   }
  74.   else if (distance >= 5)
  75.   {
  76.     digitalWrite(svitlo_LED_PIN_D3, HIGH); // Turn on 3 LEDs
  77.   }
  78.   else if (distance >= 2)
  79.   {
  80.     digitalWrite(svitlo_LED_PIN_D3, HIGH); // Turn on 4 LEDs
  81.   }
  82.   else
  83.   {
  84.     digitalWrite(svitlo_LED_PIN_D3, HIGH); // Turn on all 5 LEDs
  85.   }
  86.  
  87.   // System Requirement 2
  88.   delay(500); // Wait for 500 milliseconds before updating the distance
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement