#define LED 12
#define SLIDE 13
// definire la periodcità del mio segnale
#define C_TIME 100 // PWM period
// how much il led deve rimanere acceso (duty cycle)
volatile int pwm_value = 0; // volatile perchè cambia spesso valore
hw_timer_t *Mytimer = NULL; //timer for triggering the led
// funzione che parte se il timer finisce
void IRAM_ATTR timer_function(){
bool led_status = digitalRead(LED);
if(led_status && pwm_value != C_TIME){
digitalWrite(LED, !led_status);
timerWrite(Mytimer, pwm_value);
}
else if(!led_status && pwm_value != 0){
digitalWrite(LED, !led_status);
timerWrite(Mytimer, C_TIME - pwm_value);
}
}
void setup() {
// put your setup code here, to run once:
pinMode(LED, OUTPUT); // define led
pinMode(SLIDE, INPUT); // define slide
Mytimer = timerBegin(0, 80, true); // startare il timer
// 0 is for timer index in the controller
// 80 is the divider for defining the timer tick rate
// true is increment
// attach the interrupt when timer fires
timerAttachInterrupt(Mytimer, &timer_function, true);
timerAlarmWrite(Mytimer, C_TIME, true);
// C_TIME is when to fire
// true is for starting again timer or not
//start timer
timerAlarmEnable(Mytimer);
}
void loop() {
// put your main code here, to run repeatedly:
// la funzione map permette di creare una proporzione tra quello che gli diamo in input e l'altra roba
// quindi lo slide che originariamente va da 0 a 4096, andrà da 0 a C_TIME
pwm_value = map(analogRead(SLIDE), 0, 4096, 0, C_TIME);
}