Ciao a tutti,
ho creato un PWM per dimmerare un LED accensione e deve rimanere acceso
--fin qui tutto OK--
quando premo un tasto il LED si deve spegnere e rilasciando il tasto deve ripartire il dimmer in accensione rimando acceso
--anche fin qui tutto OK--
Il problema nasce quando voglio gestire un ritardo della ripartenza dell'accensione dimmer del LED dopo la pressione del tasto
Il primo skecth come dicevo funziona:
const int led = 9; // PWM pin del LED
const int tasto = 7; // ingresso per tasto spegni
unsigned long FADE_PEDIOD = 6000; // imposta il tempo del fade time
unsigned long fadeStartTime;
unsigned long waiting = 1500; // imposta il timeout prima della riaccensione del LED
unsigned long t1 = millis();
unsigned long dt;
bool RUN = false;
int tastoValue;
int tastoLight;
// the setup routine
void setup() {
pinMode(led, OUTPUT); // LED
pinMode(7, INPUT_PULLUP); //tasto
Serial.begin(9600);
fadeStartTime = millis();
}
// fade-in in loop, and restart after finishing
void loop() {
unsigned long progress = millis() - fadeStartTime;
tastoValue = digitalRead (tasto);
if (tastoValue == HIGH) {
tastoLight = 1;
Serial.print ("HIGH");
}
if (tastoValue == LOW) {
tastoLight = 0;
digitalWrite (led, 0);
fadeStartTime = millis(); // restart fade again
Serial.print ("LOW");
}
if ((progress <= FADE_PEDIOD) && (tastoLight == 1)) {
long brightness = map(progress, 0, FADE_PEDIOD, 0, 255);
analogWrite(led, brightness);
Serial.print ("FADE");
}
}
Ma nella versione con il tasto temporizzato ..... purtroppo non va:
const int led = 9; // PWM pin del LED
const int tasto = 7; // ingresso per tasto spegni
unsigned long FADE_PEDIOD = 6000; // imposta il tempo del fade time
unsigned long fadeStartTime;
unsigned long waiting = 1500; // imposta il timeout prima della riaccensione del LED
unsigned long t1 = millis();
unsigned long dt;
int tastoValue;
int tastoLight;
// the setup routine
void setup() {
pinMode(led, OUTPUT); //LED
pinMode(7, INPUT_PULLUP); //tasto
Serial.begin(9600);
fadeStartTime = millis();
}
// fade-in
void loop() {
unsigned long progress = millis() - fadeStartTime;
tastoValue = digitalRead (tasto);
if (tastoValue == HIGH) {
tastoLight = 1;
Serial.print ("HIGH");
}
if (tastoValue == LOW){
tastoLight = 0;
digitalWrite (led, 0);
t1 = millis();
}
if ((tastoLight == 0) && (dt >= waiting)) {
fadeStartTime = millis(); // restart fade again
Serial.print ("LOW");
}
if ((progress <= FADE_PEDIOD) && (tastoLight == 1)) {
long brightness = map(progress, 0, FADE_PEDIOD, 0, 255);
analogWrite(led, brightness);
Serial.print ("FADE");
}
}
Qualcucno mi può dare una dritta?
Grazie in anticipo