Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Servo Control
- - Source Code NOT compiled for: Arduino Mega
- - Source Code created on: 2024-05-09 21:14:48
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* read pot and manage angle of the servo. */
- /****** SYSTEM REQUIREMENT 2 *****/
- /* put servo to 180° if pot is at higher value (1024) */
- /* otherwise 0° if pot is at lower value (0). */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <Servo.h> // Include the Servo library
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void updateOutputs(void); // Function prototype for updating outputs
- /***** DEFINITION OF ANALOG INPUT PINS *****/
- const uint8_t pot_Potentiometer_Vout_PIN_A0 = A0;
- /***** DEFINITION OF PWM OUTPUT PINS *****/
- const uint8_t servo_Servomotor_PWMSignal_PIN_D2 = 2;
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- /***** used to store raw data *****/
- uint8_t servo_Servomotor_PWMSignal_PIN_D2_rawData = 0;
- /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
- /***** used to store data after characteristic curve transformation *****/
- float servo_Servomotor_PWMSignal_PIN_D2_phyData = 0.0;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- Servo myservo; // Instantiate the Servo object
- void setup(void)
- {
- // put your setup code here, to run once:
- pinMode(pot_Potentiometer_Vout_PIN_A0, INPUT);
- pinMode(servo_Servomotor_PWMSignal_PIN_D2, OUTPUT);
- myservo.attach(servo_Servomotor_PWMSignal_PIN_D2); // Attach the servo object to the PWM pin
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- // Read potentiometer value and manage angle of the servo
- int potValue = analogRead(pot_Potentiometer_Vout_PIN_A0);
- int angle = map(potValue, 0, 1023, 0, 180); // Map potentiometer value to servo angle
- myservo.write(angle); // Set the servo angle
- // Put servo to 180° if pot is at higher value (1024) otherwise 0° if pot is at lower value (0)
- if (potValue >= 1024) {
- myservo.write(180);
- } else if (potValue <= 0) {
- myservo.write(0);
- }
- }
- void updateOutputs(void)
- {
- analogWrite(servo_Servomotor_PWMSignal_PIN_D2, servo_Servomotor_PWMSignal_PIN_D2_rawData);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement