LESSON 14: If Statements and Conditionals in Arduino

In our LED project in Lesson 13, we controlled the color of the LED by changing lines of code in the program. As you know, you really want to do that sort of thing by interacting with the user over the serial port. So, in this project, we are going to have the user set the color by indicating his preferred color in the serial monitor. Notice in our new picture below we have covered the LED with a ping pong ball. We did this so the LED will show better in the video, but after doing it, I really like it! It makes a cool little lamp. You can do this by creating a small hole the size of the LED in the ping pong ball, and then just mounting the ball on the LED. Cool stuff!

We have added a ping pong ball to the top of our RGB LED.

This project uses the same circuit we set up in Project 13. We will just be changing the code.

Circuit used to control RGB LED from an arduino

So, for this project, we will want the user to input his desired color for the LED. Then we will make the LED change to the color he requested. In order to do this, we need to learn a new programming command called the if statement. The if statement has a clause, or group of commands between a set of curly brackets. It executes those commands only if a condition is met. An if statement in arduino would look like this:

 

The commands between the curly brackets will be executed if the condition inside the parenthesis is true. Lets consider you had two variables, a and b. Lets say you only wanted to execute the commands in the clause if a is > or = to b. In that case you would put that condition in the parenthesis and your code would look like this:

The arduino would only execute the code if the condition was true. Otherwise it would jump to the first line of code after the closing curly bracket. There are a number of different condition tests that can be used in the parenthesis:

You can combine more than one condition by using the logical operators “and” or “or”. In arduino the symbol for “and” operation is && and the symbol for “or” operator is ||. (This is the symbol above the backslash on the keyboard). So you could have a condition where a is greater than b AND a is greater than c. This would look like:

 OK, so that is how if statements and conditional work. A key thing is that you have to remember than when you are testing for equality in an if statement, you use two equal signs. One of the common mistakes I see students make is to forget and just use one equal sign in a conditional.

What you will want to do now is write a program that will ask the user what color he would like the LED to be. In the prompt let him know that his choices are Red, Green, or Blue. Then turn the LED that color. You should also include a “test” . . . if he does not choose one of the allowed choices, he must choose again. Do your best to develop the code on your own, but if you get stuck, peak at my code below. You should not copy and paste my code, but just use it as a guide if you get stuck.

 OK, now your assignment is to move forward and have the program have more options for colors. Have it where you can turn the LED Red, Green, Blue, Cyan, Magenta, Yellow, Orange, Purple, Pink. Do some research on the internet to make sure you are really hitting these colors. Part of your grade will be how well you match the colors.