Advertisement
pleasedontcode

Servo Control rev_02

May 9th, 2024
956
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 Mega
  14.     - Source Code created on: 2024-05-09 21:14:48
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* read pot and manage angle of the servo. */
  21. /****** SYSTEM REQUIREMENT 2 *****/
  22.     /* put servo to 180° if pot is at higher value (1024) */
  23.     /* otherwise 0° if pot is at lower value (0). */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <Servo.h> // Include the Servo library
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32. void updateOutputs(void); // Function prototype for updating outputs
  33.  
  34. /***** DEFINITION OF ANALOG INPUT PINS *****/
  35. const uint8_t pot_Potentiometer_Vout_PIN_A0 = A0;
  36.  
  37. /***** DEFINITION OF PWM OUTPUT PINS *****/
  38. const uint8_t servo_Servomotor_PWMSignal_PIN_D2 = 2;
  39.  
  40. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  41. /***** used to store raw data *****/
  42. uint8_t servo_Servomotor_PWMSignal_PIN_D2_rawData = 0;
  43.  
  44. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  45. /***** used to store data after characteristic curve transformation *****/
  46. float servo_Servomotor_PWMSignal_PIN_D2_phyData = 0.0;
  47.  
  48. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  49. Servo myservo; // Instantiate the Servo object
  50.  
  51. void setup(void)
  52. {
  53.   // put your setup code here, to run once:
  54.   pinMode(pot_Potentiometer_Vout_PIN_A0, INPUT);
  55.   pinMode(servo_Servomotor_PWMSignal_PIN_D2, OUTPUT);
  56.  
  57.   myservo.attach(servo_Servomotor_PWMSignal_PIN_D2); // Attach the servo object to the PWM pin
  58. }
  59.  
  60. void loop(void)
  61. {
  62.   // put your main code here, to run repeatedly:
  63.   // Read potentiometer value and manage angle of the servo
  64.   int potValue = analogRead(pot_Potentiometer_Vout_PIN_A0);
  65.   int angle = map(potValue, 0, 1023, 0, 180); // Map potentiometer value to servo angle
  66.   myservo.write(angle); // Set the servo angle
  67.  
  68.   // Put servo to 180° if pot is at higher value (1024) otherwise 0° if pot is at lower value (0)
  69.   if (potValue >= 1024) {
  70.     myservo.write(180);
  71.   } else if (potValue <= 0) {
  72.     myservo.write(0);
  73.   }
  74. }
  75.  
  76. void updateOutputs(void)
  77. {
  78.   analogWrite(servo_Servomotor_PWMSignal_PIN_D2, servo_Servomotor_PWMSignal_PIN_D2_rawData);
  79. }
  80.  
  81. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement