Advertisement
pleasedontcode

Servo Control rev_05

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