Advertisement
pleasedontcode

Servo Control rev_04

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