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 switch to turn on one of several different LEDs based on a byte of data received serially. The sketch listens for serial input, and turns on a different LED for the characters a, b, c, d, or e.
Step 1: What You Need?
1 x Arduino Board ( Arduino UNO R3 used in this tutorial.)
5 x LEDs (RED)
5 x 220 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. 5 x LEDs (RED)
5 x 220 Ohm Resistors
1 x Mini Breadboard
1 x USB Type-B Cable
Male-to-Male Jumper Wires
Optional
Step 2: Build Your Circuit.
To make this sketch work, your board must be connected to your computer. In the Arduino IDE open the serial monitor and send the characters a, b, c, d, or e to lit up the corresponding LED, or anything else to switch them off.
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 >> switchCase2
Click press the "upload" button (see the button with right arrow mark).
0 comments:
Post a Comment