From f794af5de04c45dbfab7f52ebb49315f72b9fc9f Mon Sep 17 00:00:00 2001 From: Andrea Manni Date: Mon, 27 Apr 2015 18:59:06 +0200 Subject: [PATCH] multitasking --- RGB_LED/rgb_0/rgb_0.ino | 14 ++- RGB_LED/rgb_0_soluzione/rgb_0_soluzione.ino | 3 +- RGB_LED/rgb_1_all_color/rgb_1_all_color.ino | 103 +++++++++--------- .../BlinkWithoutDelay_1.ino | 69 ++++++++++++ .../BlinkWithoutDelay_2_led.ino | 91 ++++++++++++++++ multitasking/blink_0/blink_0.ino | 31 ++++++ .../blink_0_soluzione/blink_0_soluzione.ino | 60 ++++++++++ .../state_3_one_led/state_3_one_led.ino | 46 ++++++++ .../state_4_one_led_two_timers.ino | 49 +++++++++ .../state_5_two_led_two_timers.ino | 77 +++++++++++++ .../debug_1_preprocessor_simple.ino | 46 ++++++++ .../debug_2_preprocessor_advanced.ino} | 0 12 files changed, 532 insertions(+), 57 deletions(-) create mode 100644 multitasking/BlinkWithoutDelay_1/BlinkWithoutDelay_1.ino create mode 100644 multitasking/BlinkWithoutDelay_2_led/BlinkWithoutDelay_2_led.ino create mode 100644 multitasking/blink_0/blink_0.ino create mode 100644 multitasking/blink_0_soluzione/blink_0_soluzione.ino create mode 100644 multitasking/state_3_one_led/state_3_one_led.ino create mode 100644 multitasking/state_4_one_led_two_timers/state_4_one_led_two_timers.ino create mode 100644 multitasking/state_5_two_led_two_timers/state_5_two_led_two_timers.ino create mode 100644 serial/debug_1_preprocessor_simple/debug_1_preprocessor_simple.ino rename serial/{debug_preprocessor/debug_preprocessor.ino => debug_2_preprocessor_advanced/debug_2_preprocessor_advanced.ino} (100%) diff --git a/RGB_LED/rgb_0/rgb_0.ino b/RGB_LED/rgb_0/rgb_0.ino index ea32ca6..2290582 100644 --- a/RGB_LED/rgb_0/rgb_0.ino +++ b/RGB_LED/rgb_0/rgb_0.ino @@ -28,14 +28,18 @@ void loop() } /* Domande: + 1. Come scrivere le istruzioni analog Write in modo da sottrarre i valori? + 2. Accendere il LED nei vari colori + - http://i.stack.imgur.com/LcBvQ.gif + Soluzione: vedi lo sketch rgb_1_all_color - 1. Accendere il LED nei vari colori - 2. Come scrivere le istruzioni analog Write in modo da sottrarre i valori? 3. Scrivere una funzione che accetti 3 parametri per impostare i colori - 4. Scrivere una funzione che accetti i colori in esadecimale - - http://www.yellowpipe.com/yis/tools/hex-to-rgb/color-converter.php - 5. Scrivere una funzione che accetti come parametro il nome del colore + 4. Scrivere una funzione che accetti come parametro il nome del colore es "blue" e imposti il LED. + + Eventuale: + 5. Scrivere una funzione che accetti i colori in esadecimale + - http://www.yellowpipe.com/yis/tools/hex-to-rgb/color-converter.php */ diff --git a/RGB_LED/rgb_0_soluzione/rgb_0_soluzione.ino b/RGB_LED/rgb_0_soluzione/rgb_0_soluzione.ino index 475de32..b9fb952 100644 --- a/RGB_LED/rgb_0_soluzione/rgb_0_soluzione.ino +++ b/RGB_LED/rgb_0_soluzione/rgb_0_soluzione.ino @@ -20,7 +20,8 @@ void setup() void loop() { - setColor(0xFF,0x00,0x00) ; // imposta il LED in rosso + setColor(255,0,0) ; // imposta il LED in rosso + //setColor(0xFF,0x00,0x00) ; // imposta il LED in rosso in esadecimale // setName("green") ; } diff --git a/RGB_LED/rgb_1_all_color/rgb_1_all_color.ino b/RGB_LED/rgb_1_all_color/rgb_1_all_color.ino index 546d636..b88d0da 100644 --- a/RGB_LED/rgb_1_all_color/rgb_1_all_color.ino +++ b/RGB_LED/rgb_1_all_color/rgb_1_all_color.ino @@ -1,53 +1,54 @@ - /* +/* Adafruit Arduino - Lesson 3. RGB LED - - RGB LED: rotazione tra tutti i colori. - - Schema: http://lab.piffa.net/schemi/rgb.jpg - - */ - - int redPin = 11; - int greenPin = 10; - int bluePin = 9; - - //uncomment this line if using a Common Anode LED - //#define COMMON_ANODE - - void setup() - { - pinMode(redPin, OUTPUT); - pinMode(greenPin, OUTPUT); - pinMode(bluePin, OUTPUT); - } - - void loop() - { - setColor(255, 0, 0); // red - delay(1000); - setColor(0, 255, 0); // green - delay(1000); - setColor(0, 0, 255); // blue - delay(1000); - setColor(255, 255, 0); // yellow - delay(1000); - setColor(80, 0, 80); // purple - delay(1000); - setColor(0, 255, 255); // aqua - delay(1000); - } - - void setColor(int red, int green, int blue) - { - #ifdef COMMON_ANODE - red = 255 - red; - green = 255 - green; - blue = 255 - blue; - #endif - analogWrite(redPin, red); - analogWrite(greenPin, green); - analogWrite(bluePin, blue); - } - - + + RGB LED: rotazione tra tutti i colori. + + Schema: http://lab.piffa.net/schemi/rgb.jpg + + */ + +int redPin = 11; +int greenPin = 10; +int bluePin = 9; + +//uncomment this line if using a Common Anode LED +//#define COMMON_ANODE + +void setup() +{ + pinMode(redPin, OUTPUT); + pinMode(greenPin, OUTPUT); + pinMode(bluePin, OUTPUT); +} + +void loop() +{ + setColor(255, 0, 0); // red + delay(1000); + setColor(0, 255, 0); // green + delay(1000); + setColor(0, 0, 255); // blue + delay(1000); + setColor(255, 255, 0); // yellow + delay(1000); + setColor(80, 0, 80); // purple + delay(1000); + setColor(0, 255, 255); // aqua + delay(1000); +} + +void setColor(int red, int green, int blue) +{ +#ifdef COMMON_ANODE + red = 255 - red; + green = 255 - green; + blue = 255 - blue; +#endif + analogWrite(redPin, red); + analogWrite(greenPin, green); + analogWrite(bluePin, blue); +} + + + diff --git a/multitasking/BlinkWithoutDelay_1/BlinkWithoutDelay_1.ino b/multitasking/BlinkWithoutDelay_1/BlinkWithoutDelay_1.ino new file mode 100644 index 0000000..bbed9de --- /dev/null +++ b/multitasking/BlinkWithoutDelay_1/BlinkWithoutDelay_1.ino @@ -0,0 +1,69 @@ +/* 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 + + 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; // the number of the LED pin + +// 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. +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. + unsigned long currentMillis = millis(); + + if(currentMillis - previousMillis > interval) { + // save the last time you blinked the LED + previousMillis = currentMillis; + + // if the LED is off turn it on and vice-versa: + if (ledState == LOW) + ledState = HIGH; + else + ledState = LOW; + + // set the LED with the ledState of the variable: + digitalWrite(ledPin, ledState); + } +} + +/* Domande + 1. Aggioungere un LED che brilli ogni 500ms + 2. E' possibile cambiare gli intervalli dei due LED? + */ diff --git a/multitasking/BlinkWithoutDelay_2_led/BlinkWithoutDelay_2_led.ino b/multitasking/BlinkWithoutDelay_2_led/BlinkWithoutDelay_2_led.ino new file mode 100644 index 0000000..fa2c35c --- /dev/null +++ b/multitasking/BlinkWithoutDelay_2_led/BlinkWithoutDelay_2_led.ino @@ -0,0 +1,91 @@ +/* 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 + + 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; // the number of the LED pin +int ledB = 12; //Secondo LED + +// Variables will change: +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() +{ + // 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. + unsigned long currentMillis = millis(); + + if(currentMillis - previousMillisA > intervalA) { + // save the last time you blinked the LED + previousMillisA = currentMillis; + + // 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); + } + + if(currentMillis - previousMillisB > intervalB) { + // save the last time you blinked the LED + previousMillisB = currentMillis; + + // 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 + 2. Inserire un secondo LED con intervallo 500ms + 1. Trasformare il codice utilizzato in una State Machine + + */ + + + diff --git a/multitasking/blink_0/blink_0.ino b/multitasking/blink_0/blink_0.ino new file mode 100644 index 0000000..96f6921 --- /dev/null +++ b/multitasking/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/multitasking/blink_0_soluzione/blink_0_soluzione.ino b/multitasking/blink_0_soluzione/blink_0_soluzione.ino new file mode 100644 index 0000000..b1ba27a --- /dev/null +++ b/multitasking/blink_0_soluzione/blink_0_soluzione.ino @@ -0,0 +1,60 @@ +/* + 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 ledA = 13; //Primo LED +int ledB = 12; //Secondo LED + +// 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() { + digitalWrite(ledA, HIGH); // turn the LED on (HIGH is the voltage level) + digitalWrite(ledB, HIGH); + + delay(500); + digitalWrite(ledB, LOW); + + delay(500); + digitalWrite(ledA, LOW); + digitalWrite(ledB, HIGH); + + + delay(500); + digitalWrite(ledB, LOW); + + delay(500); + digitalWrite(ledA, LOW); + digitalWrite(ledB, LOW); + ; +} + +/* Domande + 1. Aggiungere un secondo LED e farlo brillare ogni 500ms + mentre il primo brilla ogni 1000ms + */ + + + + + + + + + + + + + + + diff --git a/multitasking/state_3_one_led/state_3_one_led.ino b/multitasking/state_3_one_led/state_3_one_led.ino new file mode 100644 index 0000000..1b5717b --- /dev/null +++ b/multitasking/state_3_one_led/state_3_one_led.ino @@ -0,0 +1,46 @@ +/* Blink con state machine + + Accendere e spegnere un led utilizzando una state machine +*/ + +// These variables store the flash pattern +// and the current state of the LED + +int ledPin = 13; // the number of the LED pin +int ledState = LOW; // ledState used to set the LED +unsigned long previousMillis = 0; // will store last time LED was updated +long interval = 1000; // + +void setup() +{ + // set the digital pin as output: + pinMode(ledPin, OUTPUT); +} + +void loop() +{ + // check to see if it's time to change the state of the LED + unsigned long currentMillis = millis(); + if((ledState == HIGH) && (currentMillis - previousMillis >= interval)) + { + ledState = !ledState ; // Inverti il LED + previousMillis = currentMillis; // Remember the time + digitalWrite(ledPin, ledState); // Update the actual LED + + } + else if((ledState == LOW) && (currentMillis - previousMillis >= interval)) + { + ledState = !ledState ; // Inverti il LED + previousMillis = currentMillis; // Remember the time + digitalWrite(ledPin, ledState); // Update the actual LED + } + + +} + +/* +1. Modificare il codice in modo che si possa precisare il tempo di HIGH + e il tempo di LOW +2. Aggiungere un LED che brilli ogni 500ms + */ + diff --git a/multitasking/state_4_one_led_two_timers/state_4_one_led_two_timers.ino b/multitasking/state_4_one_led_two_timers/state_4_one_led_two_timers.ino new file mode 100644 index 0000000..1f696cb --- /dev/null +++ b/multitasking/state_4_one_led_two_timers/state_4_one_led_two_timers.ino @@ -0,0 +1,49 @@ +/* Blink con state machine + + Accendere e spegnere un led utilizzando una state machine + specificando sia il tempo accensione che di spegnimento. + +*/ + +// These variables store the flash pattern +// and the current state of the LED + +int ledPin = 13; // the number of the LED pin +int ledState = LOW; // ledState used to set the LED +unsigned long previousMillis = 0; // will store last time LED was updated +long intervalHigh = 1000; // +long intervalLow = 200; // + +void setup() +{ + // set the digital pin as output: + pinMode(ledPin, OUTPUT); +} + +void loop() +{ + // check to see if it's time to change the state of the LED + unsigned long currentMillis = millis(); + if((ledState == HIGH) && (currentMillis - previousMillis >= intervalHigh)) + { + ledState = !ledState ; // Inverti il LED + previousMillis = currentMillis; // Remember the time + digitalWrite(ledPin, ledState); // Update the actual LED + + } + else if((ledState == LOW) && (currentMillis - previousMillis >= intervalLow)) + { + ledState = !ledState ; // Inverti il LED + previousMillis = currentMillis; // Remember the time + digitalWrite(ledPin, ledState); // Update the actual LED + } + + +} + +/* +1. Modificare il codice in modo che si possa precisare il tempo di HIGH + e il tempo di LOW +2. Aggiungere un LED che brilli ogni 500ms + */ + diff --git a/multitasking/state_5_two_led_two_timers/state_5_two_led_two_timers.ino b/multitasking/state_5_two_led_two_timers/state_5_two_led_two_timers.ino new file mode 100644 index 0000000..174f875 --- /dev/null +++ b/multitasking/state_5_two_led_two_timers/state_5_two_led_two_timers.ino @@ -0,0 +1,77 @@ +/* Blink con state machine: due led + + Accendere e spegnere due led utilizzando una state machine + specificando sia il tempo accensione che di spegnimento. + + */ + +// These variables store the flash pattern +// and the current state of the LED + +int ledPinA = 13; // the number of the LED pin +int ledStateA = LOW; // ledState used to set the LED +unsigned long previousMillisA = 0; // will store last time LED was updated +long intervalHighA = 1000; // +long intervalLowA = 200; // + +int ledPinB = 12; // the number of the LED pin +int ledStateB = LOW; // ledState used to set the LED +unsigned long previousMillisB = 0; // will store last time LED was updated +long intervalHighB = 500; // +long intervalLowB = 100; // + +void setup() +{ + // set the digital pin as output: + pinMode(ledPinA, OUTPUT); + pinMode(ledPinB, OUTPUT); +} + +void loop() +{ + // Primo LED + // check to see if it's time to change the state of the LED + unsigned long currentMillis = millis(); + if((ledStateA == HIGH) && (currentMillis - previousMillisA >= intervalHighA)) + { + ledStateA = 1 - ledStateA ; // Inverti il LED + previousMillisA = currentMillis; // Remember the time + digitalWrite(ledPinA, ledStateA); // Update the actual LED + + } + else if((ledStateA == LOW) && (currentMillis - previousMillisA >= intervalLowA)) + { + ledStateA = 1 - ledStateA ; // Inverti il LED + previousMillisA = currentMillis; // Remember the time + digitalWrite(ledPinA, ledStateA); // Update the actual LED + } + +// Secondo LED + + // check to see if it's time to change the state of the LED + unsigned long currentMillis = millis(); + if((ledStateB == HIGH) && (currentMillis - previousMillisB >= intervalHighB)) + { + ledStateB = 1 - ledStateB ; // Inverti il LED + previousMillisB = currentMillis; // Remember the time + digitalWrite(ledPinB, ledStateB); // Update the actual LED + + } + else if((ledStateB == LOW) && (currentMillis - previousMillisB >= intervalLowB)) + { + ledStateB = 1 - ledStateB ; // Inverti il LED + previousMillisB = currentMillis; // Remember the time + digitalWrite(ledPinA, ledStateB); // Update the actual LED + } +} + +/* Domande + 1. E' possibile razzinalizzare il codice utilizzando una funzione? + + + Link: + - Utilizzare codice a oggetti per ottimizzare il codice: + https://learn.adafruit.com/multi-tasking-the-arduino-part-1?view=all#a-classy-solution + */ + + diff --git a/serial/debug_1_preprocessor_simple/debug_1_preprocessor_simple.ino b/serial/debug_1_preprocessor_simple/debug_1_preprocessor_simple.ino new file mode 100644 index 0000000..0461985 --- /dev/null +++ b/serial/debug_1_preprocessor_simple/debug_1_preprocessor_simple.ino @@ -0,0 +1,46 @@ +/* + Debug con macro per il preprocessore + + Blink v1 + + Accensione e spegnimanto di un LED utilizzando variabili + per impostare la velocita' del lampeggio. + + 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; +int breve = 200; // Variabile richiambile nel corso dell'esecuzione + +#define DEBUG + + +// the setup routine runs once when you press reset: +void setup() { + // initialize the digital pin as an output. + pinMode(led, OUTPUT); + Serial.begin(9600); +} + +// the loop routine runs over and over again forever: +void loop() { + digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) +#ifdef DEBUG + Serial.println("High"); +#endif + + delay(breve); // wait for a second + digitalWrite(led, LOW); // turn the LED off by making the voltage LOW +#ifdef DEBUG + Serial.println("Low"); +#endif + delay(breve); // wait for a second +} + + + + diff --git a/serial/debug_preprocessor/debug_preprocessor.ino b/serial/debug_2_preprocessor_advanced/debug_2_preprocessor_advanced.ino similarity index 100% rename from serial/debug_preprocessor/debug_preprocessor.ino rename to serial/debug_2_preprocessor_advanced/debug_2_preprocessor_advanced.ino -- 2.39.2