Advertisement
pleasedontcode

Control Servos rev_03

May 5th, 2024
853
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: Control Servos
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-05-05 18:06:08
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* a system of two servo, that will close the road */
  21.     /* when the button is pushed 1st time and open when */
  22.     /* pushed 2nd time. */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <Servo.h> // https://github.com/arduino-libraries/Servo
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31. void updateOutputs(void);
  32. void toggleServoPosition(Servo &servo);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t PushButton_PIN_D2 = 2;
  36.  
  37. /***** DEFINITION OF PWM OUTPUT PINS *****/
  38. const uint8_t Servomotor_PWMSignal_PIN_D3 = 3;
  39. const uint8_t Servomotor_PWMSignal_PIN_D5 = 5;
  40.  
  41. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  42. /***** used to store raw data *****/
  43. uint8_t Servomotor_PWMSignal_PIN_D3_rawData = 0;
  44. uint8_t Servomotor_PWMSignal_PIN_D5_rawData = 0;
  45.  
  46. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  47. /***** used to store data after characteristic curve transformation *****/
  48. float Servomotor_PWMSignal_PIN_D3_phyData = 0.0;
  49. float Servomotor_PWMSignal_PIN_D5_phyData = 0.0;
  50.  
  51. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  52. Servo servo1; // Servo object for Servo motor 1
  53. Servo servo2; // Servo object for Servo motor 2
  54.  
  55. bool isPushButtonPressed = false;
  56. int pushButtonState = 0;
  57.  
  58. void setup(void)
  59. {
  60.   // put your setup code here, to run once:
  61.   pinMode(PushButton_PIN_D2, INPUT_PULLUP);
  62.  
  63.   pinMode(Servomotor_PWMSignal_PIN_D3, OUTPUT);
  64.   pinMode(Servomotor_PWMSignal_PIN_D5, OUTPUT);
  65.  
  66.   servo1.attach(Servomotor_PWMSignal_PIN_D3); // Attach servo1 to PWM pin 3
  67.   servo2.attach(Servomotor_PWMSignal_PIN_D5); // Attach servo2 to PWM pin 5
  68. }
  69.  
  70. void loop(void)
  71. {
  72.   // put your main code here, to run repeatedly:
  73.   pushButtonState = digitalRead(PushButton_PIN_D2);
  74.  
  75.   if (pushButtonState == LOW && !isPushButtonPressed) {
  76.     toggleServoPosition(servo1);
  77.     isPushButtonPressed = true;
  78.   } else if (pushButtonState == LOW && isPushButtonPressed) {
  79.     toggleServoPosition(servo2);
  80.     isPushButtonPressed = false;
  81.   }
  82. }
  83.  
  84. void updateOutputs()
  85. {
  86.   servo1.write(Servomotor_PWMSignal_PIN_D3_rawData);
  87.   servo2.write(Servomotor_PWMSignal_PIN_D5_rawData);
  88. }
  89.  
  90. void toggleServoPosition(Servo &servo)
  91. {
  92.   int pos = servo.read();
  93.   if (pos == 0) {
  94.     servo.write(180);
  95.   } else {
  96.     servo.write(0);
  97.   }
  98. }
  99.  
  100. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement