From: Andrea Manni <andrea@piffa.net>
Date: Thu, 12 Mar 2015 00:09:57 +0000 (+0100)
Subject: PWM
X-Git-Url: http://git.piffa.net/web?a=commitdiff_plain;h=f7cdba7df419dcde095f911daa92deb9dcd283ec;p=sketchbook_andrea

PWM
---

diff --git a/advanced_projects/led_PWM_logical_analyzer_demo/led_PWM_logical_analyzer_demo.ino b/advanced_projects/led_PWM_logical_analyzer_demo/led_PWM_logical_analyzer_demo.ino
index e8c4cc8..723a7bf 100644
--- a/advanced_projects/led_PWM_logical_analyzer_demo/led_PWM_logical_analyzer_demo.ino
+++ b/advanced_projects/led_PWM_logical_analyzer_demo/led_PWM_logical_analyzer_demo.ino
@@ -4,17 +4,18 @@
  Connect pin ~11 to a logic analyzer and a multimeter
  and witness the power of the built-in PWM generator.
  
+ Usage: change pausa from 3000 (demostration) to 20 for sampling.
  BTW: Logic comes from: http://downloads.saleae.com/betas/1.1.34/Logic+1.1.34+(64-bit).zip
  */
 
-int led = 11;
-int pausa = 3000; // or 20 when sampling
+int led = 9;
+int pausa = 3000; // 3000 for demo or 20 when sampling
 
 
 void setup()
 {
   pinMode(led, OUTPUT);
-  delay(4000);
+  delay(3000);
 }
 
 void loop()
@@ -36,7 +37,7 @@ void loop()
     analogWrite(led, c) ;
     delay(2 );
   }
-  exit(0);
+  //exit(0);
 
 }
 
diff --git a/basic/blinks/blink_1_variabili/blink_1_variabili.ino b/basic/blinks/blink_1_variabili/blink_1_variabili.ino
index d828bc9..7a5a46a 100644
--- a/basic/blinks/blink_1_variabili/blink_1_variabili.ino
+++ b/basic/blinks/blink_1_variabili/blink_1_variabili.ino
@@ -1,3 +1,5 @@
+// ////////////
+// Commento iniziale
 /*
   Blink v1
 
@@ -9,18 +11,24 @@
   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;
 int breve = 200;  // Variabile richiambile nel corso dell'esecuzione
 int lunga = 1000;
 
-// the setup routine runs once when you press reset:
+// /////////////////
+// Setup: eseguita una volta sola
 void setup() {                
   // initialize the digital pin as an output.
   pinMode(led, OUTPUT);     
 }
 
+// ///////////////
+// loop
 // the loop routine runs over and over again forever:
 void loop() {
   digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
diff --git a/basic/blinks/blink_2_funzioni/blink_2_funzioni.ino b/basic/blinks/blink_2_funzioni/blink_2_funzioni.ino
index 210de4d..e1a6066 100644
--- a/basic/blinks/blink_2_funzioni/blink_2_funzioni.ino
+++ b/basic/blinks/blink_2_funzioni/blink_2_funzioni.ino
@@ -1,3 +1,6 @@
+
+// ////////////
+// Commento iniziale
 /*
   Blink v2
  
@@ -6,6 +9,8 @@
  
  This example code is in the public domain.
  */
+// //////////////
+// Dichiarazione variabili
 
 // Pin 13 has an LED connected on most Arduino boards.
 // give it a name:
@@ -13,19 +18,22 @@ int led = 13;
 int breve = 200;  // Variabile richiambile nel corso dell'esecuzione
 int lunga = 1000;
 
-// the setup routine runs once when you press reset:
+// /////////////////
+// Setup
 void setup() {                
   // initialize the digital pin as an output.
   pinMode(led, OUTPUT);     
 }
 
-// the loop routine runs over and over again forever:
+// ///////////////
+// loop
 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() {
@@ -49,4 +57,3 @@ void lento() {
 }
 
 
-
diff --git a/basic/buttons/button_2_serial_debug/button_2_serial_debug.ino b/basic/buttons/button_2_serial_debug/button_2_serial_debug.ino
index f0b41d5..e4866bf 100644
--- a/basic/buttons/button_2_serial_debug/button_2_serial_debug.ino
+++ b/basic/buttons/button_2_serial_debug/button_2_serial_debug.ino
@@ -9,14 +9,11 @@
  - http://lab.piffa.net/schemi/button_1_schem.png
  */
 
-// Pin 13 has an LED connected on most Arduino boards.
-// give it a name:
-int led = 13;
+int led = 12;
 int input = 2;
 
 // the setup routine runs once when you press reset:
 void setup() {                
-  // initialize the digital pin as an output.
   pinMode(led, OUTPUT);       // Il PIN e' attivato come output
   pinMode(input, INPUT);        // Il PIN e' attivato come output
 
diff --git a/basic/buttons/button_state_4_state/button_state_4_state.ino b/basic/buttons/button_state_4_state/button_state_4_state.ino
index 87ceffa..ef6ae05 100644
--- a/basic/buttons/button_state_4_state/button_state_4_state.ino
+++ b/basic/buttons/button_state_4_state/button_state_4_state.ino
@@ -4,28 +4,29 @@
  Legge lo stato di un input
  
  */
-int led = 13;
+int led = 13;              // Definizione delle variabili
 int buttonPin = 2;              
-int statoAttuale;                        // variable for reading the pin status
-int ultimoStato;                // variable to hold the last button state
+                           // Dichiarazione di variabili
+int statoAttuale;          // Variabile per leggere lo stato del bottone
+int ultimoStato;           // Variabile per registrare l'ultimo stato del bottone
 int ledStatus;             // varabile per mantenere lo stato del led
 
 void setup() {
-  pinMode(buttonPin, INPUT);    // Set the switch pin as input
+  pinMode(buttonPin, INPUT);    
   pinMode(led, OUTPUT);    
-  Serial.begin(9600);           // Set up serial communication at 9600bps
-  ultimoStato = digitalRead(buttonPin);   // read the initial state
-  ledStatus = 0;
+  Serial.begin(9600);           // Attiva la comunicazione seriale a 9600bps
+  ultimoStato = digitalRead(buttonPin);   // Prima lettura del bottone
+  ledStatus = 0;                          // Il LED viene inpostato come spento                        
 }
 
 void loop(){
   statoAttuale = digitalRead(buttonPin);      // read input value and store it in var
-  delay(20);                      // riduce l'effetto bounce
-  if (statoAttuale != ultimoStato) {          // the button state has changed!
-    if (statoAttuale == HIGH) {                // check if the button is pressed
+  delay(20);                                  // riduce l'effetto bounce
+  if (statoAttuale != ultimoStato) {          // lo stato del bottone e' cambiato
+    if (statoAttuale == HIGH) {               // il bottone e' stato provato
       Serial.println("Button premuto");
      
-      ledStatus = !ledStatus ;    // Inverte lo stato del LED 
+      ledStatus = !ledStatus ;          // Inverte lo stato del LED 
       // ledStatus = 1 - ledStatus ;    // Forma analoga
       
       Serial.print("Stato del LED: ");  // DEBUG
@@ -33,7 +34,7 @@ void loop(){
     } 
   }
 
-  ultimoStato = statoAttuale;                 // save the new state in our variable
+  ultimoStato = statoAttuale;        // save the new state in our variable
   digitalWrite(led, ledStatus);      // setta il led allo stato richiesto
 
 }
diff --git a/basic/led_PWM_simple/led_PWM_simple.ino b/basic/led_PWM_simple/led_PWM_simple.ino
deleted file mode 100644
index 254ad86..0000000
--- a/basic/led_PWM_simple/led_PWM_simple.ino
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
-    LED for PWM
- 
- LEd PWM routine with a for loop, ascending and descending.
- 
- */
-
-int led = 11;
-int c = 0;
-
-
-void setup()
-{
-  pinMode(led, OUTPUT);
-
-}
-
-void loop()
-{
-  for ( c = 0; c < 255 ; c++) {
-    analogWrite(led, c) ;
-    delay(5 );
-  }
-  // Now reverse
-    for ( c = 255; c > 0 ; c--) {
-    analogWrite(led, c) ;
-    delay(5 );
-  }
-}
-
-
-
diff --git a/basic/pwm_1_while_byte/pwm_1_while_byte.ino b/basic/pwm_1_while_byte/pwm_1_while_byte.ino
new file mode 100644
index 0000000..de1a994
--- /dev/null
+++ b/basic/pwm_1_while_byte/pwm_1_while_byte.ino
@@ -0,0 +1,27 @@
+/*pwm_
+   Fade
+ 
+   PWM per un LED: aumentare progressivamente la luminosita'.
+ */
+
+byte led  = 9   ;     // Il pin ~9 e' abilitato al PWM
+byte brightness = 0;  // Valore iniziale per il PWM del LED
+
+// the setup routine runs once when you press reset:
+void setup()  { 
+  pinMode(led, OUTPUT); // Il PIN nove va dichiarato come un OUTPUT
+} 
+
+void loop()  { 
+  analogWrite(led, brightness++);  // La funziona analogWrite utilizza il PWM
+  // a 8 bit integrato nel MCU: simula un serie di valori intermedi
+  // nell'intervallo discreto con minimo 0 (spento) e  massimo 255 (acceso).         
+  delay(10);       
+}
+
+/* Domande:
+
+1. Come fare a invertire la dissolvenza diminuendo la luminosita'?
+2. Provare a far salire e poi scendere la luminosita'
+
+
diff --git a/basic/pwm_2_for_loop/pwm_2_for_loop.ino b/basic/pwm_2_for_loop/pwm_2_for_loop.ino
new file mode 100644
index 0000000..12e6e78
--- /dev/null
+++ b/basic/pwm_2_for_loop/pwm_2_for_loop.ino
@@ -0,0 +1,36 @@
+/*
+    LED for PWM
+ 
+ PWM per un LED: aumentare progressivamente la luminosita'.
+ Utilizzo di un ciclo iterativo: for loop
+ 
+ */
+
+int led = 9; // Pin per il PWM
+
+void setup()
+{
+  pinMode(led, OUTPUT);
+
+}
+
+void loop()
+{
+  for ( i = 0; i < 255 ; i++) { // Operatore ternario, 3 argomenti:
+  /* 1. definizione iteratore
+     2. limite iteratore
+     3. incremento operatore
+     */
+    analogWrite(led, i) ;
+    delay(5 );
+  }
+  // Ora l'inverso
+  for ( c = 255; c > 0 ; c--) {
+    analogWrite(led, c) ;
+    delay(5 );
+  }
+}
+
+
+
+
diff --git a/basic/simplest_pwm_byte/simplest_pwm_byte.ino b/basic/simplest_pwm_byte/simplest_pwm_byte.ino
deleted file mode 100644
index fc7658d..0000000
--- a/basic/simplest_pwm_byte/simplest_pwm_byte.ino
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- Fade
- 
- This example shows how to fade an LED on pin 9
- using the analogWrite() function.
- 
- This example code is in the public domain.
- */
-
-byte led  = 9   ;        // the pin that the LED is attached to
-
-byte brightness = 0;    // how bright the LED is
-
-// the setup routine runs once when you press reset:
-void setup()  { 
-  // declare pin 9 to be an output:
-  pinMode(led, OUTPUT);
-} 
-
-// the loop routine runs over and over again forever:
-void loop()  { 
-  // set the brightness of pin 9:
-  analogWrite(led, brightness++);    
-  delay(10);       
-}
-
-