Last active
November 10, 2024 02:03
-
-
Save andreztz/ef93f17e248ad1e2f74f1e1be35a3d69 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <Servo.h> | |
| Servo myServo; | |
| const int servoPin = 9; // Pino onde o servo motor está conectado | |
| const int angleOpen = 90; // Ângulo para empurrar o cigarro | |
| const int angleClose = 0; // Ângulo para posição de repouso | |
| unsigned long previousMillis = 0; // Armazena o último tempo em que o servo foi ativado | |
| const long interval = 3600000; // Intervalo de 1 hora (em milissegundos) | |
| void setup() { | |
| myServo.attach(servoPin); // Conecta o servo motor | |
| myServo.write(angleClose); // Inicializa na posição de repouso | |
| previousMillis = millis(); // Define o tempo inicial | |
| } | |
| void loop() { | |
| unsigned long currentMillis = millis(); // Obtém o tempo atual | |
| // Verifica se 1 hora (3600000 milissegundos) já se passou desde a última liberação | |
| if (currentMillis - previousMillis >= interval) { | |
| liberarCigarro(); // Libera o cigarro | |
| previousMillis = currentMillis; // Atualiza o último tempo | |
| } | |
| // Verifica a cada 1 segundo (ou outro intervalo curto) | |
| delay(1000); | |
| } | |
| void liberarCigarro() { | |
| myServo.write(angleOpen); // Move o servo para empurrar o cigarro | |
| delay(1000); // Espera 1 segundo para garantir que o servo complete o movimento | |
| myServo.write(angleClose); // Retorna o servo à posição de repouso | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <Servo.h> | |
| Servo myServo; | |
| const int servoPin = 9; // Pino onde o servo motor está conectado | |
| const int angleOpen = 60;// Ângulo para empurrar o cigarro | |
| const int angleClose = 0;// Ângulo para posição de repouso | |
| unsigned long previousMillis = 0; // Armazena o último tempo em que o servo foi ativado | |
| // const long interval = 3600000; // Intervalo de 1 hora (em milissegundos) | |
| const long interval = 10000; // intervalo para testes | |
| void setup() { | |
| Serial.begin(9600); | |
| myServo.attach(servoPin); // Conecta o servo motor | |
| myServo.write(angleClose); // Inicializa na posição de repouso | |
| previousMillis = millis(); // Define o tempo inicial | |
| } | |
| void loop() { | |
| unsigned long currentMillis = millis(); // Obtém o tempo atual | |
| log("interval -> ", interval); | |
| log("previousMillis -> ", previousMillis); | |
| log("currentMillis -> ", currentMillis); | |
| log("currentMillis - previousMilis eh igual ou maior que insterval? ", (currentMillis - previousMillis >= interval)); | |
| // Verifica se 1 hora (3600000 milissegundos) já se passou desde a última liberação | |
| if (currentMillis - previousMillis >= interval) { | |
| log("Tempo atingido ou superado, liberando cigarro!"); | |
| liberarCigarro(); // Libera o cigarro | |
| previousMillis = currentMillis; // Atualiza o último tempo | |
| } else { | |
| log("O tempo para liberar um cigarro ainda nao foi atingido ou superado..."); | |
| } | |
| // Verifica a cada 1 segundo (ou outro intervalo curto) | |
| delay(1000); | |
| } | |
| void liberarCigarro() { | |
| myServo.write(angleOpen); // Move o servo para empurrar o cigarro | |
| delay(1000); // Espera 1 segundo para garantir que o servo complete o movimento | |
| myServo.write(angleClose); // Retorna o servo à posição de repouso | |
| } | |
| /* | |
| Funções para gerar registro no Serial monitor. | |
| Com a tecnica de sobrecarga de funções, é possível | |
| criar várias funções `log`, cada uma com um tipo | |
| específico para o parâmetro `value`, de forma que o | |
| Arduino vai escolher automaticamente a versão correta | |
| da função `log` com base no tipo de `value` fornecido. | |
| */ | |
| void log(String message) { | |
| // Funç | |
| Serial.println(message); | |
| } | |
| void log(String message, int value) { | |
| // Função de registro para tipo String e int | |
| Serial.print(message); | |
| Serial.println(value); | |
| } | |
| void log(String message, long int value) { | |
| // Função de registro para tipo String e long int | |
| Serial.print(message); | |
| Serial.println(value); | |
| } | |
| void log(String message, unsigned long value) { | |
| // Função de registro para tipo String e unsigned long | |
| Serial.print(message); | |
| Serial.println(value); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment