Pulsanti
Esercizio 1

In questo esercizio premendo un pulsante accendiamo il primo diodo LED, lasciando il pulsante il diodo LED si spegne.

Sketch dell'esercizio

logoinformatici
// 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);  } }
©informatici.pietrocoppo.net

Pulsanti
Esercizio 2

In questo esercizio premendo il primo pulsante accendiamo il primo diodo LED, ripremendo il pulsante lo spegniamo.

Sketch dell'esercizio

logoinformatici
// 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);   }
©informatici.pietrocoppo.net

Pulsanti
Esercizio 3

In questo esercizio premendo il primo pulsante accendiamo il primo diodo LED. Premendo il secondo pulsante invece lo spegniamo.

Sketch dell'esercizio

logoinformatici
// 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; }
©informatici.pietrocoppo.net

Pulsanti
Esercizio 4

In questo esercizio premendo il primo pulsante accendiamo il primo diodo LED, mentre premendo il secondo pulsante accendiamo il secondo diodo LED.

Sketch dell'esercizio

logoinformatici
// Esercizio 4 - PULSANTI int pinPrimo = 2; int pinSecondo = 3; int pinLed = 4; int pinLedDue = 5; int statoUno; int statoDue0; void setup() {    pinMode(pinLed,OUTPUT);    pinMode(pinLedDue,OUTPUT);    pinMode(pinPrimo,INPUT);    pinMode(pinSecondo,INPUT); } void loop() {  statoUno = digitalRead(pinPrimo);  statoDue0 = digitalRead(pinSecondo);  if (statoUno == HIGH) {    digitalWrite(pinLed, HIGH);  }  else {    digitalWrite(pinLed,LOW);  }  if (statoDue0 == HIGH) {    digitalWrite(pinLedDue,HIGH);  }  else {    digitalWrite(pinLedDue,LOW);  }    }
©informatici.pietrocoppo.net