source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/examples/05.Control/IfStatementConditional/IfStatementConditional.ino @ 4837

Last change on this file since 4837 was 4837, checked in by daduve, 2 years ago

Adding new version

File size: 1.6 KB
Line 
1/*
2  Conditionals - If statement
3
4 This example demonstrates the use of if() statements.
5 It reads the state of a potentiometer (an analog input) and turns on an LED
6 only if the potentiometer goes above a certain threshold level. It prints the analog value
7 regardless of the level.
8
9 The circuit:
10 * potentiometer connected to analog pin 0.
11 Center pin of the potentiometer goes to the analog pin.
12 side pins of the potentiometer go to +5V and ground
13 * LED connected from digital pin 13 to ground
14
15 * Note: On most Arduino boards, there is already an LED on the board
16 connected to pin 13, so you don't need any extra components for this example.
17
18 created 17 Jan 2009
19 modified 9 Apr 2012
20 by Tom Igoe
21
22This example code is in the public domain.
23
24http://www.arduino.cc/en/Tutorial/IfStatement
25
26 */
27
28// These constants won't change:
29const int analogPin = A0;    // pin that the sensor is attached to
30const int ledPin = 13;       // pin that the LED is attached to
31const int threshold = 400;   // an arbitrary threshold level that's in the range of the analog input
32
33void setup() {
34  // initialize the LED pin as an output:
35  pinMode(ledPin, OUTPUT);
36  // initialize serial communications:
37  Serial.begin(9600);
38}
39
40void loop() {
41  // read the value of the potentiometer:
42  int analogValue = analogRead(analogPin);
43
44  // if the analog value is high enough, turn on the LED:
45  if (analogValue > threshold) {
46    digitalWrite(ledPin, HIGH);
47  } else {
48    digitalWrite(ledPin, LOW);
49  }
50
51  // print the analog value:
52  Serial.println(analogValue);
53  delay(1);        // delay in between reads for stability
54}
55
Note: See TracBrowser for help on using the repository browser.