]> git.piffa.net Git - sketchbook_andrea/blob - basic/analog_input/optimization/analogInput_with_range_and_limits/analogInput_with_range_and_limits.ino
operatori + analog
[sketchbook_andrea] / basic / analog_input / optimization / analogInput_with_range_and_limits / analogInput_with_range_and_limits.ino
1 /*
2   Analog Input
3  Demonstrates analog input by reading an analog sensor on analog pin 0 and
4  turning on and off a light emitting diode(LED)  connected to digital pin 13. 
5  The amount of time the LED will be on and off depends on
6  the value obtained by analogRead(). 
7  
8  The circuit:
9  * Potentiometer attached to analog input 0
10  * center pin of the potentiometer to the analog pin
11  * one side pin (either one) to ground
12  * the other side pin to +5V
13  * LED anode (long leg) attached to digital output 13
14  * LED cathode (short leg) attached to ground
15  
16  * Note: because most Arduinos have a built-in LED attached 
17  to pin 13 on the board, the LED is optional.
18  
19  
20  Created by David Cuartielles
21  modified 30 Aug 2011
22  By Tom Igoe
23  
24  This example code is in the public domain.
25  
26  http://arduino.cc/en/Tutorial/AnalogInput
27  
28  Modified by A.Manni for using a 2.4k Pot with 2 5k resistors
29  Range = (1024 - offset) *  1024 / (1024 - offset)
30  Range can be defined with map(sensorValue, 724, 0124, 0, 1024)
31  Note: range is inverted
32  With serial debugging.
33  - Added initial limit for delay 
34  
35  */
36
37 int sensorPin = A0;    // select the input pin for the potentiometer
38 int ledPin = 13;      // select the pin for the LED
39 int sensorValue = 0;  // variable to store the value coming from the sensor
40 int calValue = map(sensorValue, 723, 1024, 0, 1024) ;    // variable to store calibrated value for the Pot
41 // This could be done with a map()
42 void setup() {
43
44   pinMode(ledPin, OUTPUT);  // declare the ledPin as an OUTPUT:
45   Serial.begin(9600);
46
47 }
48
49 void loop() {
50   // read the value from the sensor:
51   sensorValue = analogRead(sensorPin);    
52
53     // turn the ledPin on
54     digitalWrite(ledPin, HIGH);  
55     // stop the program for <sensorValue> milliseconds:
56     delay(sensorValue);          
57     // turn the ledPin off:        
58     digitalWrite(ledPin, LOW);   
59     // stop the program for for <sensorValue> milliseconds:
60     // Range = (1024 - offset) *  1024 / (1024 - offset)
61     // calValue = (sensorValue -723 ) * 3.4 ; 
62     delay(calValue);
63     Serial.print("Sensor Value: ") ;
64     Serial.println(sensorValue);
65     Serial.print("Adjusted value: ") ;
66     Serial.println(calValue);
67  
68 }
69
70
71