Advertisement
pleasedontcode

"Servo Control" rev_04

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