#include <ESP32Servo.h>
#define servoPin 13
#define led 22
#define speedB 34
#define turnB 35
Servo servo;
bool turnAble = false;
bool turnDirection = false;
int currentSpeed = 0;
int currentAngle = 90;
void IRAM_ATTR Ext_INT1_ISR(){
currentSpeed=(currentSpeed+1)%4;
analogWrite(led, currentSpeed*70);
}
void IRAM_ATTR Ext_INT2_ISR(){
turnAble = !turnAble;
}
void setup() {
pinMode(speedB, INPUT);
pinMode(turnB, INPUT);
attachInterrupt(speedB, Ext_INT1_ISR, RISING);
attachInterrupt(turnB, Ext_INT2_ISR, RISING);
servo.attach(servoPin);
Serial.begin(9600);
}
void loop() {
if(turnAble){
if(currentAngle > 180) turnDirection = true;
else if(currentAngle < 0) turnDirection = false;
servo.write(currentAngle);
turnDirection == false? currentAngle += 10 : currentAngle -= 10;
delay(100);
}
delay(10);
}