Advertisement
pleasedontcode

"Switch Servo" rev_01

Jan 22nd, 2024
81
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: "Switch Servo"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-01-22 07:39:19
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* when turn on switch rotate and lock and when off */
  21.     /* the switch rotate and unlock */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <Arduino.h>
  26. #include <Servo.h>
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31.  
  32. /***** DEFINITION OF PWM OUTPUT PINS *****/
  33. const uint8_t servo_Servomotor_PWMSignal_PIN_D3 = 3;
  34. const uint8_t servo_Servomotor_PWMSignal_PIN_D5 = 5;
  35.  
  36. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  37. Servo servo_Servomotor_D3;
  38. Servo servo_Servomotor_D5;
  39.  
  40. const uint8_t switch_PIN = 2; // Define the switch input pin here
  41.  
  42. void setup(void)
  43. {
  44.   // put your setup code here, to run once:
  45.   pinMode(switch_PIN, INPUT_PULLUP); // Set the switch pin as input with internal pull-up resistor
  46.   servo_Servomotor_D3.attach(servo_Servomotor_PWMSignal_PIN_D3);
  47.   servo_Servomotor_D5.attach(servo_Servomotor_PWMSignal_PIN_D5);
  48.  
  49.   // Set initial positions
  50.   servo_Servomotor_D3.write(0);
  51.   servo_Servomotor_D5.write(0);
  52. }
  53.  
  54. void loop(void)
  55. {
  56.   // put your main code here, to run repeatedly:
  57.  
  58.   // Check the switch state
  59.   bool switchState = digitalRead(switch_PIN);
  60.  
  61.   if (switchState == HIGH)
  62.   {
  63.     // Switch is turned on, rotate and lock servos
  64.     servo_Servomotor_D3.write(90);
  65.     servo_Servomotor_D5.write(90);
  66.   }
  67.   else
  68.   {
  69.     // Switch is turned off, rotate and unlock servos
  70.     servo_Servomotor_D3.write(0);
  71.     servo_Servomotor_D5.write(0);
  72.   }
  73.  
  74.   delay(100); // Adjust the delay time as needed
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement