Advertisement
pleasedontcode

"Servo Control" rev_01

Nov 23rd, 2024
171
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-23 16:01:18
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Servo Motors  move when potentiometer degrees are */
  21.     /* changed. */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <Servo.h>
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32.  
  33. // Define the number of servos and potentiometers
  34. #define NUM_SERVOS 4
  35.  
  36. // Define arrays for potentiometer pins and servo objects
  37. int potPins[NUM_SERVOS] = {A0, A1, A2, A3}; // Potentiometer pins
  38. Servo myServos[NUM_SERVOS]; // Servo objects
  39. int vals[NUM_SERVOS]; // Store potentiometer values
  40.  
  41. void setup(void)
  42. {
  43.     // Start serial communication for debugging
  44.     Serial.begin(9600);
  45.  
  46.     // Attach servos to digital pins 6, 7, 8, and 9
  47.     for (int i = 0; i < NUM_SERVOS; i++) {
  48.         myServos[i].attach(6 + i); // Attach servos to pins 6, 7, 8, 9
  49.     }
  50. }
  51.  
  52. void loop(void)
  53. {
  54.     // Read potentiometer values and map them to servo angles
  55.     for (int i = 0; i < NUM_SERVOS; i++) {
  56.         vals[i] = analogRead(potPins[i]); // Read potentiometer value
  57.         vals[i] = map(vals[i], 0, 1023, 0, 180); // Map to servo angle (0-180)
  58.         myServos[i].write(vals[i]); // Write the mapped value to the servo
  59.  
  60.         // Print the current servo value to the Serial Monitor
  61.         Serial.print("Servo ");
  62.         Serial.print(i);
  63.         Serial.print(": ");
  64.         Serial.println(vals[i]);
  65.        
  66.         // Short delay for stability
  67.         delay(10);
  68.     }
  69. }
  70.  
  71. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement