Advertisement
pleasedontcode

"Distance Control" rev_159

Dec 6th, 2023
95
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: "Distance Control"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2023-12-06 23:22:52
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* if object read by ultrasonic sensor is less than */
  21.     /* 20cm, so switch on red led and switch off green */
  22.     /* led. If object is above 100cm so switch on green */
  23.     /* led and switch off red led. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <Arduino.h>
  28. #include <EasyButton.h>
  29. #include <Ultrasonic.h>
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t ultrasonic_sensor_HC_SR04_Echo_PIN_D3 = 3;
  37.  
  38. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  39. const uint8_t VERDE_LED_PIN_D11 = 11;
  40. const uint8_t ROSSO_LED_PIN_D12 = 12;
  41. const uint8_t ultrasonic_sensor_HC_SR04_Trigger_PIN_D2 = 2;
  42.  
  43. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  44. EasyButton button(ultrasonic_sensor_HC_SR04_Echo_PIN_D3);
  45. Ultrasonic ultrasonic(ultrasonic_sensor_HC_SR04_Trigger_PIN_D2, ultrasonic_sensor_HC_SR04_Echo_PIN_D3);
  46.  
  47. void setup(void)
  48. {
  49.   // Initialize the input and output pins
  50.   pinMode(ultrasonic_sensor_HC_SR04_Echo_PIN_D3, INPUT);
  51.   pinMode(VERDE_LED_PIN_D11, OUTPUT);
  52.   pinMode(ROSSO_LED_PIN_D12, OUTPUT);
  53.   pinMode(ultrasonic_sensor_HC_SR04_Trigger_PIN_D2, OUTPUT);
  54.  
  55.   // Initialize the EasyButton instance
  56.   button.begin();
  57. }
  58.  
  59. void loop(void)
  60. {
  61.   // Read the distance measured by the ultrasonic sensor
  62.   unsigned int distance = ultrasonic.read();
  63.  
  64.   // Check if the object is less than 20cm away
  65.   if (distance < 20)
  66.   {
  67.     // Switch on the red LED and switch off the green LED
  68.     digitalWrite(ROSSO_LED_PIN_D12, HIGH);
  69.     digitalWrite(VERDE_LED_PIN_D11, LOW);
  70.   }
  71.   // Check if the object is above 100cm away
  72.   else if (distance > 100)
  73.   {
  74.     // Switch on the green LED and switch off the red LED
  75.     digitalWrite(ROSSO_LED_PIN_D12, LOW);
  76.     digitalWrite(VERDE_LED_PIN_D11, HIGH);
  77.   }
  78.  
  79.   // Put the rest of your main code here
  80.  
  81.   // Update the button state
  82.   button.read();
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement