Advertisement
pleasedontcode

Sensor Control rev_01

Sep 30th, 2024
55
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: Sensor Control
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-09-30 17:18:14
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The application must implement the Ultrasonic */
  21.     /* sensor for real-time distance detection and */
  22.     /* utilize EasyButton for user-triggered actions, */
  23.     /* enhancing interaction and responsiveness in the */
  24.     /* CROSSING project environment. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <EasyButton.h>  // https://github.com/evert-arias/EasyButton
  29. #include <Ultrasonic.h>  // https://github.com/ErickSimoes/Ultrasonic
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void updateOutputs(void);
  35. void buttonPressedTwoSeconds(void);
  36. void buttonISR(void);
  37.  
  38. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  39. const uint8_t OD_HC_SR04_Echo_PIN_D3 = 3;
  40.  
  41. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  42. const uint8_t OD_HC_SR04_Trigger_PIN_D2 = 2;
  43.  
  44. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  45. /***** used to store raw data *****/
  46. bool OD_HC_SR04_Trigger_PIN_D2_rawData = 0;
  47.  
  48. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  49. /***** used to store data after characteristic curve transformation *****/
  50. float OD_HC_SR04_Trigger_PIN_D2_phyData = 0.0;
  51.  
  52. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  53. // Initialize EasyButton object with the button pin
  54. EasyButton button(2); // Assuming button is connected to pin 2
  55.  
  56. // Initialize Ultrasonic object with the trigger and echo pins
  57. Ultrasonic ultrasonic(OD_HC_SR04_Trigger_PIN_D2, OD_HC_SR04_Echo_PIN_D3); // Trigger pin, Echo pin
  58.  
  59. void setup(void)
  60. {
  61.     // Set up the ultrasonic sensor pins
  62.     pinMode(OD_HC_SR04_Echo_PIN_D3, INPUT);
  63.     pinMode(OD_HC_SR04_Trigger_PIN_D2, OUTPUT);
  64.    
  65.     // Initialize serial communication for debugging
  66.     Serial.begin(115200);
  67.     Serial.println();
  68.     Serial.println(">>> Ultrasonic and EasyButton Example <<<");
  69.    
  70.     // Initialize the button
  71.     button.begin();
  72.     button.onPressedFor(2000, buttonPressedTwoSeconds); // Set action for button press duration
  73.  
  74.     if (button.supportsInterrupt()) {
  75.         button.enableInterrupt(buttonISR); // Enable interrupt for button
  76.         Serial.println("Button will be used through interrupts");
  77.     }
  78. }
  79.  
  80. void loop(void)
  81. {
  82.     // Refresh output data
  83.     updateOutputs();
  84.     button.update(); // Update button state
  85.  
  86.     // Read distance from ultrasonic sensor and print it
  87.     int distance = ultrasonic.read(); // Get distance in cm
  88.     Serial.print("Distance in CM: ");
  89.     Serial.println(distance);
  90. }
  91.  
  92. void updateOutputs()
  93. {
  94.     digitalWrite(OD_HC_SR04_Trigger_PIN_D2, OD_HC_SR04_Trigger_PIN_D2_rawData);
  95. }
  96.  
  97. // Function called when the button is pressed for two seconds
  98. void buttonPressedTwoSeconds(void) {
  99.     Serial.println("Button pressed for two seconds");
  100. }
  101.  
  102. // Interrupt service routine for button
  103. void buttonISR(void) {
  104.     button.read(); // Read the button state
  105. }
  106.  
  107. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement