Advertisement
pleasedontcode

**Servo Control** rev_01

Nov 12th, 2024
61
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 Uno
  14.     - Source Code created on: 2024-11-12 19:23:38
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The project aims to develop an Arduino-based */
  21.     /* system that utilizes connected components for */
  22.     /* automated tasks, ensuring efficient resource */
  23.     /* management and seamless integration of libraries */
  24.     /* for enhanced functionality. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <Wire.h>   // Include Wire library for I2C communication
  31. #include <SPI.h>    // Include SPI library for SPI communication
  32. #include <Servo.h>  // Include Servo library to control servo motors
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37.  
  38. /****** GLOBAL VARIABLES *****/
  39. Servo myServo; // Create a Servo object
  40.  
  41. void setup(void)
  42. {
  43.     // Initialize I2C communication
  44.     Wire.begin();
  45.  
  46.     // Initialize SPI communication
  47.     SPI.begin();
  48.  
  49.     // Attach the servo to pin 9
  50.     myServo.attach(9);
  51.  
  52.     // Set initial position of the servo
  53.     myServo.write(90); // Move servo to the middle position
  54. }
  55.  
  56. void loop(void)
  57. {
  58.     // Example of controlling the servo
  59.     for (int pos = 0; pos <= 180; pos += 1) // Sweep from 0 to 180 degrees
  60.     {
  61.         myServo.write(pos); // Move servo to position
  62.         delay(15); // Wait 15 milliseconds for the servo to reach the position
  63.     }
  64.     for (int pos = 180; pos >= 0; pos -= 1) // Sweep from 180 to 0 degrees
  65.     {
  66.         myServo.write(pos); // Move servo to position
  67.         delay(15); // Wait 15 milliseconds for the servo to reach the position
  68.     }
  69. }
  70.  
  71. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement