An if statement allows you to choose between two discrete options, TRUE or FALSE. When there are more than two options, you can use multiple if statements, or you can use the switch statement. Switch allows you to choose between several discrete options. This tutorial shows you how to use it to switch between four desired states of a photoresistor: really dark, dim, medium, and bright.
This program first reads the photoresistor. Then it uses the map() function to map its output to one of four values: 0, 1, 2, or 3. Finally, it uses the switch() statement to print one of four messages back to the computer depending on which of the four values is returned.
Step 1: What You Need?
1 x Arduino Board ( Arduino UNO R3 used in this tutorial.)
1 x Analog Sensor ( Potentiometer, Photoresistor, etc.)
1 x 10k Ohm Resistors
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 ( Potentiometer, Photoresistor, etc.)
1 x 10k Ohm Resistors
1 x Mini Breadboard
1 x USB Type-B Cable
Male-to-Male Jumper Wires
Optional
Step 2: Build Your Circuit.
The photoresistor is connected to analog in pin 0 using a voltage divider circuit. A 10K ohm resistor makes up the other side of the voltage divider, running from Analog in 0 to ground. The analogRead() function returns a range of about 0 to 600 from this circuit in a reasonably lit indoor space.Step 3: Upload The Code.
1. Select the Arduino board type: Select Tools >> Board >> Select your correct Arduino board used.
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 >> switchCase
Click press the "upload" button (see the button with right arrow mark).
Download:
Arduino IDESources:https://www.arduino.cc/en/Tutorial/SwitchCase
0 comments:
Post a Comment