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 Uno
- - Source Code created on: 2025-01-12 20:20:47
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* esquema elétrico na plataforma Arduino representa */
- /* um sistema que separa peças que se movimentam num */
- /* tapete rolantes a uma cadência de 50 ms entre */
- /* elas. Pretende-se que o sistema separe */
- /* alternadamente uma peça para esquerda, a seguinte */
- /* para a direia */
- /****** SYSTEM REQUIREMENT 2 *****/
- /* acendendo um Led Verde ou Vermelho respetivamente. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Servo.h> //https://github.com/arduino-libraries/Servo
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void updateOutputs(void);
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t LedVerde_LED_PIN_D6 = 6;
- const uint8_t LedVermelho_LED_PIN_D2 = 2;
- /***** DEFINITION OF PWM OUTPUT PINS *****/
- const uint8_t Servo1_Servomotor_PWMSignal_PIN_D3 = 3;
- // USER CODE VARIABLES
- Servo servoMotor; // Create a Servo object to control the servo
- int anguloEsquerda = 45; // Angle to move the servo to the left
- int anguloDireita = 135; // Angle to move the servo to the right
- unsigned long tempoAnterior = 0; // Variable to store the previous time
- unsigned long intervalo = 50; // Interval of 50 ms between each separation
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- bool LedVerde_LED_PIN_D6_rawData = 0;
- bool LedVermelho_LED_PIN_D2_rawData = 0;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- void setup(void)
- {
- // Initialize the output pins
- pinMode(LedVerde_LED_PIN_D6, OUTPUT);
- pinMode(LedVermelho_LED_PIN_D2, OUTPUT);
- pinMode(Servo1_Servomotor_PWMSignal_PIN_D3, OUTPUT);
- servoMotor.attach(Servo1_Servomotor_PWMSignal_PIN_D3); // Connect the servo to the PWM pin
- servoMotor.write(anguloEsquerda); // Initialize the servo in the left position
- }
- void loop(void)
- {
- // Main code to run repeatedly
- unsigned long tempoAtual = millis(); // Get the current time in milliseconds
- if (tempoAtual - tempoAnterior >= intervalo) { // Check if 50 ms has passed
- tempoAnterior = tempoAtual; // Update the previous time
- // Alternate the LEDs and move the servo
- if (servoMotor.read() == anguloEsquerda) {
- servoMotor.write(anguloDireita); // Move the servo to the right
- LedVerde_LED_PIN_D6_rawData = LOW; // Turn off the green LED
- LedVermelho_LED_PIN_D2_rawData = HIGH; // Turn on the red LED
- } else {
- servoMotor.write(anguloEsquerda); // Move the servo to the left
- LedVerde_LED_PIN_D6_rawData = HIGH; // Turn on the green LED
- LedVermelho_LED_PIN_D2_rawData = LOW; // Turn off the red LED
- }
- }
- updateOutputs(); // Refresh output data
- }
- void updateOutputs()
- {
- digitalWrite(LedVerde_LED_PIN_D6, LedVerde_LED_PIN_D6_rawData);
- digitalWrite(LedVermelho_LED_PIN_D2, LedVermelho_LED_PIN_D2_rawData);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement