Advertisement
pleasedontcode

Servo Control rev_03

May 9th, 2024
830
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:17:16
  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; // Initialize the Servo object
  56.  
  57. void setup(void)
  58. {
  59.   // put your setup code here, to run once:
  60.   myServo.attach(servo_Servomotor_PWMSignal_PIN_D2); // Attach servo to PWM pin
  61.  
  62.   pinMode(pot_Potentiometer_Vout_PIN_A0, INPUT);
  63.   pinMode(relay_RelayModule_Signal_PIN_D3, OUTPUT);
  64. }
  65.  
  66. void loop(void)
  67. {
  68.   // put your main code here, to run repeatedly:
  69.   int potValue = analogRead(pot_Potentiometer_Vout_PIN_A0);
  70.   int servoAngle = map(potValue, 0, 1023, 0, 180); // Map pot value to servo angle
  71.  
  72.   myServo.write(servoAngle); // Set servo angle based on pot value
  73.  
  74.   if (potValue >= 512) {
  75.     myServo.write(180); // Put servo to 180° if pot is at higher value (1024)
  76.   } else {
  77.     myServo.write(0); // Put servo to 0° if pot is at lower value (0)
  78.   }
  79.  
  80.   delay(15);
  81. }
  82.  
  83. void updateOutputs(void)
  84. {
  85.   digitalWrite(relay_RelayModule_Signal_PIN_D3, relay_RelayModule_Signal_PIN_D3_rawData);
  86.   analogWrite(servo_Servomotor_PWMSignal_PIN_D2, servo_Servomotor_PWMSignal_PIN_D2_rawData);
  87. }
  88.  
  89. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement