Strano. ![]()
Collega un led con la sua resistenza al pin 9 (vedendo il tuo sito credo che a casa ne abbia un bel pò
), mentre il pulsante al pin 3 con resistenza + una resistenza di pull-down. (Fig. 3 --> Collegare un pulsante ad Arduino | McMajan)
Ricopio qui il codice per chiarezza
// this constant won't change:
const int buttonPin = 3; // the pin that the pushbutton is attached to
const int ledPin = 9; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
unsigned long t1, t2 = 0;
int durata = 0;
int intervallo=0;
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
t2=millis();
durata=t2 - t1;
Serial.print(durata);
}
else {
t1 = millis();
intervallo= t1 - t2;
Serial.print(" : ");
Serial.println(intervallo);
digitalWrite(ledPin, HIGH);
delay(10);
digitalWrite(ledPin, LOW);
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
Apri il serial monitor e aziona il pulsante.
Una volta testato il tipo di funzionamento del programma, prova a sostituire il pulsante con il disco combinatore.
EDIT:
Non vorrei ti avesse fuorviato il mio commento
PaoloP:
Tiro ad indovinare... Bianco e Verde.![]()
Stavo veramente cercando di indovinare. Prova le varie combinazioni di colori.