Advertisement
pleasedontcode

**Servo Control** rev_02

Dec 14th, 2024
51
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 NOT compiled for: Arduino Pro Mini 3.3V
  14.     - Source Code created on: 2024-12-14 06:18:12
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The system shall initialize all connected */
  21.     /* components during setup and continuously monitor */
  22.     /* their status in the loop function, ensuring */
  23.     /* efficient resource management and responsiveness. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /* START CODE */
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <Wire.h>
  30. #include <SPI.h>
  31. #include <Servo.h>
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36.  
  37. // Instantiate library objects
  38. Servo myServo; // Create a Servo object
  39.  
  40. void setup(void)
  41. {
  42.     // Initialize the I2C communication
  43.     Wire.begin();
  44.    
  45.     // Initialize SPI communication
  46.     SPI.begin();
  47.    
  48.     // Attach the servo to pin 9
  49.     myServo.attach(9);
  50.    
  51.     // Set initial position of the servo
  52.     myServo.write(90); // Set to middle position (90 degrees)
  53. }
  54.  
  55. void loop(void)
  56. {
  57.     // Continuously monitor the status of the components
  58.     // For example, read from a sensor or update the servo position
  59.     // Here we can add code to read sensor values or control the servo based on conditions
  60.  
  61.     // Example: Move the servo back and forth
  62.     for (int pos = 0; pos <= 180; pos += 1) { // Sweep from 0 to 180 degrees
  63.         myServo.write(pos);              // Tell servo to go to position in variable 'pos'
  64.         delay(15);                       // Wait for the servo to reach the position
  65.     }
  66.     for (int pos = 180; pos >= 0; pos -= 1) { // Sweep from 180 to 0 degrees
  67.         myServo.write(pos);              // Tell servo to go to position in variable 'pos'
  68.         delay(15);                       // Wait for the servo to reach the position
  69.     }
  70. }
  71.  
  72. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement