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.

circuit

Schematic

schematic

Code

1/*
2
3 Conditionals - while statement
4
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 of
10
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 0
18
19 - 10 kilohm resistor connected from ground to analog in pin 0
20
21 - LED connected from digital pin 9 to ground through 220 ohm resistor
22
23 - pushbutton attached from pin 2 to +5V
24
25 - 10 kilohm resistor attached from pin 2 to ground
26
27 created 17 Jan 2009
28
29 modified 30 Aug 2011
30
31 by Tom Igoe
32
33 modified 20 Jan 2017
34
35 by Arturo Guadalupi
36
37 This example code is in the public domain.
38
39 http://www.arduino.cchttps://www.arduino.cc/en/Tutorial/WhileLoop
40
41*/
42
43// These constants won't change:
44
45const int sensorPin = A0; // pin that the sensor is attached to
46
47const int ledPin = 9; // pin that the LED is attached to
48
49const int indicatorLedPin = 13; // pin that the built-in LED is attached to
50
51const int buttonPin = 2; // pin that the button is attached to
52
53// These variables will change:
54int sensorMin = 1023; // minimum sensor value
55int sensorMax = 0; // maximum sensor value
56int sensorValue = 0; // the sensor value
57
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 period
80
81 digitalWrite(indicatorLedPin, LOW);
82
83 // read the sensor:
84
85 sensorValue = analogRead(sensorPin);
86
87 // apply the calibration to the sensor reading
88
89 sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
90
91 // in case the sensor value is outside the range seen during calibration
92
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 value
111
112 if (sensorValue > sensorMax) {
113
114 sensorMax = sensorValue;
115
116 }
117
118 // record the minimum sensor value
119
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

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.