ciao a tutti, qualcuno ha idea del perche' e magari una riga di spiegazione del perche' non funziona?
semplicemente con un pulsante ogni volta che viene premuto si deve accendere un led ad ogni pressione si spegne il led precedente per accendersi il sucessivo.
ma non va niente, tutto spento.
grazie
const int buttonPin = 8;
const int ledPin1 = 2;
const int ledPin2 = 3;
const int ledPin3 = 4;
int ButtonState = 0;
int ButtonCount = 0;
void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
ButtonCount = digitalRead(buttonPin);
if (ButtonState == HIGH && ButtonCount == 0) {
digitalWrite(ledPin1, HIGH);
ButtonCount++;}
else if (ButtonState == HIGH && ButtonCount == 1) {
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin1, LOW);
ButtonCount++;}
else if (ButtonState == HIGH && ButtonCount == 2) {
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin2, LOW);
ButtonCount++;}
}
come puó assumere ButtonCount un valore diverso da 0 o 1 se lo leggi con ButtonCount = digitalRead(buttonPin); ?
hai confuso ButtonCount e ButtonState ?
da qualche parte devi poi resettare ButtonCount
Inoltre devi fare un controllo che il pulsante sia stato rilasciato prima di passaare al LED sucessivo. senó Ti passa troppo velocemente da un LEd all'altro.
Pulsante HIGH // prima volta premuto
Pulsante LOW // pulsante rilasciato
Pulsante HIGH // seconda volta premuto
Pulsante LOW // pulsante rilasciato
ecc
Allora, comincia col prendere un esempio per accendere un solo led con la funzione debounce, il debounce crea un piccolo ritardo che impedisce di avere false letture durante l'avvicinamento e l'allontanamento dei contatti durante la pressione e il rilascio del pulsante.
poi modifica quello scketch aggiungendo più led seguendo i consigli di uwe, posta il codice di quello che hai fatto se non capisci qualcosa
fatto, ho modificato l'esempio molto e' migliorato ma ancora va un po' a muzzo
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 8; // the number of the pushbutton pin
const int ledPin1 = 2; // the number of the LED pin
const int ledPin2 = 3; // the number of the LED pin
const int ledPin3 = 4; // the number of the LED pin
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
ledState = !ledState;
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
}
}
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
ledState = !ledState;
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, LOW);
}
}
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
ledState = !ledState;
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH);
}
}
}
lastButtonState = reading;
}
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 8; // the number of the pushbutton pin
const int ledPin1 = 2; // the number of the LED pin
const int ledPin2 = 3; // the number of the LED pin
const int ledPin3 = 4; // the number of the LED pin
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
int count=0;
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay && count==0) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
ledState = !ledState;
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
count++;
}
}
}
if ((millis() - lastDebounceTime) > debounceDelay && count==1) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
ledState = !ledState;
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, LOW);
count++;
}
}
}
if ((millis() - lastDebounceTime) > debounceDelay && count==2) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
ledState = !ledState;
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH);
count++;
}
}
}
lastButtonState = reading;
if (count>2)
{
count=0;
}
}
Ti sei complicato la vita parecchio, in realtà potevi farlo solo con un "if (millis()" che è quello che serve per leggere il buttons + un array dei pin dove l'indice (count) corrisponde a numero del pinled, se devi farlo per 16 led cosa fai metti 16 pezzi "if (millis()"?