From 0cf9246dae9d495f333cee506f647664bcf12234 Mon Sep 17 00:00:00 2001 From: eaman Date: Wed, 19 Oct 2016 12:38:06 +0200 Subject: [PATCH] oggi --- .../BlinkWithoutDelay_1.ino | 72 ++++++++++++ .../BlinkWithoutDelay_2_led.ino | 88 ++++++++++++++ .../BlinkWithoutDelay_3_funzione.ino | 93 +++++++++++++++ .../BlinkWithoutDelay_4_argomento.ino | 85 ++++++++++++++ .../BlinkWithoutDelay_5_cleanup.ino | 80 +++++++++++++ .../BlinkWithoutDelay_6_class.ino | 62 ++++++++++ oggi/blink_0/blink_0.ino | 31 +++++ oggi/blink_0_soluzione/blink_0_soluzione.ino | 81 +++++++++++++ oggi/loop_0_rider/loop_0_rider.ino | 110 ++++++++++++++++++ oggi/loop_1_array_loop/loop_1_array_loop.ino | 77 ++++++++++++ .../loop_2_array_loop_serial.ino | 58 +++++++++ 11 files changed, 837 insertions(+) create mode 100644 oggi/BlinkWithoutDelay_1/BlinkWithoutDelay_1.ino create mode 100644 oggi/BlinkWithoutDelay_2_led/BlinkWithoutDelay_2_led.ino create mode 100644 oggi/BlinkWithoutDelay_3_funzione/BlinkWithoutDelay_3_funzione.ino create mode 100644 oggi/BlinkWithoutDelay_4_argomento/BlinkWithoutDelay_4_argomento.ino create mode 100644 oggi/BlinkWithoutDelay_5_cleanup/BlinkWithoutDelay_5_cleanup.ino create mode 100644 oggi/BlinkWithoutDelay_6_class/BlinkWithoutDelay_6_class.ino create mode 100644 oggi/blink_0/blink_0.ino create mode 100644 oggi/blink_0_soluzione/blink_0_soluzione.ino create mode 100644 oggi/loop_0_rider/loop_0_rider.ino create mode 100644 oggi/loop_1_array_loop/loop_1_array_loop.ino create mode 100644 oggi/loop_2_array_loop_serial/loop_2_array_loop_serial.ino diff --git a/oggi/BlinkWithoutDelay_1/BlinkWithoutDelay_1.ino b/oggi/BlinkWithoutDelay_1/BlinkWithoutDelay_1.ino new file mode 100644 index 0000000..72a9694 --- /dev/null +++ b/oggi/BlinkWithoutDelay_1/BlinkWithoutDelay_1.ino @@ -0,0 +1,72 @@ +/* Blink without Delay + + Turns on and off a light emitting diode(LED) connected to a digital + pin, without using the delay() function. This means that other code + can run at the same time without being interrupted by the LED code. + + The circuit: + * LED attached from pin 13 to ground. + * Note: on most Arduinos, there is already an LED on the board + that's attached to pin 13, so no hardware is needed for this example. + + + created 2005 + by David A. Mellis + modified 8 Feb 2010 + by Paul Stoffregen + modified by eaman + + This example code is in the public domain. + + + http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay + */ + +// constants won't change. Used here to +// set pin numbers: +const int ledPin = 13; + +// Variables will change: +int ledState = LOW; // ledState used to set the LED +long previousMillis = 0; // will store last time LED was updated + +// the follow variables is a long because the time, measured in miliseconds, +// will quickly become a bigger number than can be stored in an int. +const long interval = 1000; // interval at which to blink (milliseconds) + +void setup() { + // set the digital pin as output: + pinMode(ledPin, OUTPUT); +} + +void loop() +{ + // here is where you'd put code that needs to be running all the time. + + // check to see if it's time to blink the LED; that is, if the + // difference between the current time and last time you blinked + // the LED is bigger than the interval at which you want to + // blink the LED. + + if (millis() > previousMillis + interval) { + // Aggiorniamo il contatore previousMillis + previousMillis = millis(); + + // if the LED is off turn it on and vice-versa: + if (ledState == LOW) + ledState = HIGH; + else + ledState = LOW; + // e' possibile semplificare questa operazione? + // Hint: lo stato del LED e' binario: ha solo due stati possibili. + + // set the LED with the ledState of the variable: + digitalWrite(ledPin, ledState); + } +} + +/* Domande + 1. Aggioungere un LED che brilli ogni 500ms + 2. E' ora agevole cambiare gli intervalli dei due LED? + Modificare gli intervalli dei due led (es 500ms - 320ms) + */ diff --git a/oggi/BlinkWithoutDelay_2_led/BlinkWithoutDelay_2_led.ino b/oggi/BlinkWithoutDelay_2_led/BlinkWithoutDelay_2_led.ino new file mode 100644 index 0000000..2100ead --- /dev/null +++ b/oggi/BlinkWithoutDelay_2_led/BlinkWithoutDelay_2_led.ino @@ -0,0 +1,88 @@ +/* Blink without Delay - due led + + Turns on and off a light emitting diode(LED) connected to a digital + pin, without using the delay() function. This means that other code + can run at the same time without being interrupted by the LED code. + + The circuit: + * LED attached from pin 13 to ground. + * Note: on most Arduinos, there is already an LED on the board + that's attached to pin 13, so no hardware is needed for this example. + + + created 2005 + by David A. Mellis + modified 8 Feb 2010 + by Paul Stoffregen + modified by eaman + + This example code is in the public domain. + + + http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay + */ + +// constants won't change. Used here to +// set pin numbers: +const int ledA = 13; // Primo LED +const int ledB = 12; // Secondo LED + +// Variabbili di stato +int ledStateA = LOW; // ledState used to set the LED +int ledStateB = LOW; // ledState used to set the LED + +long previousMillisA = 0; // will store last time LED was updated +long previousMillisB = 0; // will store last time LED was updated + +// the follow variables is a long because the time, measured in miliseconds, +// will quickly become a bigger number than can be stored in an int. +long intervalA = 1000; // interval at which to blink (milliseconds) +long intervalB = 500; // interval at which to blink (milliseconds) + +void setup() { + // set the digital pin as output: + pinMode(ledA, OUTPUT); + pinMode(ledB, OUTPUT); +} + +void loop() +{ +// Primo LED + if (millis() > previousMillisA + intervalA) { + // save the last time you blinked the LED + previousMillisA = millis(); + + // if the LED is off turn it on and vice-versa: + if (ledStateA == LOW) + ledStateA = HIGH; + else + ledStateA = LOW; + // set the LED with the ledState of the variable: + digitalWrite(ledA, ledStateA); + } + +// Secondo LED + if (millis() > previousMillisB + intervalB) { + // save the last time you blinked the LED + previousMillisB = millis(); + + // if the LED is off turn it on and vice-versa: + if (ledStateB == LOW) + ledStateB = HIGH; + else + ledStateB = LOW; + // set the LED with the ledState of the variable: + digitalWrite(ledB, ledStateB); + } +} + +/* Domande + 1. Provare a isolare il codice per accendere ogni singolo led in una funzione: + - Quali variabili determinano il comportamento del LED? + - Come cambiano durante il corso dello script? + - Sono globali o locali? + - Quali parti vanno eseguite una sola volta e quali a ogni esecuzione? + */ + + + diff --git a/oggi/BlinkWithoutDelay_3_funzione/BlinkWithoutDelay_3_funzione.ino b/oggi/BlinkWithoutDelay_3_funzione/BlinkWithoutDelay_3_funzione.ino new file mode 100644 index 0000000..e27cf55 --- /dev/null +++ b/oggi/BlinkWithoutDelay_3_funzione/BlinkWithoutDelay_3_funzione.ino @@ -0,0 +1,93 @@ +/* Blink without Delay + + Blink con funzione + + Soluzione: Provare a isolare il codice per accendere ogni singolo led in una funzione: + + - Quali variabili determinano il comportamento del LED? + - Come cambiano durante il corso dello script? + - Sono globali o locali? + + */ + +///////////// +// First LED +const int ledA = 13; // the number of the LED pin +// Variables will change: +int ledStateA = LOW; // ledState used to set the LED +long previousMillisA = 0; // will store last time LED was updated +// the follow variables is a long because the time, measured in miliseconds, +// will quickly become a bigger number than can be stored in an int. +long intervalA = 1000; // interval at which to blink (milliseconds) +void lightLedA () ; + +////////////// +// Second LED +// Now with less global variables thanks to static (see function body) +const int ledB = 12; //Secondo LED + // ledState used to set the LED +long previousMillisB = 0; // will store last time LED was updated + // interval at which to blink (milliseconds) +void lightLedB () ; + + +void setup() { + // set the digital pin as output: + pinMode(ledA, OUTPUT); + pinMode(ledB, OUTPUT); +} + +void loop() +{ + lightLedA(); + lightLedB(); +} + + +// Funzioni: + +void lightLedA () { + if (millis() > previousMillisA + intervalA) { + // save the last time you blinked the LED + previousMillisA = millis(); + + // if the LED is off turn it on and vice-versa: + if (ledStateA == LOW) + ledStateA = HIGH; + else + ledStateA = LOW; + // set the LED with the ledState of the variable: + digitalWrite(ledA, ledStateA); + } + +} + +void lightLedB () { + long intervalB = 500; + static int ledStateB ; // https://www.arduino.cc/en/Reference/Static + if (millis() > previousMillisB + intervalB) { + // save the last time you blinked the LED + previousMillisB = millis(); + + // if the LED is off turn it on and vice-versa: + if (ledStateB == LOW) + ledStateB = HIGH; + else + ledStateB = LOW; + // set the LED with the ledState of the variable: + digitalWrite(ledB, ledStateB); + } +} + + +/* Domande + 1. Modificare le funzioni in modo che accettino come parametro + l'intervallo di lampeggio. + + */ + + + + + + diff --git a/oggi/BlinkWithoutDelay_4_argomento/BlinkWithoutDelay_4_argomento.ino b/oggi/BlinkWithoutDelay_4_argomento/BlinkWithoutDelay_4_argomento.ino new file mode 100644 index 0000000..e2b28a9 --- /dev/null +++ b/oggi/BlinkWithoutDelay_4_argomento/BlinkWithoutDelay_4_argomento.ino @@ -0,0 +1,85 @@ +/* Blink without Delay + Soluzione + + Introdotto un argomento per la funzione che nodifica l'intervallo di lampeggio + + */ + +// First LED +int ledA = 13; // the number of the LED pin +// Variables will change: +int ledStateA = LOW; // ledState used to set the LED +long previousMillisA = 0; // will store last time LED was updated + +// Second LED data +int ledB = 12; //Secondo LED +int ledStateB = LOW; // ledState used to set the LED +long previousMillisB = 0; // will store last time LED was updated + +void setup() { + // set the digital pin as output: + pinMode(ledA, OUTPUT); + pinMode(ledB, OUTPUT); +} + +void loop() +{ + lightLedA(333); + lightLedB(500); +} + +////////////// +// Funzioni + +void lightLedA (int interval) { + // Illumina il ledA secondo un intervallo passato come argomento + + if (millis() > previousMillisA + interval) { + // save the last time you blinked the LED + previousMillisA = millis(); + + // if the LED is off turn it on and vice-versa: + if (ledStateA == LOW) + ledStateA = HIGH; + else + ledStateA = LOW; + // set the LED with the ledState of the variable: + digitalWrite(ledA, ledStateA); + } + +} + +void lightLedB (int interval) { + // Illumina il ledB secondo un intervallo passato come argomento + + if (millis() > previousMillisB + interval) { + // save the last time you blinked the LED + previousMillisB = millis(); + + // if the LED is off turn it on and vice-versa: + if (ledStateB == LOW) + ledStateB = HIGH; + else + ledStateB = LOW; + // set the LED with the ledState of the variable: + digitalWrite(ledB, ledStateB); + } +} + +/* Approfondimenti +- Quali similitudini ci sono tra le due funzioni? +- Distinguere i dati comuni tra le due funzioni che ci servono per + far lampeggiare i LED +- Distinguere i dati che caratterizzano un LED rispetto all'altro +- Provare a integrare le variabili relative ai due LED dentro le + rispettive funzioni. +- Sarebbe possibile scrivere una funzione che possa interagire con un LED qualunque? + ES: Come inpostare il PIN del LED? Come gestire lo stato del LED? +*/ + + + + + + + diff --git a/oggi/BlinkWithoutDelay_5_cleanup/BlinkWithoutDelay_5_cleanup.ino b/oggi/BlinkWithoutDelay_5_cleanup/BlinkWithoutDelay_5_cleanup.ino new file mode 100644 index 0000000..824c53f --- /dev/null +++ b/oggi/BlinkWithoutDelay_5_cleanup/BlinkWithoutDelay_5_cleanup.ino @@ -0,0 +1,80 @@ +/* Blink without Delay - Pulizia + +Semplificato il ciclo condizionale, la seconda funzione non necessita +di una variabile di stato per tracciare il LED. + + */ + +// constants won't change. Used here to +// set pin numbers: + +// First LED +const int ledA = 13; // the number of the LED pin +// Variables will change: +int ledStateA = LOW; // ledState used to set the LED +long previousMillisA = 0; // will store last time LED was updated + +// Second LED data +const int ledB = 12; //Secondo LED +// int ledStateB = LOW; // Possiamo leggere lo stato del registro del LED + // con digitalRead() +long previousMillisB = 0; // will store last time LED was updated + +void setup() { + // set the digital pin as output: + pinMode(ledA, OUTPUT); + pinMode(ledB, OUTPUT); +} + +void loop() +{ + lightLedA(1000); + lightLedB(500); +} + +//////////////// +// Funzioni + +void lightLedA (int interval) { + // Illumina il ledA secondo un intervallo passato come argomento + + if (millis() > previousMillisA + interval) { + // save the last time you blinked the LED + previousMillisA = millis(); + + // if the LED is off turn it on and vice-versa: + ledStateA = !ledStateA ; // Inverti il LED + } + digitalWrite(ledA, ledStateA); +} + +void lightLedB (int interval) { + // Illumina il ledB secondo un intervallo passato come argomento + + if (millis() > previousMillisB + interval) { + previousMillisB = millis(); + digitalWrite(ledB, !digitalRead(ledB)); + // Leggiamo direttamente il registro di ledB e scriviamo il suo opposto, + // questo ci permette di non dover avere una variabile per tracciare lo stato. + } +} +/* Domande: + 1. E' possibile avere una sola funzione che permetta di gestire + qualunque LED io voglia aggiungere? + +/* Approfondimenti + - integrazione tra funzioni e dati: programmazione a oggetti + - Uso di pointers per modificare dati esterni allo scope della funzione, static + - Uso di forme di dati strutturate (array, struct) per scambiare dati tra funzioni e programma + */ + + + + + + + + + + + diff --git a/oggi/BlinkWithoutDelay_6_class/BlinkWithoutDelay_6_class.ino b/oggi/BlinkWithoutDelay_6_class/BlinkWithoutDelay_6_class.ino new file mode 100644 index 0000000..9ce5e9b --- /dev/null +++ b/oggi/BlinkWithoutDelay_6_class/BlinkWithoutDelay_6_class.ino @@ -0,0 +1,62 @@ +/* Blink without Delay + Class: definizione di una classe LED. + + L'oggetto LED integra i dati (proprieta') del led con i metodi (le funzioni). + */ + +// Oggetti: +class Lampeggiatore { + // Lampeggia un LED utilizzando millis() + // Variabili + int ledPin ; // il numero del LED pin + int ledState ; // stato attuale del LED + long interval ; // milliseconds di intervallo nel lampeggiare + long previousMillis ; // precedente cambio di stato + + // Constructor: come viene instanziato un oggetto facente parte della classe +public: + Lampeggiatore(int pin, long time) + { + ledPin = pin; + pinMode(ledPin, OUTPUT); + ledState = LOW; + previousMillis = 0; + interval = time; + } + +// Una funzione facente parte di una classe prende il nome di "metodo" della stessa: + void Update () { + // Illumina il ledB secondo un intervallo passato come argomento + + if (millis() > previousMillis + interval) { + // save the last time you blinked the LED + previousMillis = millis(); + + // if the LED is off turn it on and vice-versa: + ledState = !ledState ; // Inverti il LED + } + // set the LED with the ledState of the variable: + digitalWrite(ledPin, ledState); + } + +}; + +// Instanziamo i due led dalla classe +Lampeggiatore ledA(13, 1000); +Lampeggiatore ledB(12, 500); + +void setup() { +} + +void loop() +{ +ledA.Update(); +ledB.Update(); +} + +/* Domande: + 1. Ogni quante volte viene eseguito il codice del loop per ogni millisecondo? + 2. Utilizzare un interrupt per richiamare Update() + Es: https://learn.adafruit.com/multi-tasking-the-arduino-part-2/overview + */ + diff --git a/oggi/blink_0/blink_0.ino b/oggi/blink_0/blink_0.ino new file mode 100644 index 0000000..96f6921 --- /dev/null +++ b/oggi/blink_0/blink_0.ino @@ -0,0 +1,31 @@ +/* + Blink + Turns on an LED on for one second, then off for one second, repeatedly. + + This example code is in the public domain. + */ + +// Pin 13 has an LED connected on most Arduino boards. +// give it a name: +int led = 13; + +// the setup routine runs once when you press reset: +void setup() { + // initialize the digital pin as an output. + pinMode(led, OUTPUT); +} + +// the loop routine runs over and over again forever: +void loop() { + digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) + delay(1000); // wait for a second + digitalWrite(led, LOW); // turn the LED off by making the voltage LOW + delay(1000); // wait for a second +} + +/* Domande + 1. Aggiungere un secondo LED e farlo brillare ogni 500ms + mentre il primo brilla ogni 1000ms + 2. Cosa succederebbe se dovessi anche leggere un input da un sensore / bottone? + */ + diff --git a/oggi/blink_0_soluzione/blink_0_soluzione.ino b/oggi/blink_0_soluzione/blink_0_soluzione.ino new file mode 100644 index 0000000..53aec5d --- /dev/null +++ b/oggi/blink_0_soluzione/blink_0_soluzione.ino @@ -0,0 +1,81 @@ +/* + Blink due LED - Soluzione + + Aggiungere un secondo LED e farlo brillare ogni 500ms + mentre il primo brilla ogni 1000ms + + Massimo comun denominatore 1000 MCD 500 = 500ms + Durata Periodo = 500ms + + + Stati: + + a | b Changes + ======== ========= + 1 | 1 x | x + 1 | 0 | x + 0 | 1 x | x + 0 | 0 | x + + + */ + +// Pin 13 has an LED connected on most Arduino boards. +// give it a name: +const int ledA = 13; //Primo LED +const int ledB = 12; //Secondo LED, con resistenza + +// the setup routine runs once when you press reset: +void setup() { + // initialize the digital pin as an output. + pinMode(ledA, OUTPUT); + pinMode(ledB, OUTPUT); +} + +// the loop routine runs over and over again forever: +void loop() { + // Primo periodo + digitalWrite(ledA, HIGH); // turn the LED on (HIGH is the voltage level) + digitalWrite(ledB, HIGH); + delay(500); // Minimo comun denominatore del periodo + + // Secondo periodo + //digitalWrite(ledA, HIGH); // ledA non cambia + digitalWrite(ledB, LOW); + delay(500); + + // Terzo periodo + digitalWrite(ledA, LOW); + digitalWrite(ledB, HIGH); + delay(500); + + // Quarto periodo + //digitalWrite(ledA, LOW); + digitalWrite(ledB, LOW); + delay(500); +} + +/* Domande + 1. Altro scenartio: fare brillare un LED ogni 300ms mentre il secondo brilla ogni 400m + 2. Aggiungere un terzo LED + */ + + + + + + + + + + + + + + + + + + + + diff --git a/oggi/loop_0_rider/loop_0_rider.ino b/oggi/loop_0_rider/loop_0_rider.ino new file mode 100644 index 0000000..1549668 --- /dev/null +++ b/oggi/loop_0_rider/loop_0_rider.ino @@ -0,0 +1,110 @@ +/* Knight Rider 1 + * -------------- + * + * Basically an extension of Blink_LED. + * + * + * (cleft) 2005 K3, Malmo University + * @author: David Cuartielles + * @hardware: David Cuartielles, Aaron Hallborg + See: https://www.arduino.cc/en/Tutorial/KnightRider + + Schema semplificato: + - http://lab.piffa.net/schemi/8_led_single_res_bb.png + - http://lab.piffa.net/schemi/8_led_single_res_schem.png + */ + +int pin2 = 2; +int pin3 = 3; +int pin4 = 4; +int pin5 = 5; +int pin6 = 6; +int pin7 = 7; +int pin8 = 8; +int pin9 = 9; +int timer = 100; + +void setup(){ + pinMode(pin2, OUTPUT); + pinMode(pin3, OUTPUT); + pinMode(pin4, OUTPUT); + pinMode(pin5, OUTPUT); + pinMode(pin6, OUTPUT); + pinMode(pin7, OUTPUT); + pinMode(pin8, OUTPUT); + pinMode(pin9, OUTPUT); +} + +void loop() { + digitalWrite(pin2, HIGH); + delay(timer); + digitalWrite(pin2, LOW); + delay(timer); + + digitalWrite(pin3, HIGH); + delay(timer); + digitalWrite(pin3, LOW); + delay(timer); + + digitalWrite(pin4, HIGH); + delay(timer); + digitalWrite(pin4, LOW); + delay(timer); + + digitalWrite(pin5, HIGH); + delay(timer); + digitalWrite(pin5, LOW); + delay(timer); + + digitalWrite(pin6, HIGH); + delay(timer); + digitalWrite(pin6, LOW); + delay(timer); + + digitalWrite(pin7, HIGH); + delay(timer); + digitalWrite(pin7, LOW); + delay(timer); + + digitalWrite(pin8, HIGH); + delay(timer); + digitalWrite(pin8, LOW); + delay(timer); + + digitalWrite(pin9, HIGH); + delay(timer); + digitalWrite(pin9, LOW); + delay(timer); + + // Ding! Mezzo giro + + digitalWrite(pin8, HIGH); + delay(timer); + digitalWrite(pin8, LOW); + delay(timer); + + digitalWrite(pin7, HIGH); + delay(timer); + digitalWrite(pin7, LOW); + delay(timer); + + digitalWrite(pin6, HIGH); + delay(timer); + digitalWrite(pin6, LOW); + delay(timer); + + digitalWrite(pin5, HIGH); + delay(timer); + digitalWrite(pin5, LOW); + delay(timer); + + digitalWrite(pin4, HIGH); + delay(timer); + digitalWrite(pin4, LOW); + delay(timer); + + digitalWrite(pin3, HIGH); + delay(timer); + digitalWrite(pin3, LOW); + delay(timer); +} diff --git a/oggi/loop_1_array_loop/loop_1_array_loop.ino b/oggi/loop_1_array_loop/loop_1_array_loop.ino new file mode 100644 index 0000000..bb9662e --- /dev/null +++ b/oggi/loop_1_array_loop/loop_1_array_loop.ino @@ -0,0 +1,77 @@ +/* Knight Rider 2 + * -------------- + * + * Array e uso dei cicli iterativi. + * + + + Schema semplificato: + - http://lab.piffa.net/schemi/8_led_single_res_bb.png + - http://lab.piffa.net/schemi/8_led_single_res_schem.png + */ + +int pinArray[8] = {2, 3, 4, 5, 6, 7, 8, 9}; +int timer = 100; + +void setup(){ + // we make all the declarations at once + for (int count=0;count<9;count++) { + pinMode(pinArray[count], OUTPUT); + } +} + +void loop() { + for (int count=0;count<8;count++) { // 8 e' un numero magico + digitalWrite(pinArray[count], HIGH); + delay(timer); + digitalWrite(pinArray[count], LOW); + delay(timer); + } + +// Ciclo inverso: dall'alto in basso + for (int count=8;count>=0;count--) { + digitalWrite(pinArray[count], HIGH); + delay(timer); + digitalWrite(pinArray[count], LOW); + delay(timer); + } +} + +/* Domande: + + 1. Come posso fare per saltare un elemento del loop? + 2. Come posso fare per uscire completamente dal loop? + 3. 8 e' un numero magico: come posso evitarlo? + +. +. +. +. +. +. +. +. +. +. +. +. +. +. +. +. +. +. +. +. +. +Soluzioni: + 1. utilizzare continue + 2. utilizzare break + 3. Utilizzare un variabile sarebbe gia' un inizio, ancora meglio estrarre il + valore tramite la funzione sizeof(). +Links: +- http://www.tutorialspoint.com/cprogramming/c_continue_statement.htm +- https://www.arduino.cc/en/Reference/Sizeof +*/ + + diff --git a/oggi/loop_2_array_loop_serial/loop_2_array_loop_serial.ino b/oggi/loop_2_array_loop_serial/loop_2_array_loop_serial.ino new file mode 100644 index 0000000..7591641 --- /dev/null +++ b/oggi/loop_2_array_loop_serial/loop_2_array_loop_serial.ino @@ -0,0 +1,58 @@ +/* + For Loop Iteration + + Demonstrates the use of a for() loop. + Lights multiple LEDs in sequence, then in reverse. + + The circuit: + * LEDs from pins 2 through 9 to ground + + Schemi: + - http://lab.piffa.net/schemi/8_led_single_res_bb.png + - http://lab.piffa.net/schemi/8_led_single_res_schem.png + + http://www.arduino.cc/en/Tutorial/ForLoop + */ + +byte ledPins[8] = { // Domanda: cosa succede se uso int? + 2,3,4,5,6,7,8,9} +; //Array +int timer = 100; // Pausa per far brillare i LED + +void setup() { + Serial.begin(9600); + // use a for loop to initialize each pin as an output: + for (int thisPin = 0; thisPin < sizeof(ledPins); thisPin++) { + pinMode(ledPins[thisPin], OUTPUT); + Serial.print("Inizializzato pin n. "); + Serial.println( thisPin); + } + + Serial.print("Dimesione array: "); + Serial.println(sizeof(ledPins)); +} + +void loop() { + // loop from the lowest pin to the highest: + for (int thisPin = 0; thisPin < sizeof(ledPins); thisPin++) { + Serial.print("Accensione pin n. "); + Serial.println(thisPin); + // turn the pin on: + digitalWrite(ledPins[thisPin], HIGH); + delay(timer); + // turn the pin off: + digitalWrite(ledPins[thisPin], LOW); + // Debug + + } + + // loop from the highest pin to the lowest: + for (int thisPin = sizeof(ledPins) -1 ; thisPin > 0; thisPin--) { + Serial.print("Accensione pin n. "); // Gli array sono indicizzati da 0 + Serial.println(thisPin); + digitalWrite(ledPins[thisPin], HIGH); + delay(timer); + digitalWrite(ledPins[thisPin], LOW); + + } +} -- 2.39.2