Advertisement
pleasedontcode

Servo Control rev_09

May 9th, 2024
1,164
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:31:27
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* put servo to 180° if pot is at higher value (1024) */
  21.     /* otherwise 0° if pot is at lower value (0). */
  22. /****** SYSTEM REQUIREMENT 2 *****/
  23.     /* activate relay when servo is 180° */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26.  
  27. /********* User code review feedback **********
  28. #### Feedback 1 ####
  29. - use relay library
  30.  
  31. ********* User code review feedback **********/
  32.  
  33. /****** DEFINITION OF LIBRARIES *****/
  34. #include <Servo.h> // Include the Servo library
  35. #include <Relay.h> // Include the Relay library
  36.  
  37. /****** FUNCTION PROTOTYPES *****/
  38. void setup(void);
  39. void loop(void);
  40. void updateOutputs(void);
  41.  
  42. /***** DEFINITION OF ANALOG INPUT PINS *****/
  43. const uint8_t pot_Potentiometer_Vout_PIN_A0 = A0;
  44.  
  45. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  46. const uint8_t relay_RelayModule_Signal_PIN_D3 = 3;
  47.  
  48. /***** DEFINITION OF PWM OUTPUT PINS *****/
  49. const uint8_t servo_Servomotor_PWMSignal_PIN_D2 = 2;
  50.  
  51. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  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. float relay_RelayModule_Signal_PIN_D3_phyData = 0.0;
  57. float servo_Servomotor_PWMSignal_PIN_D2_phyData = 0.0;
  58.  
  59. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  60. Servo myservo; // Instantiate the Servo class object
  61. Relay light(relay_RelayModule_Signal_PIN_D3, 30); // Instantiate the Relay class object on pin 3 with a period of 30 seconds
  62.  
  63. void setup(void)
  64. {
  65.   // put your setup code here, to run once:
  66.   pinMode(pot_Potentiometer_Vout_PIN_A0, INPUT);
  67.   pinMode(relay_RelayModule_Signal_PIN_D3, OUTPUT);
  68.   pinMode(servo_Servomotor_PWMSignal_PIN_D2, OUTPUT);
  69. }
  70.  
  71. void loop(void)
  72. {
  73.   // put your main code here, to run repeatedly:
  74.   int potValue = analogRead(pot_Potentiometer_Vout_PIN_A0);
  75.   if (potValue >= 512)
  76.   {
  77.     myservo.write(180); // Set servo to 180° if potentiometer value is higher or equal to 512
  78.     light.setRelayPosition(relayPositionClosed); // Activate relay
  79.   }
  80.   else
  81.   {
  82.     myservo.write(0); // Set servo to 0° if potentiometer value is lower than 512
  83.     light.setRelayPosition(relayPositionOpen); // Deactivate relay
  84.   }
  85.  
  86.   updateOutputs(); // Refresh output data
  87. }
  88.  
  89. void updateOutputs(void)
  90. {
  91.   digitalWrite(relay_RelayModule_Signal_PIN_D3, light.getRelayPosition() == relayPositionClosed);
  92.   servo_Servomotor_PWMSignal_PIN_D2_rawData = map(myservo.read(), 0, 180, 0, 255); // Map servo angle to PWM range
  93.   analogWrite(servo_Servomotor_PWMSignal_PIN_D2, servo_Servomotor_PWMSignal_PIN_D2_rawData);
  94. }
  95.  
  96. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement