#include <ESP32Servo.h>
Servo servo;
int pinPulsador1 = 4;
int pinPulsador2 = 5;
int pinServo = 2;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
//Serial.println("Hello, ESP32!");
pinMode(pinPulsador1, INPUT);
pinMode(pinPulsador2, INPUT);
servo.attach(pinServo,500,2500);
}
int posServo = 0;
void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(pinPulsador1)==HIGH)
{
moverServo(180);
delay(1000);
}
if(digitalRead(pinPulsador2)==HIGH)
{
moverServo(0);
delay(1000);
}
delay(20); // this speeds up the simulation
}
void moverServo(int posFinal){
if(posServo < posFinal)
{
for(int pos = posServo; pos<=posFinal;pos+=1)
{
servo.write(pos);
delay(20);
}
}
else
{
for(int pos = posFinal;pos >= posServo;pos-=1)
{
servo.write(pos);
delay(20);
}
}
}