Advertisement
pleasedontcode

Servo Control rev_07

May 9th, 2024
912
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 23:26:32
  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>  // https://github.com/arduino-libraries/Servo
  28. #include <Relay.h>  // https://github.com/rafaelnsantos/Relay
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33. void updateOutputs(void);
  34.  
  35. /***** DEFINITION OF ANALOG INPUT PINS *****/
  36. const uint8_t pot_Potentiometer_Vout_PIN_A0      = A0;
  37.  
  38. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  39. const uint8_t relay_RelayModule_Signal_PIN_D3     = 3;
  40.  
  41. /***** DEFINITION OF PWM OUTPUT PINS *****/
  42. const uint8_t servo_Servomotor_PWMSignal_PIN_D2   = 2;
  43.  
  44. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  45. /***** used to store raw data *****/
  46. bool    relay_RelayModule_Signal_PIN_D3_rawData   = 0;
  47. uint8_t servo_Servomotor_PWMSignal_PIN_D2_rawData = 0;
  48.  
  49. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  50. /***** used to store data after characteristic curve transformation *****/
  51. float   relay_RelayModule_Signal_PIN_D3_phyData   = 0.0;
  52. float   servo_Servomotor_PWMSignal_PIN_D2_phyData = 0.0;
  53.  
  54. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  55. Servo myservo; // Creating an instance of the Servo class
  56.  
  57. void setup(void)
  58. {
  59.     // put your setup code here, to run once:
  60.     myservo.attach(servo_Servomotor_PWMSignal_PIN_D2); // Attaching the servo to the specified pin
  61.  
  62.     pinMode(pot_Potentiometer_Vout_PIN_A0,   INPUT);
  63.     pinMode(relay_RelayModule_Signal_PIN_D3, OUTPUT);
  64.     pinMode(servo_Servomotor_PWMSignal_PIN_D2, OUTPUT);
  65. }
  66.  
  67. void loop(void)
  68. {
  69.     // put your main code here, to run repeatedly:
  70.     int potValue = analogRead(pot_Potentiometer_Vout_PIN_A0); // Read potentiometer value
  71.     int servoAngle = map(potValue, 0, 1024, 0, 180); // Map potentiometer value to servo angle
  72.  
  73.     myservo.write(servoAngle); // Set servo angle based on potentiometer value
  74.  
  75.     if (potValue >= 512) {
  76.         myservo.write(180); // Put servo to 180° if pot is at higher value (1024)
  77.     } else {
  78.         myservo.write(0);   // Put servo to 0° if pot is at lower value (0)
  79.     }
  80.  
  81.     delay(15); // Delay for smooth servo movement
  82. }
  83.  
  84. void updateOutputs(void)
  85. {
  86.     digitalWrite(relay_RelayModule_Signal_PIN_D3, relay_RelayModule_Signal_PIN_D3_rawData);
  87.     analogWrite(servo_Servomotor_PWMSignal_PIN_D2, servo_Servomotor_PWMSignal_PIN_D2_rawData);
  88. }
  89.  
  90. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement