]> git.piffa.net Git - sketchbook_andrea/blobdiff - basic/analog_input/photo_4_calibrated/photo_4_calibrated.ino
analog cleanup
[sketchbook_andrea] / basic / analog_input / photo_4_calibrated / photo_4_calibrated.ino
index b43252840ebf42a8ae92a369b67a23265220834d..2d4a997b64368189e66254c3a82924e947403150 100644 (file)
@@ -7,18 +7,20 @@
  quantita' di luce.
  
  Per ottenere valori significativi utilizzare unaresistenza
- da ~5k ohms in serie con il sensore.
+ da ~5k - 10k ohms in serie con il sensore.
  
  Questo sketch modifica l'intervallo di intermittenza di un led
  in base alla luminosita' rilevata.
+ Schema: https://learn.adafruit.com/assets/460
  */
 
 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 = 240;        // valore minimo rilevato dal sensore
-int max = 380;        // valore massimo rilevato dal sensore
+int min = 60;        // valore minimo rilevato dal sensore
+int max = 600;        // valore massimo rilevato dal sensore
 
 void setup() {
   // declare the ledPin as an OUTPUT:
@@ -30,26 +32,38 @@ void setup() {
 void loop() {
   // read the value from the sensor:
   sensorValue = analogRead(sensorPin);   
-  int calValue = map(sensorValue,min,max,0,1024) ; 
-  // Max pausa = 1024 
-      // turn the ledPin on
-    digitalWrite(ledPin, HIGH);  
-  // stop the program for <sensorValue> milliseconds:
-  delay(calValue);          
-  // turn the ledPin off:        
-  digitalWrite(ledPin, LOW);   
-  // stop the program for for <sensorValue> milliseconds:
-  // print the results to the serial monitor:
+  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 delay = ");      
+  Serial.print("\t cal delay = ");      
   Serial.println(calValue);
-  delay(sensorValue);                  
 }
 
-/* domande:
- 1. qual'e' il valore minimo rilevato?
- 2. quale il massimo?
- 3. Come adattare la risoluzione dell'attuatore alla sensibilita' del sensore?
- */
+/*
+Domande:
+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
+*/