1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-05 16:24:12 +01:00
Arduino/build/shared/examples/5.Control/IfStatementConditional/IfStatementConditional.pde

57 lines
1.5 KiB
Plaintext
Raw Normal View History

2009-07-11 02:34:59 +02:00
/*
Conditionals - If statement
This example demonstrates the use of if() statements.
It reads the state of a potentiometer (an analog input) and turns on an LED
only if the LED goes above a certain threshold level. It prints the analog value
regardless of the level.
The circuit:
* potentiometer connected to analog pin 0.
Center pin of the potentiometer goes to the analog pin.
side pins of the potentiometer go to +5V and ground
* LED connected from digital pin 13 to ground
* Note: On most Arduino boards, there is already an LED on the board
connected to pin 13, so you don't need any extra components for this example.
created 17 Jan 2009
modified 4 Sep 2010
2009-07-11 02:34:59 +02:00
by Tom Igoe
2010-02-24 04:04:35 +01:00
This example code is in the public domain.
2009-07-11 02:34:59 +02:00
2010-02-24 04:04:35 +01:00
http://arduino.cc/en/Tutorial/IfStatement
2009-07-11 02:34:59 +02:00
*/
// These constants won't change:
const int analogPin = A0; // pin that the sensor is attached to
2009-07-11 02:34:59 +02:00
const int ledPin = 13; // pin that the LED is attached to
const int threshold = 400; // an arbitrary threshold level that's in the range of the analog input
void setup() {
// initialize the LED pin as an output:
2009-10-18 21:41:30 +02:00
pinMode(ledPin, OUTPUT);
2009-07-11 02:34:59 +02:00
// initialize serial communications:
Serial.begin(9600);
}
void loop() {
// read the value of the potentiometer:
int analogValue = analogRead(analogPin);
// if the analog value is high enough, turn on the LED:
if (analogValue > threshold) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin,LOW);
}
// print the analog value:
Serial.println(analogValue, DEC);
}