2011-08-30 21:33:32 +02:00
|
|
|
/*
|
|
|
|
Conditionals - while statement
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-15 00:30:34 +02:00
|
|
|
This example demonstrates the use of while() statements.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-15 00:30:34 +02:00
|
|
|
While the pushbutton is pressed, the sketch runs the calibration routine.
|
2017-07-14 21:34:00 +02:00
|
|
|
The sensor readings during the while loop define the minimum and maximum of
|
|
|
|
expected values from the photoresistor.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-15 00:30:34 +02:00
|
|
|
This is a variation on the calibrate example.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-15 00:30:34 +02:00
|
|
|
The circuit:
|
|
|
|
- photoresistor connected from +5V to analog in pin 0
|
2017-07-12 22:18:23 +02:00
|
|
|
- 10 kilohm resistor connected from ground to analog in pin 0
|
2017-07-15 00:30:34 +02:00
|
|
|
- LED connected from digital pin 9 to ground through 220 ohm resistor
|
|
|
|
- pushbutton attached from pin 2 to +5V
|
2017-07-12 22:18:23 +02:00
|
|
|
- 10 kilohm resistor attached from pin 2 to ground
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-15 00:30:34 +02:00
|
|
|
created 17 Jan 2009
|
|
|
|
modified 30 Aug 2011
|
|
|
|
by Tom Igoe
|
|
|
|
modified 20 Jan 2017
|
|
|
|
by Arturo Guadalupi
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-15 00:30:34 +02:00
|
|
|
This example code is in the public domain.
|
2011-08-30 21:33:32 +02:00
|
|
|
|
2017-07-15 00:30:34 +02:00
|
|
|
http://www.arduino.cc/en/Tutorial/WhileLoop
|
2017-07-15 00:35:58 +02:00
|
|
|
*/
|
2011-08-30 21:33:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
// These constants won't change:
|
2017-01-20 09:40:07 +01:00
|
|
|
const int sensorPin = A0; // pin that the sensor is attached to
|
2011-08-30 21:33:32 +02:00
|
|
|
const int ledPin = 9; // pin that the LED is attached to
|
|
|
|
const int indicatorLedPin = 13; // pin that the built-in LED is attached to
|
|
|
|
const int buttonPin = 2; // pin that the button is attached to
|
|
|
|
|
|
|
|
|
|
|
|
// These variables will change:
|
|
|
|
int sensorMin = 1023; // minimum sensor value
|
|
|
|
int sensorMax = 0; // maximum sensor value
|
|
|
|
int sensorValue = 0; // the sensor value
|
|
|
|
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
// set the LED pins as outputs and the switch pin as input:
|
|
|
|
pinMode(indicatorLedPin, OUTPUT);
|
2015-07-06 15:18:33 +02:00
|
|
|
pinMode(ledPin, OUTPUT);
|
|
|
|
pinMode(buttonPin, INPUT);
|
2011-08-30 21:33:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
// while the button is pressed, take calibration readings:
|
|
|
|
while (digitalRead(buttonPin) == HIGH) {
|
2013-10-21 09:58:40 +02:00
|
|
|
calibrate();
|
2011-08-30 21:33:32 +02:00
|
|
|
}
|
|
|
|
// signal the end of the calibration period
|
2013-10-21 09:58:40 +02:00
|
|
|
digitalWrite(indicatorLedPin, LOW);
|
2011-08-30 21:33:32 +02:00
|
|
|
|
|
|
|
// read the sensor:
|
|
|
|
sensorValue = analogRead(sensorPin);
|
|
|
|
|
|
|
|
// apply the calibration to the sensor reading
|
|
|
|
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
|
|
|
|
|
|
|
|
// in case the sensor value is outside the range seen during calibration
|
|
|
|
sensorValue = constrain(sensorValue, 0, 255);
|
|
|
|
|
|
|
|
// fade the LED using the calibrated value:
|
|
|
|
analogWrite(ledPin, sensorValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void calibrate() {
|
|
|
|
// turn on the indicator LED to indicate that calibration is happening:
|
|
|
|
digitalWrite(indicatorLedPin, HIGH);
|
|
|
|
// read the sensor:
|
|
|
|
sensorValue = analogRead(sensorPin);
|
|
|
|
|
|
|
|
// record the maximum sensor value
|
|
|
|
if (sensorValue > sensorMax) {
|
|
|
|
sensorMax = sensorValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// record the minimum sensor value
|
|
|
|
if (sensorValue < sensorMin) {
|
|
|
|
sensorMin = sensorValue;
|
|
|
|
}
|
|
|
|
}
|