Last active
December 2, 2025 12:24
-
-
Save IgorDePaula/d4999d572dfe328088f9be4dbfb3c502 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 <ESP32Servo.h> | |
| // TOUCH SENSORS | |
| #define TOUCH1 4 // T0 (GPIO 4) | |
| #define TOUCH2 27 // T7 (GPIO 27) | |
| // SERVOS | |
| static const int servoPin = 18; | |
| static const int servoPin2 = 19; | |
| Servo servo1; | |
| Servo servo2; | |
| // Constantes para servo de rotação contínua | |
| const int PARADO = 90; // Servo parado | |
| const int HORARIO = 0; // Gira no sentido horário (velocidade máxima) | |
| const int ANTI_HORARIO = 180; // Gira no sentido anti-horário (velocidade máxima) | |
| // Variáveis de estado | |
| int estadoAtual = PARADO; | |
| unsigned long ultimaLeitura = 0; | |
| const int intervaloLeitura = 50; // 50ms entre leituras | |
| // Threshold do touch (ajuste conforme necessário) | |
| const int THRESHOLD_TOUCH = 40; | |
| void setup() { | |
| Serial.begin(115200); | |
| delay(1000); | |
| Serial.println("=== INICIANDO - Servo MG996R 360° ==="); | |
| // Aloca timers | |
| ESP32PWM::allocateTimer(0); | |
| ESP32PWM::allocateTimer(1); | |
| // Configura servos | |
| servo1.setPeriodHertz(50); | |
| servo1.attach(servoPin, 1000, 2000); | |
| servo1.write(PARADO); | |
| servo2.setPeriodHertz(50); | |
| servo2.attach(servoPin2, 1000, 2000); | |
| servo2.write(PARADO); | |
| Serial.println("Servos configurados e PARADOS"); | |
| Serial.println("Touch1: Gira em um sentido"); | |
| Serial.println("Touch2: Gira no sentido oposto"); | |
| Serial.println("Solte: Para os motores"); | |
| } | |
| void loop() { | |
| // Controle de tempo para evitar leituras excessivas | |
| unsigned long tempoAtual = millis(); | |
| if (tempoAtual - ultimaLeitura < intervaloLeitura) { | |
| return; | |
| } | |
| ultimaLeitura = tempoAtual; | |
| // Lê os sensores touch | |
| int touch1 = touchRead(TOUCH1); | |
| int touch2 = touchRead(TOUCH2); | |
| // Debug (comente se quiser menos mensagens) | |
| Serial.print("T1: "); | |
| Serial.print(touch1); | |
| Serial.print(" | T2: "); | |
| Serial.print(touch2); | |
| Serial.print(" | Estado: "); | |
| Serial.println(estadoAtual); | |
| // Determina o novo estado baseado nos touches | |
| int novoEstado = PARADO; | |
| if (touch1 < THRESHOLD_TOUCH) { | |
| novoEstado = HORARIO; // ou ANTI_HORARIO, se quiser inverter | |
| } | |
| else if (touch2 < THRESHOLD_TOUCH) { | |
| novoEstado = ANTI_HORARIO; // ou HORARIO, se quiser inverter | |
| } | |
| // Só envia comando se o estado mudou | |
| if (novoEstado != estadoAtual) { | |
| estadoAtual = novoEstado; | |
| servo1.write(estadoAtual); | |
| servo2.write(estadoAtual); | |
| // Mensagens de debug | |
| if (estadoAtual == PARADO) { | |
| Serial.println(">>> MOTORES PARADOS"); | |
| } else if (estadoAtual == HORARIO) { | |
| Serial.println(">>> GIRANDO: Sentido Horário"); | |
| } else if (estadoAtual == ANTI_HORARIO) { | |
| Serial.println(">>> GIRANDO: Sentido Anti-Horário"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment