]> git.piffa.net Git - sketchbook_andrea/commitdiff
analog cleanup
authorAndrea Manni <andrea@piffa.net>
Wed, 12 Apr 2017 00:32:22 +0000 (02:32 +0200)
committerAndrea Manni <andrea@piffa.net>
Wed, 12 Apr 2017 11:38:38 +0000 (13:38 +0200)
basic/analog_input/photo_3_serial/photo_3_serial.ino
basic/analog_input/photo_4_calibrated/photo_4_calibrated.ino
basic/analog_input/photo_5_calibration/photo_5_calibration.ino
basic/analog_input/photo_6_smooth/photo_6_smooth.ino
basic/pwm/pwm_0_stati/pwm_0_stati.ino
basic/pwm/pwm_2_for_loop/pwm_2_for_loop.ino
basic/pwm/pwm_4_analog_input/pwm_4_analog_input.ino

index 3b0a96d754d7db11a0085b687660bd4fd769bad1..22c070f72bfeba4d662661333fb739274ad84686 100644 (file)
  in base alla luminosita' rilevata.
  
  Schema: http://lab.piffa.net/schemi/photoresistor_led.png
  in base alla luminosita' rilevata.
  
  Schema: http://lab.piffa.net/schemi/photoresistor_led.png
+ Guida:  https://learn.adafruit.com/photocells/using-a-photocell
  */
 
  */
 
-int sensorPin = A0;    // select the input pin for the potentiometer
-int ledPin = 13;      // select the pin for the LED
+int sensorPin = A0;   // select the input pin for the potentiometer
+int ledPin = 3;      // select the pin for the LED
 int sensorValue = 0;  // variable to store the value coming from the sensor
 
 void setup() {
 int sensorValue = 0;  // variable to store the value coming from the sensor
 
 void setup() {
@@ -47,7 +48,8 @@ void loop() {
 /* domande:
  1. qual'e' il valore minimo rilevato?
  2. quale il massimo?
 /* domande:
  1. qual'e' il valore minimo rilevato?
  2. quale il massimo?
- 3. Come adattare la risoluzione dell'attuatore alla sensibilita' del sensore?
+ 3. Modificare lo sketch in modo che modifichi la luminosita' di un LED 
+via PWM tramite il valore letto dal sensore. (vedi esercizio sucessivo)
  */
 
 
  */
 
 
index 313598d2cd98f5bc375a17f3f9eec6857e086974..2d4a997b64368189e66254c3a82924e947403150 100644 (file)
  Questo sketch modifica l'intervallo di intermittenza di un led
  in base alla luminosita' rilevata.
  
  Questo sketch modifica l'intervallo di intermittenza di un led
  in base alla luminosita' rilevata.
  
- Schema: http://lab.piffa.net/schemi/photoresistor_led.png
+ Schema: https://learn.adafruit.com/assets/460
  */
 
 int sensorPin = A0;    // select the input pin for the potentiometer
  */
 
 int sensorPin = A0;    // select the input pin for the potentiometer
-int ledPin = 13;      // select the pin for the LED
+int ledPin = 11;      // select the pin for the LED
 int sensorValue = 0;  // variable to store the value coming from the sensor
 
 int min = 60;        // valore minimo rilevato dal sensore
 int sensorValue = 0;  // variable to store the value coming from the sensor
 
 int min = 60;        // valore minimo rilevato dal sensore
@@ -32,28 +32,38 @@ void setup() {
 void loop() {
   // read the value from the sensor:
   sensorValue = analogRead(sensorPin);   
 void loop() {
   // read the value from the sensor:
   sensorValue = analogRead(sensorPin);   
-  int calValue = map(sensorValue,min,max,0,1000) ; 
-  // Max pausa = 1sec
-      // turn the ledPin on
-    digitalWrite(ledPin, HIGH);  
-  // stop the program for <sensorValue> milliseconds:
-  delay(calValue);          
-  // turn the ledPin off:        
-  digitalWrite(ledPin, LOW);   
+  delay(5); // stabilizziamo il sensore
+  int calValue = map(sensorValue,min,max,0,255) ; 
+  analogWrite(ledPin, calValue);  
   // Debug, per disabilitarlo togliere l'inizializzazione della seriale
   // dal setup()
   Serial.print("sensor = " );                       
   Serial.print(sensorValue);      
   Serial.print("\t cal delay = ");      
   Serial.println(calValue);
   // Debug, per disabilitarlo togliere l'inizializzazione della seriale
   // dal setup()
   Serial.print("sensor = " );                       
   Serial.print(sensorValue);      
   Serial.print("\t cal delay = ");      
   Serial.println(calValue);
-  delay(calValue);                  
 }
 
 /*
 Domande:
 }
 
 /*
 Domande:
-1. Modificare lo sketch in modo che modifichi la luminosita' di un led 
-via PWM tramite il valore letto dal sensore.
-2. Come fare per costringere la variabile dentro un intervallo stabilito?
-3. Come si potrebbe eseguire la calibrazione automaticamente?
+1. Come fare per costringere la variabile dentro un intervallo stabilito?
+2. Come si potrebbe eseguire la calibrazione automaticamente?
+.
+.
+.
+.
+.
+.
+.
+.
+.
+.
+.
+.
+.
+.
+.
+
+1. Utilizzando costrain()
+2. Vedi esercizio sucessivo
 */
 
 */
 
index fd86f7f641cd6b0e494f66157ec972d53a9d2e2d..bf43b37ae713981720ea1e6c0529b09553d13984 100644 (file)
  http://arduino.cc/en/Tutorial/Calibration
  
  This example code is in the public domain.
  http://arduino.cc/en/Tutorial/Calibration
  
  This example code is in the public domain.
+ Schema:  Schema: https://learn.adafruit.com/assets/460
  
  */
 
 // These constants won't change:
 const int sensorPin = A0;    // pin that the sensor is attached to
  
  */
 
 // These constants won't change:
 const int sensorPin = A0;    // pin that the sensor is attached to
-const int ledPin = 9;        // pin that the LED is attached to
+const int ledPin = 11;        // pin that the LED is attached to
 
 // variables:
 int sensorValue = 0;         // the sensor value
 
 // variables:
 int sensorValue = 0;         // the sensor value
index 1aaa6685e4ca81b2d44ca7f7d8b12ca5a20b21c2..60913fe2414f3bdb931ce91f6a603f3de62ac5cf 100644 (file)
@@ -2,13 +2,16 @@
   Smoothing
  
  Legge 10 valori dal sensore e ritorna il valore medio tra questi.
   Smoothing
  
  Legge 10 valori dal sensore e ritorna il valore medio tra questi.
+
+ Schema: https://learn.adafruit.com/assets/460
+
  
  
  */
 
 // These constants won't change:
 const int sensorPin = A0;    // pin that the sensor is attached to
  
  
  */
 
 // These constants won't change:
 const int sensorPin = A0;    // pin that the sensor is attached to
-const int ledPin = 9;        // pin that the LED is attached to
+const int ledPin = 11;        // pin that the LED is attached to
 
 // variables:
 int sensorValue = 0;         // the sensor value
 
 // variables:
 int sensorValue = 0;         // the sensor value
@@ -19,6 +22,7 @@ int sensorMax = 0;           // maximum sensor value
 void setup() {
   // turn on LED to signal the start of the calibration period:
   pinMode(13, OUTPUT);
 void setup() {
   // turn on LED to signal the start of the calibration period:
   pinMode(13, OUTPUT);
+  pinMode(ledPin, OUTPUT);
   digitalWrite(13, HIGH);
 
   // calibrate during the first five seconds 
   digitalWrite(13, HIGH);
 
   // calibrate during the first five seconds 
index 3863c4a8a3ad19ec4c3351c2962396b7a8e2033d..7842b6cc2d981061b2b55c5e91b417e100cd2fac 100644 (file)
@@ -3,11 +3,13 @@
  
    PWM per un LED: impostare i valori di luminosita' di un LED.
    4 stati di luminosita' per un LED collegato a un PIN PWM
  
    PWM per un LED: impostare i valori di luminosita' di un LED.
    4 stati di luminosita' per un LED collegato a un PIN PWM
+
+   Il PWM del 328p simula un convertitore digitale -> analogico
+   con profondita' di 8 bit: 256 valori da 0 - 255 .
    
  */
 
 const int led  = 9   ;     // Il pin ~9 e' abilitato al PWM
    
  */
 
 const int led  = 9   ;     // Il pin ~9 e' abilitato al PWM
-byte brightness = 255;  // Valore iniziale per il PWM del LED
 const int pausa = 2000; // Pausa tra uno stato e l'altro
 
 void setup()  { 
 const int pausa = 2000; // Pausa tra uno stato e l'altro
 
 void setup()  { 
@@ -16,22 +18,22 @@ void setup()  {
 
 void loop()  { 
   // OUTPUT al 100% : 255
 
 void loop()  { 
   // OUTPUT al 100% : 255
-  analogWrite(led, brightness) ; 
+  analogWrite(led, 255) ; 
   delay(pausa);
 
   // OUTPUT al 75% : 191
   delay(pausa);
 
   // OUTPUT al 75% : 191
-  analogWrite(led, brightness * 0.75) ; 
+  analogWrite(led, 191) ; 
   delay(pausa);
   
   // OUTPUT al 50% : 127
   delay(pausa);
   
   // OUTPUT al 50% : 127
-  analogWrite(led, brightness * 0.5) ; 
+  analogWrite(led, 127) ; 
   delay(pausa);
   
   // OUTPUT al 25%: 63
   delay(pausa);
   
   // OUTPUT al 25%: 63
-  analogWrite(led, brightness * 0.25 ) ; 
+  analogWrite(led, 63 ) ; 
   delay(pausa);
 
   // OUTPUT al 0%
   delay(pausa);
 
   // OUTPUT al 0%
-  analogWrite(led, brightness * 0) ;
+  analogWrite(led, 0) ;
   delay(pausa);
 }
   delay(pausa);
 }
index 610d302891feceeb146c0aa067f4131d63463813..99ff02b37b324a4942e1be8a1e6f38456262fe39 100644 (file)
@@ -33,7 +33,7 @@ void loop()
 }
 
 /* Domande:
 }
 
 /* Domande:
2. I due loop sembrano molto simili: e' possibile accorparli?
1. I due loop sembrano molto simili: e' possibile accorparli?
 
  .
  .
 
  .
  .
@@ -56,9 +56,7 @@ void loop()
  .
  .
  - Risposte:
  .
  .
  - Risposte:
- 1. Si, le variabili i e c esistono solo nello scopo degli iteratori
-    in cui sono dichiarate.
- 2. Vedi es. suciessivo.
+ 1. Vedi es. suciessivo.
  */
 
 
  */
 
 
index 5e9f83d603e46391612227af005fc6a522a27f79..fe670c40be9d977d25dbd1fbca078104a3368629 100644 (file)
@@ -7,9 +7,9 @@
   
   */
   
   
   */
   
-int inputPin = A0;  // set input pin for the potentiometer
-int inputValue = 0; // potentiometer input variable
-int ledPin = 3;     // output pin, deve avere il PWM
+const byte inputPin = A0;// set input pin for the potentiometer
+int inputValue = 0;      // potentiometer input variable
+const byte ledPin = 3;   // output pin, deve avere il PWM
 
 void setup() {
      // declare the ledPin as an OUTPUT:
 
 void setup() {
      // declare the ledPin as an OUTPUT: