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.
Step 1: What You Need?
1 x Arduino Board ( Arduino UNO R3 used in this tutorial.)
1 x Analog Sensor ( Photoresistor used in this tutorial.)
2 x 10k Ohm Resistors
1 x Pushbutton or Switch
1 x LED (RED)
1 x 220 Ohm Resistor
1 x Mini Breadboard
1 x USB Type-B Cable
Male-to-Male Jumper Wires
Optional
Don't have components? Don't worry. Just click the component's name. 1 x Analog Sensor ( Photoresistor used in this tutorial.)
2 x 10k Ohm Resistors
1 x Pushbutton or Switch
1 x LED (RED)
1 x 220 Ohm Resistor
1 x Mini Breadboard
1 x USB Type-B Cable
Male-to-Male Jumper Wires
Optional
Step 2: Build Your Circuit.
Step 3: Upload The Code.
2. Find the port number by accessing device manager on Windows. See the section Port (COM&LPT) and look for an open port named "Arduino Uno (COMxx)". If you are using a different board, you will find a name accordingly. What matters is the xx in COMxx part. In my case, it's COM3. So my port number is 3.
Select the right port: Tools >> Port >> Select the port number.
3. You can find this code in the example of Arduino IDE.
Select File >> Examples >> 05.Control >> WhileStatementConditional
Click press the "upload" button (see the button with right arrow mark).
0 comments:
Post a Comment