From 22e41500749cb15583e70d04aa6088196d77da06 Mon Sep 17 00:00:00 2001 From: eaman Date: Mon, 14 Nov 2016 07:03:38 +0100 Subject: [PATCH] 1st day clean up 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 | 46 ++--- oggi/blink_0_soluzione/blink_0_soluzione.ino | 81 --------- oggi/blink_1_variabili/blink_1_variabili.ino | 39 +++++ oggi/blink_2_1_sos/blink_2_1_sos.ino | 159 ++++++++++++++++++ .../blink_2_2_funzioni_argomenti.ino | 54 ++++++ oggi/blink_2_funzioni/blink_2_funzioni.ino | 65 +++++++ 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 ------- 15 files changed, 340 insertions(+), 829 deletions(-) delete mode 100644 oggi/BlinkWithoutDelay_1/BlinkWithoutDelay_1.ino delete mode 100644 oggi/BlinkWithoutDelay_2_led/BlinkWithoutDelay_2_led.ino delete mode 100644 oggi/BlinkWithoutDelay_3_funzione/BlinkWithoutDelay_3_funzione.ino delete mode 100644 oggi/BlinkWithoutDelay_4_argomento/BlinkWithoutDelay_4_argomento.ino delete mode 100644 oggi/BlinkWithoutDelay_5_cleanup/BlinkWithoutDelay_5_cleanup.ino delete mode 100644 oggi/BlinkWithoutDelay_6_class/BlinkWithoutDelay_6_class.ino delete mode 100644 oggi/blink_0_soluzione/blink_0_soluzione.ino create mode 100644 oggi/blink_1_variabili/blink_1_variabili.ino create mode 100644 oggi/blink_2_1_sos/blink_2_1_sos.ino create mode 100644 oggi/blink_2_2_funzioni_argomenti/blink_2_2_funzioni_argomenti.ino create mode 100644 oggi/blink_2_funzioni/blink_2_funzioni.ino delete mode 100644 oggi/loop_0_rider/loop_0_rider.ino delete mode 100644 oggi/loop_1_array_loop/loop_1_array_loop.ino delete 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 deleted file mode 100644 index 72a9694..0000000 --- a/oggi/BlinkWithoutDelay_1/BlinkWithoutDelay_1.ino +++ /dev/null @@ -1,72 +0,0 @@ -/* 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 deleted file mode 100644 index 2100ead..0000000 --- a/oggi/BlinkWithoutDelay_2_led/BlinkWithoutDelay_2_led.ino +++ /dev/null @@ -1,88 +0,0 @@ -/* 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 deleted file mode 100644 index e27cf55..0000000 --- a/oggi/BlinkWithoutDelay_3_funzione/BlinkWithoutDelay_3_funzione.ino +++ /dev/null @@ -1,93 +0,0 @@ -/* 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 deleted file mode 100644 index e2b28a9..0000000 --- a/oggi/BlinkWithoutDelay_4_argomento/BlinkWithoutDelay_4_argomento.ino +++ /dev/null @@ -1,85 +0,0 @@ -/* 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 deleted file mode 100644 index 824c53f..0000000 --- a/oggi/BlinkWithoutDelay_5_cleanup/BlinkWithoutDelay_5_cleanup.ino +++ /dev/null @@ -1,80 +0,0 @@ -/* 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 deleted file mode 100644 index 9ce5e9b..0000000 --- a/oggi/BlinkWithoutDelay_6_class/BlinkWithoutDelay_6_class.ino +++ /dev/null @@ -1,62 +0,0 @@ -/* 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 index 96f6921..f26f96d 100644 --- a/oggi/blink_0/blink_0.ino +++ b/oggi/blink_0/blink_0.ino @@ -1,31 +1,31 @@ +// 1. //////////// +// Commento iniziale multi linea /* - Blink - Turns on an LED on for one second, then off for one second, repeatedly. - - This example code is in the public domain. - */ + Blink -// Pin 13 has an LED connected on most Arduino boards. -// give it a name: -int led = 13; + Accensione e spegnimanto di un LED utilizzando variabili + per impostare la velocita' del lampeggio. + + */ + +// 2. ////////////// +// Dichiarazione variabili +// +int led = 13; // Il LED onboard corrisponde al PIN 13 + // Ha una resistenza premontata -// the setup routine runs once when you press reset: +// 3. ///////////////// +// Setup: eseguita una volta sola all'accensione della scheda void setup() { - // initialize the digital pin as an output. - pinMode(led, OUTPUT); + pinMode(led, OUTPUT); // Abilita il PIN del LED come OUTPUT in modo che + // possa essere acceso } -// the loop routine runs over and over again forever: +// 4. /////////////// +// loop: Le istruzioni vengono eseguite all'infinito 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 + digitalWrite(led, HIGH); // Mette il PIN del LED in stato acceso + delay(1000); // Aspetta un secondo (mille millisecondi) + digitalWrite(led, LOW); // Mette il PIN del LED in stato spento + delay(500); // Aspetta mezzo secondo } - -/* 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 deleted file mode 100644 index 53aec5d..0000000 --- a/oggi/blink_0_soluzione/blink_0_soluzione.ino +++ /dev/null @@ -1,81 +0,0 @@ -/* - 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/blink_1_variabili/blink_1_variabili.ino b/oggi/blink_1_variabili/blink_1_variabili.ino new file mode 100644 index 0000000..ec6c3c0 --- /dev/null +++ b/oggi/blink_1_variabili/blink_1_variabili.ino @@ -0,0 +1,39 @@ +// //////////// +// Commento iniziale +/* + Blink v1 + + Accensione e spegnimanto di un LED utilizzando variabili + per impostare la velocita' del lampeggio. + + */ + +// ////////////// +// Dichiarazione variabili + +// Pin 13 has an LED connected on most Arduino boards. +// give it a name: +int led = 13; +int breve = 200; // Variabile richiambile nel corso dell'esecuzione +int lunga = 1000; + +// ///////////////// +// Setup: eseguita una volta sola all'accensione della scheda +void setup() { + // initialize the digital pin as an output. + pinMode(led, OUTPUT); +} + +// /////////////// +// loop: Le istruzioni vengono eseguite all'infinito +void loop() { + digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) + delay(breve); // wait for a second + digitalWrite(led, LOW); // turn the LED off by making the voltage LOW + delay(breve); // wait for a second + + digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) + delay(lunga); // wait for a second + digitalWrite(led, LOW); // turn the LED off by making the voltage LOW + delay(lunga); +} diff --git a/oggi/blink_2_1_sos/blink_2_1_sos.ino b/oggi/blink_2_1_sos/blink_2_1_sos.ino new file mode 100644 index 0000000..aa53006 --- /dev/null +++ b/oggi/blink_2_1_sos/blink_2_1_sos.ino @@ -0,0 +1,159 @@ + +// //////////// +// Commento iniziale +/* + Blink v2 + + Accensione e spegnimanto di un LED utilizzando funzioni + per comandare il lampeggio. + + This example code is in the public domain. + */ +// ////////////// +// Dichiarazione variabili + +int led = 13; // LED onboard sulla scheda + + +// ///////////////// +// Setup: eseguita una volta sola all'accensione della scheda +void setup() { + // initialize the digital pin as an output. + pinMode(led, OUTPUT); +} + +// /////////////// +// loop: Le istruzioni vengono eseguite all'infinito +void loop() { + // S + rapido(); // accende e spegne rapidamente il LED + rapido(); + rapido(); + + // O + lento(); // accende e spegne lentamente il LED + lento(); + lento(); + + // S + rapido(); // accende e spegne rapidamente il LED + rapido(); + rapido(); + + + //Funzione apposita: + esse(); + ooo(); + esse(); +} + + + // /////////////// + // Funzioni create dall'utente: + +void rapido() { + // Accende e spegne rapidamente il LED + int breve = 200; + // sequenze di istruzione: accendere e spegnere il LED + digitalWrite(led, HIGH); // Mette il PIN del LED in stato acceso + delay(breve); // Pausa + digitalWrite(led, LOW); // Mette il PIN del LED in stato spento + delay(breve); // Pausa +} + +void lento() { + // Accende e spegne lentamente il LED + int lunga = 1000; + // sequenze di istruzione: accendere e spegnere il LED + digitalWrite(led, HIGH); // Mette il PIN del LED in stato spento) + delay(lunga); // Pausa + digitalWrite(led, LOW); // Mette il PIN del LED in stato spento + delay(lunga); // Pausa +} + +void esse() { + // Emette una lettera S in Morse + + int breve = 200; + + // sequenze di istruzione: accendere e spegnere il LED + digitalWrite(led, HIGH); // Mette il PIN del LED in stato acceso + delay(breve); // Pausa + digitalWrite(led, LOW); // Mette il PIN del LED in stato spento + delay(breve); // Pausa + + digitalWrite(led, HIGH); // Mette il PIN del LED in stato acceso + delay(breve); // Pausa + digitalWrite(led, LOW); // Mette il PIN del LED in stato spento + delay(breve); // Pausa + + digitalWrite(led, HIGH); // Mette il PIN del LED in stato acceso + delay(breve); // Pausa + digitalWrite(led, LOW); // Mette il PIN del LED in stato spento + delay(breve); // Pausa +} + +void ooo() { + // Emette una lettera o in Morse + + int lunga = 1000; + // sequenze di istruzione: accendere e spegnere il LED + digitalWrite(led, HIGH); // Mette il PIN del LED in stato spento) + delay(lunga); // Pausa + digitalWrite(led, LOW); // Mette il PIN del LED in stato spento + delay(lunga); // Pausa + + digitalWrite(led, HIGH); // Mette il PIN del LED in stato spento) + delay(lunga); // Pausa + digitalWrite(led, LOW); // Mette il PIN del LED in stato spento + delay(lunga); // Pausa + + digitalWrite(led, HIGH); // Mette il PIN del LED in stato spento) + delay(lunga); // Pausa + digitalWrite(led, LOW); // Mette il PIN del LED in stato spento + delay(lunga); // Pausa + +} + +void sos() { + // Emette un segnale di S.O.S + +} + +////////////////// +/* Domande: + 1. Che differenza c'e' tra le funzioni esse() e sos()? + + 2. Esercizio: creare uno sketch per far brillare un led 10 volte lentamente + e dieci volte rapidamente. + + Soluzioni a fondo pagina. + + + + + + + + + + + + + + + + + + + + + + + Risposta: + + 1. esse() e' una funzione autonoma, puo' essere copiata/incollata in qualunque + sketch e funzionera' autonomamente. + sos() invece e' composta da altre funzioni: anche queste dovranno essere disponibili. + + */ diff --git a/oggi/blink_2_2_funzioni_argomenti/blink_2_2_funzioni_argomenti.ino b/oggi/blink_2_2_funzioni_argomenti/blink_2_2_funzioni_argomenti.ino new file mode 100644 index 0000000..aa9074e --- /dev/null +++ b/oggi/blink_2_2_funzioni_argomenti/blink_2_2_funzioni_argomenti.ino @@ -0,0 +1,54 @@ + +/* + Blink v2 + + Accensione e spegnimanto di un LED utilizzando funzioni + per comandare il lampeggio. + + This example code is in the public domain. + */ + +// Dichiarazione variabili + +// Pin 13 has an LED connected on most Arduino boards. +// give it a name: +int led = 13; +// Le variabili lunga e breve non sono piu' necessarie + +// ///////////////// +// Setup +void setup() { + // initialize the digital pin as an output. + pinMode(led, OUTPUT); +} + +// loop +void loop() { + brilla(300); + brilla(300); + brilla(600); +} + +// Funzioni create dall'utente: + +void brilla(int velocita) { + // Accende e spegne il LED accetando un argomento + // per impostare la velocita'. + + // sequenze di istruzione: accendere e spegnere il LED + digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) + delay(velocita); // wait for a second + digitalWrite(led, LOW); // turn the LED off by making the voltage LOW + delay(velocita); // wait for a second +} + + + +/* Domande: + * 1. Come si potrebbe fare per poter utilizzare la funzione brilla + * con PIN diversi rispetto a LED? + * + * 2. velocita' ora e' una variabile o una costante ? + * + * Esercizi sucessivi sulle funzioni: blink_5 e 6 + */ diff --git a/oggi/blink_2_funzioni/blink_2_funzioni.ino b/oggi/blink_2_funzioni/blink_2_funzioni.ino new file mode 100644 index 0000000..92db22b --- /dev/null +++ b/oggi/blink_2_funzioni/blink_2_funzioni.ino @@ -0,0 +1,65 @@ + +// //////////// +// Commento iniziale +/* + Blink v2 + + Accensione e spegnimanto di un LED utilizzando funzioni + per comandare il lampeggio. + + This example code is in the public domain. + */ +// ////////////// +// Dichiarazione variabili + +int led = 13; // LED onboard sulla scheda +int breve = 200; // Variabile richiambile nel corso dell'esecuzione +int lunga = 1000; + +// ///////////////// +// Setup: eseguita una volta sola all'accensione della scheda +void setup() { + // initialize the digital pin as an output. + pinMode(led, OUTPUT); +} + +// /////////////// +// loop: Le istruzioni vengono eseguite all'infinito +void loop() { + rapido(); // accende e spegne rapidamente il LED + rapido(); // accende e spegne rapidamente il LED + lento(); // accende e spegne lentamente il LED +} + +// /////////////// +// Funzioni create dall'utente: + +void rapido() { + // Accende e spegne rapidamente il LED + + // sequenze di istruzione: accendere e spegnere il LED + digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) + delay(breve); // wait for a second + digitalWrite(led, LOW); // turn the LED off by making the voltage LOW + delay(breve); // wait for a second +} + +void lento() { + // Accende e spegne lentamente il LED + + // sequenze di istruzione: accendere e spegnere il LED + digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) + delay(lunga); // wait for a second + digitalWrite(led, LOW); // turn the LED off by making the voltage LOW + delay(lunga); +} + +/* Domande: + 1. I valori delle variabili led, breve, lunga cambiano durante + l'esecuzione del programma? Sono variabili? + + 2. Le dichiarazioni delle variabili breve e lunga possono essere + accorpate nelle rispettive funzioni? + + 3. Esercizio: creare una funzione per effettuare un S.O.S. : ...---... + */ diff --git a/oggi/loop_0_rider/loop_0_rider.ino b/oggi/loop_0_rider/loop_0_rider.ino deleted file mode 100644 index 1549668..0000000 --- a/oggi/loop_0_rider/loop_0_rider.ino +++ /dev/null @@ -1,110 +0,0 @@ -/* 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 deleted file mode 100644 index bb9662e..0000000 --- a/oggi/loop_1_array_loop/loop_1_array_loop.ino +++ /dev/null @@ -1,77 +0,0 @@ -/* 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 deleted file mode 100644 index 7591641..0000000 --- a/oggi/loop_2_array_loop_serial/loop_2_array_loop_serial.ino +++ /dev/null @@ -1,58 +0,0 @@ -/* - 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