Advertisement
pleasedontcode

Servo Control rev_01

May 28th, 2024
660
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 Uno
  14.     - Source Code created on: 2024-05-28 09:56:13
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* I want to add a pushputton that controls the servo */
  21.     /* motor */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <Servo.h>  //https://github.com/arduino-libraries/Servo
  26.  
  27. /****** FUNCTION PROTOTYPES *****/
  28. void setup(void);
  29. void loop(void);
  30. void updateOutputs(void);
  31.  
  32. /***** DEFINITION OF PWM OUTPUT PINS *****/
  33. const uint8_t mypush_Servomotor_PWMSignal_PIN_D9 = 9;  // Changed to pin 9 as per the example
  34. const uint8_t potpin = A0;  // Potentiometer pin
  35. const uint8_t buttonPin = 2;  // Push button pin
  36.  
  37. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  38. /***** used to store raw data *****/
  39. uint8_t mypush_Servomotor_PWMSignal_PIN_D9_rawData = 0;
  40.  
  41. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  42. /***** used to store data after characteristic curve transformation *****/
  43. float mypush_Servomotor_PWMSignal_PIN_D9_phyData = 0.0;
  44.  
  45. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  46. Servo myservo;  // Create servo object to control a servo
  47.  
  48. void setup(void)
  49. {
  50.   // put your setup code here, to run once:
  51.   myservo.attach(mypush_Servomotor_PWMSignal_PIN_D9);  // Attaches the servo on pin 9 to the servo object
  52.   pinMode(buttonPin, INPUT);  // Set the button pin as an input
  53. }
  54.  
  55. void loop(void)
  56. {
  57.   // put your main code here, to run repeatedly:
  58.   updateOutputs();  // Refresh output data
  59. }
  60.  
  61. void updateOutputs()
  62. {
  63.   static bool buttonPressed = false;  // Variable to store the button state
  64.  
  65.   // Read the button state
  66.   if (digitalRead(buttonPin) == HIGH) {
  67.     buttonPressed = !buttonPressed;  // Toggle the button state
  68.     delay(200);  // Debounce delay
  69.   }
  70.  
  71.   if (buttonPressed) {
  72.     int val = analogRead(potpin);  // Read the value of the potentiometer
  73.     val = map(val, 0, 1023, 0, 180);  // Scale it to use it with the servo (value between 0 and 180)
  74.     myservo.write(val);  // Set the servo position according to the scaled value
  75.   } else {
  76.     myservo.write(90);  // Set the servo to the neutral position (90 degrees) when the button is not pressed
  77.   }
  78.  
  79.   delay(15);  // Wait for the servo to reach the position
  80. }
  81.  
  82. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement