Salve a tutti!
Ho compilato, con spunti da altri sketch online, un driver per strip led, che cambia modalità alla pressione di un pulsante.
Funziona bene, non riesco però a cambiare modalità (e quindi spegnere, essendo lo stato successivo lo spegnimento) mentre è attivo il ciclo tra i vari colori.
A volte becco il momento esatto da premere per farlo andare avanti, ma vorrei semplicemente poter interrompere il fade in qualunque momento. Come faccio?
#define REDPIN 3
#define GREENPIN 5
#define BLUEPIN 6
#define buttonPin 4
#define FADESPEED 5 // make this higher to slow down
int buttonState = 0;
int buttonPushCounter = 0;
int lastButtonState = 0;
int r, g, b;
void setup() {
pinMode(REDPIN, OUTPUT);
pinMode(GREENPIN, OUTPUT);
pinMode(BLUEPIN, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
buttonPushCounter++;
}
delay(50);
}
lastButtonState = buttonState;
//ROSSO
if (buttonPushCounter == 1) {
analogWrite(REDPIN, 255);
analogWrite(GREENPIN, 0);
analogWrite(BLUEPIN, 0);
}
//BLU
if (buttonPushCounter == 2) {
analogWrite(REDPIN, 0);
analogWrite(GREENPIN, 0);
analogWrite(BLUEPIN, 255);
}
//VERDE
if (buttonPushCounter == 3) {
analogWrite(REDPIN, 0);
analogWrite(GREENPIN, 255);
analogWrite(BLUEPIN, 0);
}
//TEAL
if (buttonPushCounter == 4) {
analogWrite(REDPIN, 0);
analogWrite(GREENPIN, 128);
analogWrite(BLUEPIN, 128);
}
//BIANCO
if (buttonPushCounter == 5) {
analogWrite(REDPIN, 255);
analogWrite(GREENPIN, 255);
analogWrite(BLUEPIN, 245);
}
//FADE
if (buttonPushCounter == 6) {
// fade from blue to violet
for (r = 0; r < 256; r++) {
analogWrite(REDPIN, r);
delay(FADESPEED);
}
// fade from violet to red
for (b = 255; b > 0; b--) {
analogWrite(BLUEPIN, b);
delay(FADESPEED);
}
// fade from red to yellow
for (g = 0; g < 256; g++) {
analogWrite(GREENPIN, g);
delay(FADESPEED);
}
// fade from yellow to green
for (r = 255; r > 0; r--) {
analogWrite(REDPIN, r);
delay(FADESPEED);
}
// fade from green to teal
for (b = 0; b < 256; b++) {
analogWrite(BLUEPIN, b);
delay(FADESPEED);
}
// fade from teal to blue
for (g = 255; g > 0; g--) {
analogWrite(GREENPIN, g);
delay(FADESPEED);
}
}
// Reset del counter
if (buttonPushCounter == 7) {
analogWrite(REDPIN, 0);
analogWrite(GREENPIN, 0);
analogWrite(BLUEPIN, 0);
buttonPushCounter = 0;
}
}