#include <ESP32Servo.h>

#define PIN_SERVO 4
#define PIN_BOTAO 5

Servo cancela;
int pos = 90;

void setup() {
  cancela.attach(PIN_SERVO);
  pinMode(PIN_BOTAO, INPUT_PULLUP);
}

void loop() {
  int buttom = digitalRead((PIN_BOTAO));

  if(!buttom){
    open();
    delay(200);
    close();
  }

  buttom = 0;
}

void open(){
  for (pos = 90; pos >= 0; pos -= 1) {
    cancela.write(pos);
    delay(15);
  }
  
}

void close(){
  
  for (pos = 0; pos <= 90; pos += 1) {
    cancela.write(pos);
    delay(15);
  }
}