Advertisement
pleasedontcode

Button Control rev_01

Mar 30th, 2024
76
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: Button Control
  13.     - Source Code compiled for: Arduino Nano
  14.     - Source Code created on: 2024-03-30 19:54:51
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* servo go 90 degres if button is presed? led on */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /****** DEFINITION OF LIBRARIES *****/
  24. #include <Servo.h>   // Servo library for controlling servo motors
  25. #include <EasyButton.h>   // EasyButton library for handling button inputs
  26.  
  27. /****** FUNCTION PROTOTYPES *****/
  28. void setup(void);
  29. void loop(void);
  30. void onPressed();
  31.  
  32. /****** SYSTEM REQUIREMENTS *****/
  33. /****** SYSTEM REQUIREMENT 1 *****/
  34.     /* servo go 90 degrees if button is pressed? led on */
  35.  
  36. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  37. const uint8_t button_PIN_D2 = 2;
  38.  
  39. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  40. const uint8_t led_PIN_D4 = 4;
  41.  
  42. /***** DEFINITION OF PWM OUTPUT PINS *****/
  43. const uint8_t servo_PIN_D3 = 3;
  44.  
  45. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  46. bool led_rawData = false;
  47. uint8_t servo_rawData = 0;
  48.  
  49. /***** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  50. Servo myservo;  // Servo object for controlling the servo motor
  51. EasyButton button(button_PIN_D2); // EasyButton object for handling button input
  52.  
  53. void setup() {
  54.   pinMode(led_PIN_D4, OUTPUT);  // Set LED pin as output
  55.   myservo.attach(servo_PIN_D3);  // Attach servo motor to PWM pin
  56.   button.begin();  // Initialize the button object
  57.   button.onPressed(onPressed);  // Set onPressed function as callback for button press event
  58. }
  59.  
  60. void loop() {
  61.   button.read();  // Read the button state
  62.  
  63.   if (button.isPressed()) {
  64.     led_rawData = true;
  65.     myservo.write(90);  // Set servo angle to 90 degrees
  66.   } else {
  67.     led_rawData = false;
  68.   }
  69.  
  70.   digitalWrite(led_PIN_D4, led_rawData);  // Set LED state based on led_rawData value
  71. }
  72.  
  73. void onPressed() {
  74.   // Button pressed event
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement