While Loop
How to use a while loop to calibrate a sensor while a button is being read.
Sometimes you want everything in the program to stop while a given condition is true. You can do this using a while loop. This example shows how to use a while loop to calibrate the value of an analog sensor.
In the main loop, the sketch below reads the value of a photoresistor on analog pin 0 and uses it to fade an LED on pin 9. But while a button attached to digital pin 2 is pressed, the program runs a method called
calibrate()
that looks for the highest and lowest values of the analog sensor. When you release the button, the sketch continues with the main loop.This technique lets you update the maximum and minimum values for the photoresistor when the lighting conditions change.
Hardware Required
Arduino Board
pushbutton or switch
photoresistor or another analog sensor
2 10k ohm resistors
breadboard
Circuit
Connect your analog sensor (e.g. potentiometer, light sensor) on analog input 2 with a 10K ohm resistor to ground. Connect your button to digital pin, again with a 10K ohm resistor to ground. Connect your LED to digital pin 9, with a 220 ohm resistor in series.
Schematic
Code
1/*2
3 Conditionals - while statement4
5 This example demonstrates the use of while() statements.6
7 While the pushbutton is pressed, the sketch runs the calibration routine.8
9 The sensor readings during the while loop define the minimum and maximum of10
11 expected values from the photoresistor.12
13 This is a variation on the calibrate example.14
15 The circuit:16
17 - photoresistor connected from +5V to analog in pin 018
19 - 10 kilohm resistor connected from ground to analog in pin 020
21 - LED connected from digital pin 9 to ground through 220 ohm resistor22
23 - pushbutton attached from pin 2 to +5V24
25 - 10 kilohm resistor attached from pin 2 to ground26
27 created 17 Jan 200928
29 modified 30 Aug 201130
31 by Tom Igoe32
33 modified 20 Jan 201734
35 by Arturo Guadalupi36
37 This example code is in the public domain.38
39 https://www.arduino.cc/en/Tutorial/WhileLoop40
41*/42
43// These constants won't change:44
45const int sensorPin = A0; // pin that the sensor is attached to46
47const int ledPin = 9; // pin that the LED is attached to48
49const int indicatorLedPin = 13; // pin that the built-in LED is attached to50
51const int buttonPin = 2; // pin that the button is attached to52
53// These variables will change:54int sensorMin = 1023; // minimum sensor value55int sensorMax = 0; // maximum sensor value56int sensorValue = 0; // the sensor value57
58void setup() {59
60 // set the LED pins as outputs and the switch pin as input:61
62 pinMode(indicatorLedPin, OUTPUT);63
64 pinMode(ledPin, OUTPUT);65
66 pinMode(buttonPin, INPUT);67}68
69void loop() {70
71 // while the button is pressed, take calibration readings:72
73 while (digitalRead(buttonPin) == HIGH) {74
75 calibrate();76
77 }78
79 // signal the end of the calibration period80
81 digitalWrite(indicatorLedPin, LOW);82
83 // read the sensor:84
85 sensorValue = analogRead(sensorPin);86
87 // apply the calibration to the sensor reading88
89 sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);90
91 // in case the sensor value is outside the range seen during calibration92
93 sensorValue = constrain(sensorValue, 0, 255);94
95 // fade the LED using the calibrated value:96
97 analogWrite(ledPin, sensorValue);98}99
100void calibrate() {101
102 // turn on the indicator LED to indicate that calibration is happening:103
104 digitalWrite(indicatorLedPin, HIGH);105
106 // read the sensor:107
108 sensorValue = analogRead(sensorPin);109
110 // record the maximum sensor value111
112 if (sensorValue > sensorMax) {113
114 sensorMax = sensorValue;115
116 }117
118 // record the minimum sensor value119
120 if (sensorValue < sensorMin) {121
122 sensorMin = sensorValue;123
124 }125}
Learn more
You can find more basic tutorials in the built-in examples section.
You can also explore the language reference, a detailed collection of the Arduino programming language.
Last revision 2015/08/11 by SM
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.