Advertisement
pleasedontcode

Trashcan rev_01

Nov 15th, 2023
91
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: Trashcan
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2023-11-16 05:45:42
  15.     - Source Code generated by: Rebecca
  16.  
  17. ********* Pleasedontcode.com **********/
  18. /****** DEFINITION OF LIBRARIES *****/
  19. #include <Arduino.h>
  20. #include <EasyButton.h>
  21. #include <Ultrasonic.h>
  22.  
  23. /****** SYSTEM REQUIREMENT 1 *****/
  24. /* Generates a message when distance is less than 30 */
  25. /* cm */
  26.  
  27. /****** FUNCTION PROTOTYPES *****/
  28. void setup(void);
  29. void loop(void);
  30.  
  31. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  32. const uint8_t Sensor_PushButton_PIN_D2 = 2;
  33.  
  34. /***** DEFINITION OF DISTANCE SENSOR PINS *****/
  35. const uint8_t Sensor_Ultrasonic_PIN_TRIG = 3;
  36. const uint8_t Sensor_Ultrasonic_PIN_ECHO = 4;
  37.  
  38. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  39. EasyButton button(Sensor_PushButton_PIN_D2);
  40. Ultrasonic ultrasonic(Sensor_Ultrasonic_PIN_TRIG, Sensor_Ultrasonic_PIN_ECHO);
  41.  
  42. void setup(void)
  43. {
  44.   // Initialize the digital input pin for the push button
  45.   pinMode(Sensor_PushButton_PIN_D2, INPUT_PULLUP);
  46.  
  47.   // Initialize the EasyButton object
  48.   button.begin();
  49.  
  50.   // Initialize the Ultrasonic sensor
  51.   ultrasonic.setTimeout(500);
  52. }
  53.  
  54. void loop(void)
  55. {
  56.   // Read the state of the push button
  57.   button.read();
  58.  
  59.   // Check if the button is pressed
  60.   if (button.isPressed()) {
  61.     // Get the distance from the Ultrasonic sensor
  62.     float distance = ultrasonic.read();
  63.    
  64.     // Check if the distance is less than 30 cm
  65.     if (distance < 30) {
  66.       // Generate the message
  67.       Serial.println("Distance is less than 30 cm!");
  68.     }
  69.   }
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement