#define LED 12 // The digital pin to which a led is connected.
#define SLIDE 13
#define C_TIME 200000
volatile uint64_t pwm_value = 0;
volatile bool led_status_g;
hw_timer_t *My_timer = NULL;
void IRAM_ATTR onTimer(){
bool led_status = digitalRead(LED);
if (led_status && pwm_value!=C_TIME){
digitalWrite(LED, !led_status);
timerWrite(My_timer, pwm_value);
}else if (!led_status && pwm_value!=0){
digitalWrite(LED, !led_status);
timerWrite(My_timer, C_TIME-pwm_value);
}
led_status_g=digitalRead(LED);
}
void setup() {
pinMode(LED, OUTPUT);
pinMode(SLIDE, INPUT);
My_timer = timerBegin(0, 80, true);
timerAttachInterrupt(My_timer, &onTimer, true);
timerAlarmWrite(My_timer, C_TIME, true);
timerAlarmEnable(My_timer); //Just Enable
Serial.begin(115200);
Serial.setTimeout( 10);
// Serial.print( "Enter brightness, 0...");
// Serial.println( C_TIME);
}
void loop() {
//if(Serial.available() > 0)
//{
// int brightness = Serial.parseInt(); // get the number
// Serial.read(); //Delete the new line char passed after pressing enter
// pwm_value = constrain( brightness, 0, C_TIME); // set the new PWM value
//}
pwm_value = map(analogRead(SLIDE), 0, 4095, 0, C_TIME);
//Serial.println( pwm_value );
Serial.print( led_status_g );
Serial.print("\r\n");
}