Debian Codegraph @

examples/05.Control/IfStatementConditional/IfStatementConditional.ino

raw
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
7 analog value regardless of the level.
8
9 The circuit:
10 - potentiometer
11 Center pin of the potentiometer goes to analog pin 0.
12 Side pins of the potentiometer go to +5V and ground.
13 - LED connected from digital pin 13 to ground through 220 ohm resistor
14
15 - Note: On most Arduino boards, there is already an LED on the board connected
16 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
22 This example code is in the public domain.
23
24 https://www.arduino.cc/en/Tutorial/BuiltInExamples/ifStatementConditional
25 */
26
27 // These constants won't change:
28 const int analogPin = A0; // pin that the sensor is attached to
29 const int ledPin = 13; // pin that the LED is attached to
30 const int threshold = 400; // an arbitrary threshold level that's in the range of the analog input
31
32 void setup() {
33 // initialize the LED pin as an output:
34 pinMode(ledPin, OUTPUT);
35 // initialize serial communications:
36 Serial.begin(9600);
37 }
38
39 void loop() {
40 // read the value of the potentiometer:
41 int analogValue = analogRead(analogPin);
42
43 // if the analog value is high enough, turn on the LED:
44 if (analogValue > threshold) {
45 digitalWrite(ledPin, HIGH);
46 } else {
47 digitalWrite(ledPin, LOW);
48 }
49
50 // print the analog value:
51 Serial.println(analogValue);
52 delay(1); // delay in between reads for stability
53 }