]> git.piffa.net Git - sketchbook_andrea/commitdiff
light and sound
authorAndrea Manni <andrea@piffa.net>
Mon, 13 Apr 2015 16:31:59 +0000 (18:31 +0200)
committerAndrea Manni <andrea@piffa.net>
Mon, 13 Apr 2015 16:31:59 +0000 (18:31 +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 [new file with mode: 0644]
basic/analog_input/photo_6_smooth/photo_6_smooth.ino [new file with mode: 0644]
piezo/keyboard_three_pullup_buttons/keyboard_three_pullup_buttons.ino
piezo/piezo_mario_tune/piezo_mario_tune.ino

index 2266237cab2df3d23b7d47d47bbe10c9322d9795..3b0a96d754d7db11a0085b687660bd4fd769bad1 100644 (file)
@@ -1,6 +1,6 @@
 /*
   Photoresistor
 /*
   Photoresistor
-  
  Utilizzare una fotoresistenza come analog input.
  Il comportamento della foto resistenza e' simile
  a un potenziometro: varia la resistenza in base alla 
  Utilizzare una fotoresistenza come analog input.
  Il comportamento della foto resistenza e' simile
  a un potenziometro: varia la resistenza in base alla 
@@ -11,6 +11,8 @@
  
  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
  */
 
 int sensorPin = A0;    // select the input pin for the potentiometer
  */
 
 int sensorPin = A0;    // select the input pin for the potentiometer
@@ -20,7 +22,7 @@ int sensorValue = 0;  // variable to store the value coming from the sensor
 void setup() {
   // declare the ledPin as an OUTPUT:
   pinMode(ledPin, OUTPUT);  
 void setup() {
   // declare the ledPin as an OUTPUT:
   pinMode(ledPin, OUTPUT);  
-    // initialize serial communications at 9600 bps:
+  // initialize serial communications at 9600 bps:
   Serial.begin(9600); 
 }
 
   Serial.begin(9600); 
 }
 
@@ -34,7 +36,7 @@ void loop() {
   // turn the ledPin off:        
   digitalWrite(ledPin, LOW);   
   // stop the program for for <sensorValue> milliseconds:
   // turn the ledPin off:        
   digitalWrite(ledPin, LOW);   
   // stop the program for for <sensorValue> milliseconds:
-    // print the results to the serial monitor:
+  // print the results to the serial monitor:
   Serial.print("sensor = " );                       
   Serial.print(sensorValue);      
   Serial.print("\t delay = ");      
   Serial.print("sensor = " );                       
   Serial.print(sensorValue);      
   Serial.print("\t delay = ");      
@@ -43,8 +45,9 @@ void loop() {
 }
 
 /* domande:
 }
 
 /* domande:
-1. qual'e' il valore minimo rilevato?
-2. quale il massimo?
-3. Come adattare la risoluzione dell'attuatore alla sensibilita' del sensore?
-*/
+ 1. qual'e' il valore minimo rilevato?
+ 2. quale il massimo?
+ 3. Come adattare la risoluzione dell'attuatore alla sensibilita' del sensore?
+ */
+
 
 
index b43252840ebf42a8ae92a369b67a23265220834d..27086892c4ed01c079ce55a7cf678416a7286661 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
  */
 
 int sensorPin = A0;    // select the input pin for the potentiometer
 int ledPin = 13;      // select the pin for the LED
 int sensorValue = 0;  // variable to store the value coming from the sensor
 
  */
 
 int sensorPin = A0;    // select the input pin for the potentiometer
 int ledPin = 13;      // 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:
 
 void setup() {
   // declare the ledPin as an OUTPUT:
@@ -42,14 +44,16 @@ void loop() {
   // print the results to the serial monitor:
   Serial.print("sensor = " );                       
   Serial.print(sensorValue);      
   // print the results to the serial monitor:
   Serial.print("sensor = " );                       
   Serial.print(sensorValue);      
-  Serial.print("\t delay = ");      
+  Serial.print("\t cal delay = ");      
   Serial.println(calValue);
   delay(sensorValue);                  
 }
 
   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. 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?
+*/
 
 
diff --git a/basic/analog_input/photo_5_calibration/photo_5_calibration.ino b/basic/analog_input/photo_5_calibration/photo_5_calibration.ino
new file mode 100644 (file)
index 0000000..db0d2b0
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+  Calibration
+ Demonstrates one technique for calibrating sensor input.  The
+ sensor readings during the first five seconds of the sketch
+ execution define the minimum and maximum of expected values
+ attached to the sensor pin.
+ The sensor minimum and maximum initial values may seem backwards.
+ Initially, you set the minimum high and listen for anything 
+ lower, saving it as the new minimum. Likewise, you set the
+ maximum low and listen for anything higher as the new maximum.
+ The circuit:
+ * Analog sensor (potentiometer will do) attached to analog input 0
+ * LED attached from digital pin 9 to ground
+ created 29 Oct 2008
+ By David A Mellis
+ modified 30 Aug 2011
+ By Tom Igoe
+ http://arduino.cc/en/Tutorial/Calibration
+ This example code is in the public domain.
+ */
+
+// 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
+
+// variables:
+int sensorValue = 0;         // the sensor value
+int sensorMin = 1023;        // minimum sensor value
+int sensorMax = 0;           // maximum sensor value
+
+
+void setup() {
+  // turn on LED to signal the start of the calibration period:
+  pinMode(13, OUTPUT);
+  digitalWrite(13, HIGH);
+
+  // calibrate during the first five seconds 
+  while (millis() < 5000) {
+    sensorValue = analogRead(sensorPin);
+
+    // record the maximum sensor value
+    if (sensorValue > sensorMax) {
+      sensorMax = sensorValue;
+    }
+
+    // record the minimum sensor value
+    if (sensorValue < sensorMin) {
+      sensorMin = sensorValue;
+    }
+  }
+
+  // signal the end of the calibration period
+  digitalWrite(13, LOW);
+}
+
+void loop() {
+  // read the sensor:
+  sensorValue = analogRead(sensorPin);
+
+  // apply the calibration to the sensor reading
+  sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
+
+  // in case the sensor value is outside the range seen during calibration
+  sensorValue = constrain(sensorValue, 0, 255);
+
+  // fade the LED using the calibrated value:
+  analogWrite(ledPin, sensorValue);
+}
+/*
+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?
diff --git a/basic/analog_input/photo_6_smooth/photo_6_smooth.ino b/basic/analog_input/photo_6_smooth/photo_6_smooth.ino
new file mode 100644 (file)
index 0000000..9d1aa9c
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+  Smoothing
+ Legge 10 valori dal sensore e ritorna il valore medio tra questi.
+ */
+
+// 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
+
+// variables:
+int sensorValue = 0;         // the sensor value
+int sensorMin = 1023;        // minimum sensor value
+int sensorMax = 0;           // maximum sensor value
+
+
+void setup() {
+  // turn on LED to signal the start of the calibration period:
+  pinMode(13, OUTPUT);
+  digitalWrite(13, HIGH);
+
+  // calibrate during the first five seconds 
+  while (millis() < 5000) {
+    sensorValue = analogRead(sensorPin);
+
+    // record the maximum sensor value
+    if (sensorValue > sensorMax) {
+      sensorMax = sensorValue;
+    }
+
+    // record the minimum sensor value
+    if (sensorValue < sensorMin) {
+      sensorMin = sensorValue;
+    }
+  }
+
+  // signal the end of the calibration period
+  digitalWrite(13, LOW);
+}
+
+void loop() {
+  // read the sensor:
+  sensorValue = smoothRead(sensorPin);
+
+  // apply the calibration to the sensor reading
+  sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
+
+  // in case the sensor value is outside the range seen during calibration
+  sensorValue = constrain(sensorValue, 0, 255);
+
+  // fade the LED using the calibrated value:
+  analogWrite(ledPin, sensorValue);
+}
+// Funzioni
+
+int smoothRead(int sensorPin) {
+//  Legge 10 valori dal sensore e ritorna il valore medio tra questi.
+  int total = 0;
+  for (int i = 0; i < 10; i++) { 
+    total = total + analogRead(sensorPin);
+    delay(2); // Pausa per assestare il senstore
+  }
+  return(total / 10);
+}
+
+
index 1bd4e47c994c58fb0388c49b223e0e3751334c46..13fc24c67cfd3c91feefbdf0107c06eb1afafa91 100644 (file)
@@ -15,7 +15,7 @@
  */
 
 // Please take care of the include path
  */
 
 // Please take care of the include path
-#include "/root/arduino/sketchbook-lessons/piezo_mario_tune/pitches.h";
+#include "/home/utente/sketchbook-andrea/piezo/piezo_mario_tune/pitches.h";
 int input[]= {1,2,3};
   int notes[] = {
   NOTE_A4, NOTE_C4,NOTE_E3 };
 int input[]= {1,2,3};
   int notes[] = {
   NOTE_A4, NOTE_C4,NOTE_E3 };
index 26c171a7bde56b39811bb6ef40ecfa8bd62c1c11..12dc6020b9f5612a11cea19d27e7f7865c43cf35 100644 (file)
@@ -5,7 +5,7 @@
   last updated: 31/3/13
 */
 #include <pitches.h>
   last updated: 31/3/13
 */
 #include <pitches.h>
-
+//#include "/home/utente/sketchbook-andrea/piezo/piezo_mario_tune/pitches.h";
 #define melodyPin 3
 //Mario main theme melody
 int melody[] = {
 #define melodyPin 3
 //Mario main theme melody
 int melody[] = {