Advertisement
pleasedontcode

Potentiometer Control rev_08

May 9th, 2024
1,144
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: Potentiometer Control
  13.     - Source Code NOT compiled for: Arduino Mega
  14.     - Source Code created on: 2024-05-09 23:29:47
  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. /****** DEFINITION OF LIBRARIES *****/
  27. #include <Servo.h>  //https://github.com/arduino-libraries/Servo
  28. #include <Relay.h>  //https://github.com/rafaelnsantos/Relay
  29. #include <XYZrobotServo.h> // Include the XYZrobotServo library
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void updateOutputs(void);
  35.  
  36. /***** DEFINITION OF ANALOG INPUT PINS *****/
  37. const uint8_t pot_Potentiometer_Vout_PIN_A0 = A0;
  38.  
  39. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  40. const uint8_t relay_RelayModule_Signal_PIN_D3 = 3;
  41.  
  42. /***** DEFINITION OF PWM OUTPUT PINS *****/
  43. const uint8_t servo_Servomotor_PWMSignal_PIN_D2 = 2;
  44.  
  45. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  46. /***** used to store raw data *****/
  47. bool relay_RelayModule_Signal_PIN_D3_rawData = 0;
  48. uint8_t servo_Servomotor_PWMSignal_PIN_D2_rawData = 0;
  49.  
  50. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  51. /***** used to store data after characteristic curve transformation *****/
  52. float relay_RelayModule_Signal_PIN_D3_phyData = 0.0;
  53. float servo_Servomotor_PWMSignal_PIN_D2_phyData = 0.0;
  54.  
  55. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  56. Servo myservo; // Instantiate the Servo class object
  57. XYZrobotServo xyzServo(Serial1, 1); // Instantiate the XYZrobotServo class object with Serial1 and ID 1
  58.  
  59. void setup(void)
  60. {
  61.     // put your setup code here, to run once:
  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);
  71.     if (potValue >= 512) {
  72.         myservo.write(180); // Set servo to 180° if potentiometer value is higher or equal to 512
  73.         relay_RelayModule_Signal_PIN_D3_rawData = 1; // Activate relay
  74.     } else {
  75.         myservo.write(0); // Set servo to 0° if potentiometer value is lower than 512
  76.         relay_RelayModule_Signal_PIN_D3_rawData = 0; // Deactivate relay
  77.     }
  78.  
  79.     updateOutputs(); // Refresh output data
  80. }
  81.  
  82. void updateOutputs(void)
  83. {
  84.     digitalWrite(relay_RelayModule_Signal_PIN_D3, relay_RelayModule_Signal_PIN_D3_rawData);
  85.     servo_Servomotor_PWMSignal_PIN_D2_rawData = map(myservo.read(), 0, 180, 0, 255); // Map servo angle to PWM range
  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