Advertisement
pleasedontcode

"Servo Control" rev_02

Jan 12th, 2025
290
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: 2025-01-12 20:23:18
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* esquema elétrico na plataforma Arduino representa */
  21.     /* um sistema que separa peças que se movimentam num */
  22.     /* tapete rolantes a uma cadência de 50 ms entre */
  23.     /* elas.  Pretende-se que o sistema separe */
  24.     /* alternadamente uma peça para esquerda, a seguinte */
  25.     /* para a direia */
  26. /****** SYSTEM REQUIREMENT 2 *****/
  27.     /* acendendo um Led Verde ou Vermelho respetivamente. */
  28. /****** END SYSTEM REQUIREMENTS *****/
  29.  
  30.  
  31. /********* User code review feedback **********
  32. #### Feedback 1 ####
  33. - use delay code
  34.  
  35. ********* User code review feedback **********/
  36.  
  37. /* START CODE */
  38.  
  39. /****** DEFINITION OF LIBRARIES *****/
  40. #include <Servo.h> //https://github.com/arduino-libraries/Servo
  41.  
  42. /****** FUNCTION PROTOTYPES *****/
  43. void setup(void);
  44. void loop(void);
  45. void updateOutputs(void);
  46.  
  47. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  48. const uint8_t LedVerde_LED_PIN_D6 = 6;
  49. const uint8_t LedVermelho_LED_PIN_D2 = 2;
  50.  
  51. /***** DEFINITION OF PWM OUTPUT PINS *****/
  52. const uint8_t Servo1_Servomotor_PWMSignal_PIN_D3 = 3;
  53.  
  54. // USER CODE VARIABLES
  55. Servo servoMotor; // Create a Servo object to control the servo
  56. int anguloEsquerda = 45; // Angle to move the servo to the left
  57. int anguloDireita = 135; // Angle to move the servo to the right
  58.  
  59. unsigned long tempoAnterior = 0; // Variable to store the previous time
  60. unsigned long intervalo = 50; // Interval of 50 ms between each separation
  61.  
  62. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  63. bool LedVerde_LED_PIN_D6_rawData = 0;
  64. bool LedVermelho_LED_PIN_D2_rawData = 0;
  65.  
  66. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  67.  
  68. void setup(void)
  69. {
  70.     // Initialize the output pins
  71.     pinMode(LedVerde_LED_PIN_D6, OUTPUT);
  72.     pinMode(LedVermelho_LED_PIN_D2, OUTPUT);
  73.     pinMode(Servo1_Servomotor_PWMSignal_PIN_D3, OUTPUT);
  74.  
  75.     servoMotor.attach(Servo1_Servomotor_PWMSignal_PIN_D3); // Connect the servo to the PWM pin
  76.     servoMotor.write(anguloEsquerda); // Initialize the servo in the left position
  77. }
  78.  
  79. void loop(void)
  80. {
  81.     // Main code to run repeatedly
  82.     unsigned long tempoAtual = millis(); // Get the current time in milliseconds
  83.  
  84.     if (tempoAtual - tempoAnterior >= intervalo) { // Check if 50 ms has passed
  85.         tempoAnterior = tempoAtual; // Update the previous time
  86.  
  87.         // Alternate the LEDs and move the servo
  88.         if (servoMotor.read() == anguloEsquerda) {
  89.             servoMotor.write(anguloDireita); // Move the servo to the right
  90.             LedVerde_LED_PIN_D6_rawData = LOW; // Turn off the green LED
  91.             LedVermelho_LED_PIN_D2_rawData = HIGH; // Turn on the red LED
  92.         } else {
  93.             servoMotor.write(anguloEsquerda); // Move the servo to the left
  94.             LedVerde_LED_PIN_D6_rawData = HIGH; // Turn on the green LED
  95.             LedVermelho_LED_PIN_D2_rawData = LOW; // Turn off the red LED
  96.         }
  97.     }
  98.  
  99.     updateOutputs(); // Refresh output data
  100.     delay(50); // Add delay of 50 ms between each separation
  101. }
  102.  
  103. void updateOutputs()
  104. {
  105.     digitalWrite(LedVerde_LED_PIN_D6, LedVerde_LED_PIN_D6_rawData);
  106.     digitalWrite(LedVermelho_LED_PIN_D2, LedVermelho_LED_PIN_D2_rawData);
  107. }
  108.  
  109. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement