1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-02-21 15:54:39 +01:00
This commit is contained in:
Tom Igoe 2009-06-17 21:26:02 +00:00
parent ca651419c5
commit f886662ea0

View File

@ -1,27 +1,48 @@
/* /*
* AnalogInput Analog Input
* by DojoDave <http://www.0j0.org> 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.
* Turns on and off a light emitting diode(LED) connected to digital The amount of time the LED will be on and off depends on
* pin 13. The amount of time the LED will be on and off depends on the value obtained by analogRead().
* the value obtained by analogRead(). In the easiest case we connect
* a potentiometer to analog pin 2. The circuit:
* * Potentiometer attached to analog input 0
* http://www.arduino.cc/en/Tutorial/AnalogInput * 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 16 Jun 2009
By Tom Igoe
http://arduino.cc/en/Tutorial/AnalogInput
*/ */
int potPin = 2; // select the input pin for the potentiometer int sensorPin = 0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED int ledPin = 13; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor int sensorValue = 0; // variable to store the value coming from the sensor
void setup() { void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT // declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
} }
void loop() { void loop() {
val = analogRead(potPin); // read the value from the sensor // read the value from the sensor:
digitalWrite(ledPin, HIGH); // turn the ledPin on sensorValue = analogRead(sensorPin);
delay(val); // stop the program for some time // turn the ledPin on
digitalWrite(ledPin, LOW); // turn the ledPin off digitalWrite(ledPin, HIGH);
delay(val); // stop the program for some time // stop the program for <sensorValue> milliseconds:
} delay(sensorValue);
// turn the ledPin off:
digitalWrite(ledPin, LOW);
// stop the program for for <sensorValue> milliseconds:
delay(sensorValue);
}