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() {
/* 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)
*/
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 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
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);
- delay(calValue);
}
/*
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
*/
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
-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
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
-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
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
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
-byte brightness = 255; // Valore iniziale per il PWM del LED
const int pausa = 2000; // Pausa tra uno stato e l'altro
void setup() {
void loop() {
// OUTPUT al 100% : 255
- analogWrite(led, brightness) ;
+ analogWrite(led, 255) ;
delay(pausa);
// OUTPUT al 75% : 191
- analogWrite(led, brightness * 0.75) ;
+ analogWrite(led, 191) ;
delay(pausa);
// OUTPUT al 50% : 127
- analogWrite(led, brightness * 0.5) ;
+ analogWrite(led, 127) ;
delay(pausa);
// OUTPUT al 25%: 63
- analogWrite(led, brightness * 0.25 ) ;
+ analogWrite(led, 63 ) ;
delay(pausa);
// OUTPUT al 0%
- analogWrite(led, brightness * 0) ;
+ analogWrite(led, 0) ;
delay(pausa);
}
}
/* Domande:
- 2. I due loop sembrano molto simili: e' possibile accorparli?
+ 1. 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.
+ 1. Vedi es. suciessivo.
*/
*/
-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: