void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
- int calValue = map(sensorValue,min,max,0,1024) ;
- // Max pausa = 1024
+ 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);
- // stop the program for for <sensorValue> milliseconds:
- // print the results to the serial monitor:
+ // 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(sensorValue);
+ delay(calValue);
}
/*
total = total + analogRead(sensorPin);
delay(2); // Pausa per assestare il senstore
}
- return(total / 10);
+ return(total / 10); // Valore medio
}
modified 31 May 2012
by Tom Igoe, with suggestion from Michael Flynn
+ Modified by A.Manni
+
This example code is in the public domain.
http://arduino.cc/en/Tutorial/Tone2
// 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 piezoPin = 9; // pin that the piezo is attached to
// variables:
int sensorValue = 0; // the sensor value
-int sensorMin = 1023; // minimum sensor value
-int sensorMax = 0; // maximum sensor value
+int sensorMin = 1023; // minimum sensor value
+int sensorMax = 0; // maximum sensor value
+int pitch = 0; // pithc value for the piezo
void setup() {
// initialize serial communications (for debugging only):
Serial.begin(9600);
pinMode(13, OUTPUT);
+ pinMode(piezoPin, OUTPUT);
digitalWrite(13, HIGH);
// calibrate during the first five seconds
void loop() {
// read the sensor:
- int sensorReading = analogRead(sensorPin);
+ int sensorValue = analogRead(sensorPin);
// print the sensor reading so you know its range
- Serial.println(sensorReading);
- // map the analog input range (in this case, 400 - 1000 from the photoresistor)
+ Serial.println(sensorValue);
+ // map the analog input range
// to the output pitch range (120 - 1500Hz)
// change the minimum and maximum input numbers below
// depending on the range your sensor's giving:
- int thisPitch = map(sensorReading, sensorMin, sensorMax, 220, 3500);
+ pitch = map(sensorValue, sensorMin, sensorMax, 220, 3500);
// play the pitch:
- if (sensorReading < sensorMax -50) {
- tone(ledPin, thisPitch, 10);
+ if (sensorValue < sensorMax -50) { // Offset to prevent the piezo to ring
+ // all the time, check sensor polarity
+ tone(piezoPin, pitch, 10);
}
delay(1); // delay in between reads for stability
}
+/* Esercizi:
+ 1. Implementare constrain e smoothing in questo sketch
+ */
if ((millis() - previousMillis) > speed / 256) {
brightness++; // Incrementiamo la luminosita'
previousMillis = millis();
+ Serial.println(brightness);
};
}
if ((millis() - previousMillis) > speed / 256) {
brightness--; // Incrementiamo la luminosita'
previousMillis = millis();
+ Serial.println(brightness);
};
}
if ((millis() - previousMillis) > speed / 512) {
brightness = brightness + increment; // Incrementiamo la luminosita'
previousMillis = millis();
+ Serial.println(brightness);
+ analogWrite(ledPin, brightness);
if (brightness == 0 || brightness == 255) { // Reverse direction
increment = -increment ;
};
};
- analogWrite(ledPin, brightness);
}
// Variabili
int ledPin ; // il numero del LED pin
int speed ; // velocita' del ciclo in ms
- byte brightness ; // luminostia' iniziale
long previousMillis ; //precedente cambio di stato
byte increment ; // aumenta brighteness nel loop UD
void Up(long speed);
void Down(long speed);
void UD(long speed);
+ byte brightness ; // luminostia' iniziale
};
//////////////////////
+++ /dev/null
-/*
- Analog Input
- Demonstrates analog input by reading an analog sensor on analog pin 0 and
- turning on and off a light emitting diode(LED) connected to digital pin 13.
- The amount of time the LED will be on and off depends on
- the value obtained by analogRead().
-
- The circuit:
- * Potentiometer attached to analog input 0
- * center pin of the potentiometer to the analog pin
- * one side pin (either one) to ground
- * the other side pin to +5V
- * LED anode (long leg) attached to digital output 13
- * LED cathode (short leg) attached to ground
-
- * Note: because most Arduinos have a built-in LED attached
- to pin 13 on the board, the LED is optional.
-
-
- Created by David Cuartielles
- modified 30 Aug 2011
- By Tom Igoe
-
- This example code is in the public domain.
-
- http://arduino.cc/en/Tutorial/AnalogInput
-
- Schema: http://lab.piffa.net/schemi/potenziometro_bb.png
-
- */
-
-const int sensorPin = A0; // select the input pin for the potentiometer
-const int ledPin = 13; // select the pin for the LED
-int sensorValue = 0; // variable to store the value coming from the sensor
-
-void setup() {
- // declare the ledPin as an OUTPUT:
- pinMode(ledPin, OUTPUT);
- // Non e' necessario dichiarare un pin come input:
- // tutti i pin di default sono input
-}
-
-void loop() {
- // read the value from the sensor:
- sensorValue = analogRead(sensorPin);
- // turn the ledPin on
- digitalWrite(ledPin, HIGH);
- // stop the program for <sensorValue> milliseconds:
- delay(sensorValue);
- // turn the ledPin off:
- digitalWrite(ledPin, LOW);
- // stop the program for for <sensorValue> milliseconds:
- delay(sensorValue);
-}
+++ /dev/null
-/*
- Analog Input
- Demonstrates analog input by reading an analog sensor on analog pin 0 and
- turning on and off a light emitting diode(LED) connected to digital pin 13.
- The amount of time the LED will be on and off depends on
- the value obtained by analogRead().
-
- The circuit:
- * Potentiometer attached to analog input 0
- * center pin of the potentiometer to the analog pin
- * one side pin (either one) to ground
- * the other side pin to +5V
- * LED anode (long leg) attached to digital output 13
- * LED cathode (short leg) attached to ground
-
- * Note: because most Arduinos have a built-in LED attached
- to pin 13 on the board, the LED is optional.
-
-
- Created by David Cuartielles
- modified 30 Aug 2011
- By Tom Igoe
-
- This example code is in the public domain.
-
- http://arduino.cc/en/Tutorial/AnalogInput
-
- */
-
-const int sensorPin = A0; // select the input pin for the potentiometer
-const int ledPin = 13; // select the pin for the LED
-int sensorValue = 0; // variable to store the value coming from the sensor
-
-void setup() {
- // declare the ledPin as an OUTPUT:
- pinMode(ledPin, OUTPUT);
- // initialize serial communications at 9600 bps:
- Serial.begin(9600);
-}
-
-void loop() {
- // read the value from the sensor:
- sensorValue = analogRead(sensorPin);
- // turn the ledPin on
- digitalWrite(ledPin, HIGH);
- // stop the program for <sensorValue> milliseconds:
- delay(sensorValue);
- // turn the ledPin off:
- digitalWrite(ledPin, LOW);
- // stop the program for for <sensorValue> milliseconds:
- // print the results to the serial monitor:
- Serial.print("sensor = " );
- Serial.print(sensorValue);
- Serial.print("\t delay = ");
- Serial.println(sensorValue);
- delay(sensorValue);
-}
+++ /dev/null
-/*
- Operatori comparativi binari
- Comparison operators, binary
-
- */
-
-int a = 5;
-int b = 10;
-int c = 20;
-
-void setup() // run once, when the sketch starts
-{
- Serial.begin(9600); // set up Serial library at 9600 bps
-
- Serial.println("Here is some math: ");
-
- Serial.print("a = ");
- Serial.println(a);
- Serial.print("b = ");
- Serial.println(b);
- Serial.print("c = ");
- Serial.println(c);
-
- Serial.print("a > b = "); // maggiore
- Serial.println(a > b);
-
- Serial.print("a < b = "); // minore
- Serial.println(a < b);
-
- Serial.print("a == b -> "); // stesso valore
- Serial.println(a == b);
-
- Serial.print("a != b -> "); // valore diverso
- Serial.println(a != b);
-
- Serial.print("a <= b ->"); // minore uguale
- Serial.println(a <= b);
-
- Serial.print("a >= b -> "); // maggiore uguale
- Serial.println(a >= b);
-}
-
-void loop() // we need this to be here even though its empty
-{
-}
-
-
+++ /dev/null
-/*
- PWM Stati
-
- PWM per un LED: impostare i valori di luminosita' di un LED.
- 4 stati di luminosita' per un LED collegato a un PIN 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() {
- pinMode(led, OUTPUT); // Il PIN nove va dichiarato come un OUTPUT
-}
-
-void loop() {
- // OUTPUT al 100% : 255
- analogWrite(led, brightness) ;
- delay(pausa);
-
- // OUTPUT al 75% : 191
- analogWrite(led, brightness * 0.75) ;
- delay(pausa);
-
- // OUTPUT al 50% : 127
- analogWrite(led, brightness * 0.5) ;
- delay(pausa);
-
- // OUTPUT al 25%: 63
- analogWrite(led, brightness * 0.25 ) ;
- delay(pausa);
-
- // OUTPUT al 0%
- analogWrite(led, brightness * 0) ;
- delay(pausa);
-}
+++ /dev/null
-/*
- Fade sali e scendi
-
- PWM per un LED: aumentare progressivamente la luminosita'.
- Aumenta e diminuisce la luminostia' usando un ciclo while
-
- Schema: http://lab.piffa.net/schemi/led_single_bb.png
- Usare un PIN abilitato per il PWM.
- */
-
-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() {
- while (brightness < 255) {
- 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);
- brightness = brightness + 1; // Incrementiamo la luminosita'
- }
-
- while (brightness > 0) {
- analogWrite(led, brightness);// La funziona analogWrite utilizza il PWM
- delay(10);
- brightness = brightness - 1; // Decrementiamo la luminosita'
- }
-
-
-}
-/* Note:
-
-- basic/pwm/pwm_3_fade_reverser/pwm_3_fade_reverser.ino
-
- E molto piu' snello utilizzando il solo ciclo loop come iteratore
- e una condizione per cambiare l'incremento.
-*/
+++ /dev/null
-/*
- Fade
-
- PWM per un LED: aumentare progressivamente la luminosita'
- utilizzando un ciclo while e l'overflow di un byte.
-
- Schema: http://lab.piffa.net/schemi/led_single_bb.png
- (Cambiare PIN)
- */
-
-const byte led = 9 ; // Il pin ~9 e' abilitato al PWM
-byte brightness = 0; // Valore iniziale per il PWM del LED
- // Perche' brightness non e' una costante?
-
-// 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);
- brightness = brightness + 1; // Incrementiamo la luminosita'
-}
-
-/* Domande:
-
-1. Come fare a invertire la dissolvenza diminuendo la luminosita'?
-2. Provare a far salire e poi scendere la luminosita'
-
-*/
+++ /dev/null
-/*
- 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
-int pausa = 5;
-
-void setup()
-{
- pinMode(led, OUTPUT);
-}
-
-void loop()
-{
- for (int i = 0; i < 255 ; i++) { // Operatore ternario, 3 argomenti:
- /* 1. definizione iteratore
- 2. limite iteratore
- 3. incremento operatore
- */
- analogWrite(led, i) ;
- delay(pausa);
- }
- // Ora l'inverso
- for (int c = 255; c > 0 ; c--) { // Domanda: 1. avrei potuto usare come
- // variabile di nuovo i ?
- analogWrite(led, c) ;
- delay(pausa);
- }
-}
-
-/* Domande:
- 2. I due loop sembrano molto simili: e' possibile accorparli?
-
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- .
- - Risposte:
- 1. Si, le variabili i e c esistono solo nello scopo degli iteratori
- in cui sono dichiarate.
- 2. Vedi es. suciessivo.
- */
-
-
-
-
+++ /dev/null
-/*
- Analog PWM
-
- Impostare la frequenza del PWM tramite un input analogico.
-
-- Schema: http://lab.piffa.net/schemi/arduino-pwm-diagram.png
-
- */
-
-int inputPin = A0; // set input pin for the potentiometer
-int inputValue = 0; // potentiometer input variable
-int ledPin = 3; // output pin, deve avere il PWM
-
-void setup() {
- // declare the ledPin as an OUTPUT:
- pinMode(ledPin, OUTPUT);
-}
-
-void loop() {
- // read the value from the potentiometer:
- inputValue = analogRead(inputPin);
-
- // send the square wave signal to the LED:
- analogWrite(ledPin, inputValue/4);
- // la lettura analogica e' a 10 bit (0-1024)
- // Il PWM invece e' a 8 bit (0-255)
- // Circa 1024 / 4 ~= 255
-
- // Domanda: dovrebbe esserci un delay()?
-}
-