Advertisement
pleasedontcode

"Servo Control" rev_01

Jan 12th, 2025
54
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:20:47
  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. /* START CODE */
  31.  
  32. /****** DEFINITION OF LIBRARIES *****/
  33. #include <Servo.h>  //https://github.com/arduino-libraries/Servo
  34.  
  35. /****** FUNCTION PROTOTYPES *****/
  36. void setup(void);
  37. void loop(void);
  38. void updateOutputs(void);
  39.  
  40. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  41. const uint8_t LedVerde_LED_PIN_D6       = 6;
  42. const uint8_t LedVermelho_LED_PIN_D2        = 2;
  43.  
  44. /***** DEFINITION OF PWM OUTPUT PINS *****/
  45. const uint8_t Servo1_Servomotor_PWMSignal_PIN_D3        = 3;
  46.  
  47. // USER CODE VARIABLES
  48. Servo servoMotor; // Create a Servo object to control the servo
  49. int anguloEsquerda = 45; // Angle to move the servo to the left
  50. int anguloDireita = 135; // Angle to move the servo to the right
  51.  
  52. unsigned long tempoAnterior = 0; // Variable to store the previous time
  53. unsigned long intervalo = 50; // Interval of 50 ms between each separation
  54.  
  55. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  56. bool    LedVerde_LED_PIN_D6_rawData     = 0;
  57. bool    LedVermelho_LED_PIN_D2_rawData      = 0;
  58.  
  59. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  60.  
  61. void setup(void)
  62. {
  63.     // Initialize the output pins
  64.     pinMode(LedVerde_LED_PIN_D6,     OUTPUT);
  65.     pinMode(LedVermelho_LED_PIN_D2,  OUTPUT);
  66.     pinMode(Servo1_Servomotor_PWMSignal_PIN_D3,  OUTPUT);
  67.  
  68.     servoMotor.attach(Servo1_Servomotor_PWMSignal_PIN_D3); // Connect the servo to the PWM pin
  69.     servoMotor.write(anguloEsquerda); // Initialize the servo in the left position
  70. }
  71.  
  72. void loop(void)
  73. {
  74.     // Main code to run repeatedly
  75.     unsigned long tempoAtual = millis(); // Get the current time in milliseconds
  76.  
  77.     if (tempoAtual - tempoAnterior >= intervalo) { // Check if 50 ms has passed
  78.         tempoAnterior = tempoAtual; // Update the previous time
  79.  
  80.         // Alternate the LEDs and move the servo
  81.         if (servoMotor.read() == anguloEsquerda) {
  82.             servoMotor.write(anguloDireita); // Move the servo to the right
  83.             LedVerde_LED_PIN_D6_rawData = LOW; // Turn off the green LED
  84.             LedVermelho_LED_PIN_D2_rawData = HIGH; // Turn on the red LED
  85.         } else {
  86.             servoMotor.write(anguloEsquerda); // Move the servo to the left
  87.             LedVerde_LED_PIN_D6_rawData = HIGH; // Turn on the green LED
  88.             LedVermelho_LED_PIN_D2_rawData = LOW; // Turn off the red LED
  89.         }
  90.     }
  91.  
  92.     updateOutputs(); // Refresh output data
  93. }
  94.  
  95. void updateOutputs()
  96. {
  97.     digitalWrite(LedVerde_LED_PIN_D6, LedVerde_LED_PIN_D6_rawData);
  98.     digitalWrite(LedVermelho_LED_PIN_D2, LedVermelho_LED_PIN_D2_rawData);
  99. }
  100.  
  101. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement