]> git.piffa.net Git - sketchbook_andrea/commitdiff
Loop e data type strutturati
authorAndrea Manni <andrea@piffa.net>
Thu, 13 Apr 2017 16:58:09 +0000 (18:58 +0200)
committerAndrea Manni <andrea@piffa.net>
Thu, 13 Apr 2017 16:58:09 +0000 (18:58 +0200)
RGB_LED/rgb_5_struct/rgb_5_struct.ino
programming/loops/loop_1_array_loop/loop_1_array_loop.ino
programming/loops/loop_2_array_loop_serial/loop_2_array_loop_serial.ino
programming/loops/loop_3_multi_led_random/loop_3_multi_led_random.ino
programming/structured_data_types/array_loop/array_loop.ino [new file with mode: 0644]
programming/structured_data_types/oggetto_rgb/oggetto_rgb.ino [new file with mode: 0644]
programming/structured_data_types/struct/struct.ino [new file with mode: 0644]

index dbe0d05c80be350280a4e6513204776775f7865c..181a90dbb2585b02ffe3d8e734e7861a748824ab 100644 (file)
@@ -18,7 +18,6 @@ struct color {
 
 color led;
 
 
 color led;
 
-
 void setup()
 {
   for (byte i = 0; i < 4; i++) {
 void setup()
 {
   for (byte i = 0; i < 4; i++) {
@@ -26,7 +25,6 @@ void setup()
   }
 }
 
   }
 }
 
-
 void loop()
 {
   analogWrite(pin[0], led.red);
 void loop()
 {
   analogWrite(pin[0], led.red);
@@ -60,15 +58,13 @@ struct ledRGB {
     byte    red;
 };
 
     byte    red;
 };
 
-ledRGB led {0,255,255,9,10,11};
+ledRGB led {9,10,11,0,255,255};
    
    
-   
-
 void setup()
 {
 void setup()
 {
-  pinMode(led.Pin, OUTPUT);
-  pinMode(led.Pin, OUTPUT);
-  pinMode(led.Pin, OUTPUT);
+  pinMode(led.rPin, OUTPUT);
+  pinMode(led.gPin, OUTPUT);
+  pinMode(led.bPin, OUTPUT);
 }
 
 void loop()
 }
 
 void loop()
index bb9662ecd1b1eaa0a60348ee77bbf2c5b064681d..002650881e38b2eda86d2ae652831767a38c36dd 100644 (file)
@@ -1,11 +1,11 @@
 /* Knight Rider 2
  * --------------
 /* Knight Rider 2
  * --------------
- * 
+ *
  * Array e uso dei cicli iterativi.
  *
 
 
  * Array e uso dei cicli iterativi.
  *
 
 
-   Schema semplificato: 
+   Schema semplificato:
    - http://lab.piffa.net/schemi/8_led_single_res_bb.png
    - http://lab.piffa.net/schemi/8_led_single_res_schem.png
  */
    - 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;
 
 int pinArray[8] = {2, 3, 4, 5, 6, 7, 8, 9};
 int timer = 100;
 
-void setup(){
+void setup() {
   // we make all the declarations at once
   // we make all the declarations at once
-  for (int count=0;count<9;count++) {
+  for (int count = 0; count < 9; count++) {
     pinMode(pinArray[count], OUTPUT);
   }
 }
 
 void loop() {
     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);
+  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);
+  // 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:
   }
 }
 
 /* Domande:
- 1. Come posso fare per saltare un elemento del loop? 
- 2. Come posso fare per uscire completamente dal loop? 
+
+ 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?
 
 .
  3. 8 e' un numero magico: come posso evitarlo?
 
 .
@@ -64,12 +64,12 @@ void loop() {
 .
 .
 .
 .
 .
 .
-Soluzioni: 
+Soluzioni:
  1. utilizzare continue
  2. utilizzare break
  3. Utilizzare un variabile sarebbe gia' un inizio, ancora meglio estrarre il
     valore tramite la funzione sizeof().
  1. utilizzare continue
  2. utilizzare break
  3. Utilizzare un variabile sarebbe gia' un inizio, ancora meglio estrarre il
     valore tramite la funzione sizeof().
-Links: 
+Links:
 - http://www.tutorialspoint.com/cprogramming/c_continue_statement.htm
 - https://www.arduino.cc/en/Reference/Sizeof
 */
 - http://www.tutorialspoint.com/cprogramming/c_continue_statement.htm
 - https://www.arduino.cc/en/Reference/Sizeof
 */
index 7591641437c6e15e4d9a698bc474a85e8e4f8137..287628af8940dd7d61529bea3fea13b9fc1519d6 100644 (file)
@@ -1,22 +1,22 @@
 /*
   For Loop Iteration
 /*
   For Loop Iteration
+
  Demonstrates the use of a for() loop.
  Lights multiple LEDs in sequence, then in reverse.
  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
  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
  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?
  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
+  2, 3, 4, 5, 6, 7, 8, 9
+  } ; //Array
 int timer = 100;           // Pausa per far brillare i LED
 
 void setup() {
 int timer = 100;           // Pausa per far brillare i LED
 
 void setup() {
@@ -25,7 +25,7 @@ void setup() {
   for (int thisPin = 0; thisPin < sizeof(ledPins); thisPin++)  {
     pinMode(ledPins[thisPin], OUTPUT);
     Serial.print("Inizializzato pin n. ");
   for (int thisPin = 0; thisPin < sizeof(ledPins); thisPin++)  {
     pinMode(ledPins[thisPin], OUTPUT);
     Serial.print("Inizializzato pin n. ");
-    Serial.println(  thisPin);
+    Serial.println(thisPin);
   }
 
   Serial.print("Dimesione array: ");
   }
 
   Serial.print("Dimesione array: ");
@@ -38,16 +38,16 @@ void loop() {
     Serial.print("Accensione pin n. ");
     Serial.println(thisPin);
     // turn the pin on:
     Serial.print("Accensione pin n. ");
     Serial.println(thisPin);
     // turn the pin on:
-    digitalWrite(ledPins[thisPin], HIGH);  
-    delay(timer);                  
+    digitalWrite(ledPins[thisPin], HIGH);
+    delay(timer);
     // turn the pin off:
     // turn the pin off:
-    digitalWrite(ledPins[thisPin], LOW);    
+    digitalWrite(ledPins[thisPin], LOW);
     // Debug
 
   }
 
   // loop from the highest pin to the lowest:
     // Debug
 
   }
 
   // loop from the highest pin to the lowest:
-  for (int thisPin = sizeof(ledPins) -1 ; thisPin > 0; thisPin--) {
+  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);
     Serial.print("Accensione pin n. "); // Gli array sono indicizzati da 0
     Serial.println(thisPin);
     digitalWrite(ledPins[thisPin], HIGH);
index 70cfc625b573c00f0f9cea39764544351f5f0f7e..71e83a1094b9929d70962f250d85ceab59b99c48 100644 (file)
@@ -17,8 +17,8 @@
  */
 
 byte ledPins[8] = {  // Domanda: cosa succede se uso int?
  */
 
 byte ledPins[8] = {  // Domanda: cosa succede se uso int?
-  2,3,4,5,6,7,8,9
-; //Array
+  2,3,4,5,6,7,8,9
+  }; //Array
 int timer = 100;           // Pausa per far brillare i LED
 int randNumber ;
 
 int timer = 100;           // Pausa per far brillare i LED
 int randNumber ;
 
@@ -33,12 +33,13 @@ void setup() {
 
   Serial.print("Dimesione array: ");
   Serial.println(sizeof(ledPins));
 
   Serial.print("Dimesione array: ");
   Serial.println(sizeof(ledPins));
+  
   randomSeed(analogRead(0));  // Rilevazione di un valore esterno
  // per scegliere il primo elemento del pseudorandom generators
 }
 
 void loop() {
   randomSeed(analogRead(0));  // Rilevazione di un valore esterno
  // per scegliere il primo elemento del pseudorandom generators
 }
 
 void loop() {
-  // print a random number from 0 to 8
+  // print a random number from 0 to 7
   randNumber = random(8);
   // turn the pin on:
   Serial.print("Accensione pin  n. ");
   randNumber = random(8);
   // turn the pin on:
   Serial.print("Accensione pin  n. ");
@@ -60,14 +61,13 @@ void rainbow() {
   // Esegue un pattern con i led
   
   Serial.println(">>> Rainbow! <<<");
   // Esegue un pattern con i led
   
   Serial.println(">>> Rainbow! <<<");
+    // loop from the lowest pin to the highest:
   for (int thisPin = 0; thisPin < sizeof(ledPins); thisPin++) {
     // turn the pin on:
     digitalWrite(ledPins[thisPin], HIGH);  
     delay(timer / 2);                  
     // turn the pin off:
     digitalWrite(ledPins[thisPin], LOW);    
   for (int thisPin = 0; thisPin < sizeof(ledPins); thisPin++) {
     // turn the pin on:
     digitalWrite(ledPins[thisPin], HIGH);  
     delay(timer / 2);                  
     // turn the pin off:
     digitalWrite(ledPins[thisPin], LOW);    
-    // Debug
-
   }
 
   // loop from the highest pin to the lowest:
   }
 
   // loop from the highest pin to the lowest:
diff --git a/programming/structured_data_types/array_loop/array_loop.ino b/programming/structured_data_types/array_loop/array_loop.ino
new file mode 100644 (file)
index 0000000..0026508
--- /dev/null
@@ -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/programming/structured_data_types/oggetto_rgb/oggetto_rgb.ino b/programming/structured_data_types/oggetto_rgb/oggetto_rgb.ino
new file mode 100644 (file)
index 0000000..9176f62
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+    RGB Object
+
+    Un oggetto puo' contenere tutte le proprieta' di un elemento
+    assieme ai metodi (le funzioni) che lo caratterizzano.
+
+    In aggiunta il constructor e' una funzione richiamata al momento
+    di instanziare l'oggetto che puo' essere usata per attivare PIN,
+    assegnare variabili.
+
+
+ Schema: http://lab.piffa.net/schemi/rgb.jpg
+ */
+
+class RGBLed {
+  // Classe rappresentativa di un LED RGB
+  
+    // Private properties, le proprieta' sono private per default
+    byte redPin ;
+    byte greenPin ;
+    byte bluePin ;
+
+  public:
+    // Constructor: come viene instanziato un oggetto facente parte della classe
+    RGBLed(byte pinR, byte pinG, byte pinB)
+    {
+      // Carichiamo i valori dei PIN dentro alle proprieta'
+      redPin    = pinR ;
+      greenPin  = pinG ;
+      bluePin   = pinB ;
+
+      // Equivalente del Setup() per inizializzare i PIN
+      pinMode(redPin, OUTPUT);
+      pinMode(greenPin, OUTPUT);
+      pinMode(greenPin, OUTPUT);
+    }
+
+    void Arrossa () {
+      // Metodo = funzione dell'oggetto
+      // Imposta il colore di un LED RGB a rosso
+
+      analogWrite(redPin,   0);
+      analogWrite(greenPin, 255);
+      analogWrite(bluePin,  255);
+    }
+
+    void SetColor (byte r, byte g, byte b) {
+      // Imposta il colore di un LED RGB
+
+      analogWrite(redPin,   r);
+      analogWrite(greenPin, g);
+      analogWrite(bluePin,  b);
+    }
+};
+
+// Instanziamo un LED
+RGBLed led(11, 10, 9);
+/* L'oggetto viene istanziato qui e non nella funzione di setup()
+    perche' altrimenti la sua esistenza sarebbe legata solo
+    al contesto (scope) del setup(), non sarebbe disponibile nel loop()
+ */
+
+void setup()  {
+  // I PIN mode vengono settati dal constructor
+}
+
+void loop() {
+  led.Arrossa();
+  delay(1000);
+  led.SetColor(255, 0, 255) ; // Mettiamo il LED in Green
+  delay(1000);
+}
+
diff --git a/programming/structured_data_types/struct/struct.ino b/programming/structured_data_types/struct/struct.ino
new file mode 100644 (file)
index 0000000..6dd619d
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+    struct 
+
+ Struct e' un tipo struttura di dati non omogenea,
+ puo' contenere tipi di dati diversi a differenza di un array.
+
+ Possiamo quindi raggruppare diverse proprieta' di un oggetto
+ sotto lo stesso identificatore.
+
+ Schema: http://lab.piffa.net/schemi/rgb.jpg
+ */
+
+
+struct lampeggino {
+  byte pin ;
+  long pausa;
+  boolean stato;
+  char feedback[20];
+} ;
+
+
+// instanziamo il lampeggino
+lampeggino rosso { 13, 1000, 0, ">> Led Rosso <<"};
+
+void setup()
+{
+    pinMode(rosso.pin, OUTPUT);
+    Serial.begin(9600);
+}
+
+void loop()
+{
+    // Facciamo un lampeggiatore
+    digitalWrite(rosso.pin,rosso.stato);
+    Serial.println(rosso.feedback);
+    rosso.stato = !rosso.stato ;
+    delay(rosso.pausa);
+}
+