const int ledPin = 22;
const int servoPin = 13;
const int freq = 1000;
const int servofreq = 50;
const int ledChannel = 0;
const int servoChannel = 1;
const int resolution = 8;
const int STOPBTN = 27;
const int BTNPWM1 = 26;
const int BTNPWM2 = 25;
const int BTNPWM3 = 33;
const int ToggleBTN = 12;
hw_timer_t *My_timer = NULL;
bool isStop = false;
void IRAM_ATTR Level1(){
ledcWrite(ledChannel,5);
}
void IRAM_ATTR Level2(){
ledcWrite(ledChannel,25);
}
void IRAM_ATTR Level3(){
ledcWrite(ledChannel,100);
}
void IRAM_ATTR STOP(){
isStop = true;
ledcWrite(ledChannel,0);
}
void IRAM_ATTR StopNeck(){
isStop = !isStop;
}
void setup() {
pinMode(BTNPWM1, INPUT);
pinMode(BTNPWM2, INPUT);
pinMode(BTNPWM3,INPUT);
pinMode(STOPBTN, INPUT);
ledcAttachPin(ledPin, ledChannel);
ledcSetup(ledChannel, freq, resolution);
ledcAttachPin(servoPin, servoChannel);
ledcSetup(servoChannel, servofreq, resolution);
attachInterrupt(BTNPWM1,Level1,RISING);
attachInterrupt(BTNPWM2,Level2,RISING);
attachInterrupt(BTNPWM3,Level3,RISING);
attachInterrupt(STOPBTN,STOP,RISING);
attachInterrupt(ToggleBTN,StopNeck,RISING);
ledcWrite(ledChannel,0);
}
void loop() {
if(!isStop){
for(int duty = 0; duty <= 31 ;duty++){
ledcWrite(servoChannel,duty);
delay(35);
if(isStop) break;
}
delay(35);
if(!isStop){
for(int duty = 31; duty >=0 ;duty--){
ledcWrite(servoChannel,duty);
delay(35);
if(isStop) break;
}
}
}
}