From 0662b12752ee761efee7077b63c77f294e98deda Mon Sep 17 00:00:00 2001 From: Andrea Manni Date: Sun, 29 Oct 2017 20:54:48 +0100 Subject: [PATCH] Flasher con millis --- .gitignore | 1 + .../photo_6_smooth/photo_6_smooth.ino | 7 +- multitasking/millis/flasher/flasher.ino | 29 +++++++ multitasking/millis/zapper/blink/blink.ino | 82 ------------------- .../loops/loop_2_for_each/loop_2_for_each.ino | 25 ++++++ 5 files changed, 61 insertions(+), 83 deletions(-) create mode 100644 multitasking/millis/flasher/flasher.ino delete mode 100644 multitasking/millis/zapper/blink/blink.ino create mode 100644 programming/loops/loop_2_for_each/loop_2_for_each.ino diff --git a/.gitignore b/.gitignore index 8d8c367..71827a3 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ libraries/LedControl/ libraries/FreeRTOS/ libraries/Adafruit_HMC5883_Unified/ libraries/aero +libraries/AccelStepper diff --git a/basic/analog_input/photo_6_smooth/photo_6_smooth.ino b/basic/analog_input/photo_6_smooth/photo_6_smooth.ino index 60913fe..3f27170 100644 --- a/basic/analog_input/photo_6_smooth/photo_6_smooth.ino +++ b/basic/analog_input/photo_6_smooth/photo_6_smooth.ino @@ -64,9 +64,14 @@ int smoothRead(int sensorPin) { int total = 0; for (int i = 0; i < 10; i++) { total = total + analogRead(sensorPin); - delay(2); // Pausa per assestare il senstore + delay(2); // Pausa per assestare il sensore } return(total / 10); // Valore medio + /* Nota: i microcontroller non sono bravi a fare le divisioni, + sarebbe piu' efficiente ottenere 8 valori e fare uno shift dx. di 3 bit + total = total >> 3 ; +Vedi: https://www.arduino.cc/en/Reference/Bitshift + */ } diff --git a/multitasking/millis/flasher/flasher.ino b/multitasking/millis/flasher/flasher.ino new file mode 100644 index 0000000..ffc51b1 --- /dev/null +++ b/multitasking/millis/flasher/flasher.ino @@ -0,0 +1,29 @@ +/* Flasher + + Sequenza di lampeggi ad intervalli arbitrari + A ogni valore di intervallo viene fatto il toggle del PIN +*/ + +const int led = 13; + +unsigned long previousMillis = 0; // Ultimo aggiornamento +unsigned int intervalli[] = {100, 100, 100, 100, 500, 500} ; +unsigned int cur = 0 ; + +void setup() { + pinMode(led, OUTPUT); +} + +void loop() +{ + if (millis() - previousMillis >= intervalli[cur]) { + previousMillis += intervalli[cur] ; + digitalWrite(led, !digitalRead(led)); + + if (cur < sizeof(intervalli) / sizeof(intervalli[0]) -1 ) { + cur++; + } else { + cur = 0; + } + } +} diff --git a/multitasking/millis/zapper/blink/blink.ino b/multitasking/millis/zapper/blink/blink.ino deleted file mode 100644 index 9d52db1..0000000 --- a/multitasking/millis/zapper/blink/blink.ino +++ /dev/null @@ -1,82 +0,0 @@ -/* - Blink v1 - - Accensione e spegnimanto di un ledA utilizzando variabili - per impostare la velocita' del lampeggio. - - */ - -// Pin 13 ha un led collegato di default -int ledA = 6; -int ledB = 7; - -void setup() { - // Inizializziamo il PIN 13 come OUTPUT - pinMode(ledA, OUTPUT); - pinMode(ledB, OUTPUT); -} - -void loop() { - // 1st step - digitalWrite(ledA, HIGH); - delay(150); - digitalWrite(ledA, LOW); - delay(50); - digitalWrite(ledB, HIGH); - delay(150); - digitalWrite(ledB, LOW); - delay(50); - - // 2st step - digitalWrite(ledA, HIGH); - delay(150); - digitalWrite(ledA, LOW); - delay(50); - - - // 3st step - delay(150); - digitalWrite(ledA, LOW); - digitalWrite(ledB, LOW); - delay(50); - digitalWrite(ledA, HIGH); - digitalWrite(ledB, HIGH); - delay(350); - digitalWrite(ledA, LOW); - digitalWrite(ledB, LOW); - delay(50); - - - // Reverse - // 1st step - digitalWrite(ledB, HIGH); - delay(150); - digitalWrite(ledB, LOW); - delay(50); - digitalWrite(ledA, HIGH); - delay(150); - digitalWrite(ledA, LOW); - delay(50); - - // 2st step - digitalWrite(ledB, HIGH); - delay(150); - digitalWrite(ledB, LOW); - delay(50); - - - // 3st step - delay(150); - digitalWrite(ledB, LOW); - digitalWrite(ledA, LOW); - delay(50); - digitalWrite(ledB, HIGH); - digitalWrite(ledA, HIGH); - delay(350); - digitalWrite(ledB, LOW); - digitalWrite(ledA, LOW); - delay(50); - - -} - diff --git a/programming/loops/loop_2_for_each/loop_2_for_each.ino b/programming/loops/loop_2_for_each/loop_2_for_each.ino new file mode 100644 index 0000000..f9aba07 --- /dev/null +++ b/programming/loops/loop_2_for_each/loop_2_for_each.ino @@ -0,0 +1,25 @@ +/* + For Each Iteration + + Il costrutto for each fa parte delle specifiche di C++11 + abilitate di default nella Arduino IDE dalla versione 1.6.6. + + Se state usando una versione piu' vecchia dell'IDE + il codice non compilera', se avete comunque le GCC >= 4.7 + potete abilitare c++11 con i flag -std=c++11 : + https://stackoverflow.com/questions/16224746/how-to-use-c11-to-program-the-arduino +*/ + +byte valori[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + +void setup() +{ + Serial.begin(9600); +} + +void loop() { + for (int val: valori) { + Serial.println(val); + delay(100); + } +} -- 2.39.2