In questo esercizio premendo un pulsante accendiamo il primo diodo LED, lasciando il pulsante il diodo LED si spegne.
Sketch dell'esercizio
// Esercizio 1 - PULSANTI
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 4; // the number of the LED pin
// variable
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
In questo esercizio premendo il primo pulsante accendiamo il primo diodo LED, ripremendo il pulsante lo spegniamo.
Sketch dell'esercizio
// Esercizio 2 - PULSANTI
int inPin = 2; // the number of the input pin
int outPin = 4; // the number of the output pin
int state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 300; // the debounce time, increase if the output flickers
void setup()
{
pinMode(inPin, INPUT);
pinMode(outPin, OUTPUT);
}
void loop()
{
reading = digitalRead(inPin);
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;
time = millis();
}
previous = reading;
digitalWrite(outPin, state);
}
In questo esercizio premendo il primo pulsante accendiamo il primo diodo LED. Premendo il secondo pulsante invece lo spegniamo.
Sketch dell'esercizio
// Esercizio 3 - PULSANTI
int inPin = 2; // the number of the input pin
int inPin2 = 3; // the number of the input pin
int outPin = 4; // the number of the output pin
int state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
void setup()
{
pinMode(inPin, INPUT);
pinMode(inPin2, INPUT);
pinMode(outPin, OUTPUT);
}
void loop()
{
if (digitalRead(outPin) == HIGH) {
reading = digitalRead(inPin);
}
else {
reading = digitalRead(inPin2);
}
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;
time = millis();
}
digitalWrite(outPin, state);
previous = reading;
}