Advertisement
pleasedontcode

"Sensor-Driven Actions" rev_01

Jun 17th, 2024
418
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-Driven Actions"
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-06-17 10:33:34
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Rotate servo1 to 4 from 0 to 180 degree, when */
  21.     /* pushbutto light led for 200ms, if ir1 to ir4 > 50% */
  22.     /* rotate servo 1 to servo 4 to 0 degree */
  23. /****** SYSTEM REQUIREMENT 2 *****/
  24.     /* Rotate Servo1 from 0 to 180 degrees when */
  25.     /* LaserButton (D4) is pressed. Light up Laser1 LED */
  26.     /* (D16) for 200ms if IR1 (D14) and IR2 (D26) */
  27.     /* readings are above 50%. Then, reset Servo1 to 0 */
  28.     /* degrees. */
  29. /****** END SYSTEM REQUIREMENTS *****/
  30.  
  31. /****** DEFINITION OF LIBRARIES *****/
  32. #include <EasyButton.h>    //https://github.com/evert-arias/EasyButton
  33. #include <Deneyap_Servo.h> //https://github.com/deneyapkart/deneyap-servo-arduino-library
  34.  
  35. /****** FUNCTION PROTOTYPES *****/
  36. void setup(void);
  37. void loop(void);
  38. void onLaserButtonPressed(); // Function prototype for button press callback
  39. void updateOutputs(); // Function prototype for updating outputs
  40. void rotateServo(int start, int end); // Function prototype for rotating servo
  41.  
  42. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  43. const uint8_t LaserButton_PushButton_PIN_D4 = 4;
  44.  
  45. /***** DEFINITION OF ANALOG INPUT PINS *****/
  46. const uint8_t IR1_Potentiometer_Vout_PIN_D14 = 14;
  47. const uint8_t IR2_Potentiometer_Vout_PIN_D26 = 26;
  48.  
  49. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  50. const uint8_t Laser1_LED_PIN_D16 = 16;
  51.  
  52. /***** DEFINITION OF PWM OUTPUT PINS *****/
  53. const uint8_t Servo1_Servomotor_PWMSignal_PIN_D13 = 13;
  54.  
  55. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  56. /***** used to store raw data *****/
  57. bool Laser1_LED_PIN_D16_rawData = 0;
  58. uint8_t Servo1_Servomotor_PWMSignal_PIN_D13_rawData = 0;
  59.  
  60. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  61. /***** used to store data after characteristic curve transformation *****/
  62. float Laser1_LED_PIN_D16_phyData = 0.0;
  63. float Servo1_Servomotor_PWMSignal_PIN_D13_phyData = 0.0;
  64.  
  65. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  66. EasyButton laserButton(LaserButton_PushButton_PIN_D4); // Initialize EasyButton instance
  67. Servo myservo; // Initialize Servo instance
  68.  
  69. void setup(void)
  70. {
  71.     // put your setup code here, to run once:
  72.     Serial.begin(115200);
  73.  
  74.     pinMode(LaserButton_PushButton_PIN_D4, INPUT_PULLUP);
  75.     pinMode(IR1_Potentiometer_Vout_PIN_D14, INPUT);
  76.     pinMode(IR2_Potentiometer_Vout_PIN_D26, INPUT);
  77.  
  78.     pinMode(Laser1_LED_PIN_D16, OUTPUT);
  79.     pinMode(Servo1_Servomotor_PWMSignal_PIN_D13, OUTPUT);
  80.  
  81.     laserButton.begin(); // Initialize the button
  82.     laserButton.onPressed(onLaserButtonPressed); // Set the callback for button press
  83.  
  84.     myservo.attach(Servo1_Servomotor_PWMSignal_PIN_D13); // Attach the servo to pin D13
  85. }
  86.  
  87. void loop(void)
  88. {
  89.     // put your main code here, to run repeatedly:
  90.     laserButton.read(); // Read the button state
  91.     updateOutputs(); // Refresh output data
  92. }
  93.  
  94. void onLaserButtonPressed() {
  95.     Serial.println("Laser button pressed");
  96.  
  97.     // Read IR sensor values
  98.     int ir1Value = analogRead(IR1_Potentiometer_Vout_PIN_D14);
  99.     int ir2Value = analogRead(IR2_Potentiometer_Vout_PIN_D26);
  100.  
  101.     // Check if IR sensor values are above 50%
  102.     if (ir1Value > 2048 && ir2Value > 2048) {
  103.         // Light up Laser1 LED for 200ms
  104.         digitalWrite(Laser1_LED_PIN_D16, HIGH);
  105.         delay(200);
  106.         digitalWrite(Laser1_LED_PIN_D16, LOW);
  107.  
  108.         // Rotate Servo1 from 0 to 180 degrees
  109.         rotateServo(0, 180);
  110.         delay(200); // Wait for 200ms
  111.  
  112.         // Reset Servo1 to 0 degrees
  113.         rotateServo(180, 0);
  114.     }
  115. }
  116.  
  117. void updateOutputs()
  118. {
  119.     digitalWrite(Laser1_LED_PIN_D16, Laser1_LED_PIN_D16_rawData);
  120.     myservo.write(Servo1_Servomotor_PWMSignal_PIN_D13_rawData); // Update servo position
  121. }
  122.  
  123. void rotateServo(int start, int end) {
  124.     if (start < end) {
  125.         for (int pos = start; pos <= end; pos += 15) {
  126.             myservo.write(pos);
  127.             delay(100);
  128.         }
  129.     } else {
  130.         for (int pos = start; pos >= end; pos -= 15) {
  131.             myservo.write(pos);
  132.             delay(100);
  133.         }
  134.     }
  135. }
  136.  
  137. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement