Advertisement
pleasedontcode

"Servo Control" rev_02

Mar 30th, 2024
89
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: "Servo Control"
  13.     - Source Code compiled for: Arduino Nano
  14.     - Source Code created on: 2024-03-30 19:58:42
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* servo go 90 degres if button is presed? led on */
  21. /****** SYSTEM REQUIREMENT 2 *****/
  22.     /* When the push button is pressed, the servo motor */
  23.     /* should rotate to 90 degrees and the LED should */
  24.     /* turn on.  If button not pressed servo go 180 */
  25.     /* degrees after 2 seconds and led off */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <Servo.h>  // https://github.com/arduino-libraries/Servo
  30. #include <EasyButton.h> // https://github.com/evert-arias/EasyButton
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void onPressed(void); // Function prototype for button press event
  36.  
  37. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  38. const uint8_t button_PushButton_PIN_D2 = 2;
  39.  
  40. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  41. const uint8_t led_LED_PIN_D4 = 4;
  42.  
  43. /***** DEFINITION OF PWM OUTPUT PINS *****/
  44. const uint8_t servo_Servomotor_PWMSignal_PIN_D3 = 3;
  45.  
  46. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  47. /***** used to store raw data *****/
  48. bool led_LED_PIN_D4_rawData = false;
  49. uint8_t servo_Servomotor_PWMSignal_PIN_D3_rawData = 0;
  50.  
  51. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  52. /***** used to store data after characteristic curve transformation *****/
  53. float led_LED_PIN_D4_phyData = 0.0;
  54. float servo_Servomotor_PWMSignal_PIN_D3_phyData = 0.0;
  55.  
  56. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  57. Servo myservo; // Servo object instance
  58. EasyButton button(button_PushButton_PIN_D2); // EasyButton object instance
  59.  
  60. void setup(void)
  61. {
  62.   // put your setup code here, to run once:
  63.   myservo.attach(servo_Servomotor_PWMSignal_PIN_D3); // Attaching servo to the PWM pin
  64.  
  65.   pinMode(button_PushButton_PIN_D2, INPUT_PULLUP);
  66.   pinMode(led_LED_PIN_D4, OUTPUT);
  67.   pinMode(servo_Servomotor_PWMSignal_PIN_D3, OUTPUT);
  68.  
  69.   button.begin(); // Initialize the EasyButton library
  70.   button.onPressed(onPressed); // Set the onPressed callback function for the button press event
  71. }
  72.  
  73. void loop(void)
  74. {
  75.   // put your main code here, to run repeatedly:
  76.   updateOutputs(); // Refresh output data
  77.  
  78.   button.read(); // Read the button state
  79.  
  80.   // Servo control based on button state
  81.   if (button.isPressed()) {
  82.     myservo.write(90); // Rotate servo to 90 degrees
  83.     digitalWrite(led_LED_PIN_D4, HIGH); // Turn on the LED
  84.   } else {
  85.     delay(2000); // Wait for 2 seconds
  86.     myservo.write(180); // Rotate servo to 180 degrees
  87.     digitalWrite(led_LED_PIN_D4, LOW); // Turn off the LED
  88.   }
  89. }
  90.  
  91. void updateOutputs()
  92. {
  93.   digitalWrite(led_LED_PIN_D4, led_LED_PIN_D4_rawData);
  94.   analogWrite(servo_Servomotor_PWMSignal_PIN_D3, servo_Servomotor_PWMSignal_PIN_D3_rawData);
  95. }
  96.  
  97. void onPressed()
  98. {
  99.   // Button press event handler
  100.   led_LED_PIN_D4_rawData = !led_LED_PIN_D4_rawData; // Toggle the LED state
  101. }
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement