#include <ESP32Servo.h>
const int buttonPin = 34;
int oldValue = LOW;
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int estadoServo = 0; // 0 = fechado, 1 = aberto
int pos = 0; // variable to store the servo position
// buzzer
#define SPEAKER_PIN 22
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
// Initialize the pin for reading the button.
pinMode(buttonPin, INPUT);
myservo.attach(23);
myservo.write(0); // Inicia na posição 0°
//buzzer
}
void loop() {
int newValue = digitalRead(buttonPin);
if (newValue == HIGH && oldValue == LOW) { // Apenas na transição do botão pressionado
if (estadoServo == 0) {
for (pos = 0; pos <= 90; pos++) {
myservo.write(pos);
delay(15);
}
estadoServo = 1; // Servo agora está aberto
} else {
for (pos = 90; pos >= 0; pos--) {
myservo.write(pos);
delay(15);
}
estadoServo = 0; // Servo agora está fechado
}
}
oldValue = newValue; // Atualiza o estado do botão
}