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!
This project uses the same circuit we set up in Project 13. We will just be changing the code.
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:
1 2 3 4 5 | if ( some condition) { put all your commands between the curly brackets } |
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:
1 2 3 4 5 | if ( a>=b) { put all your commands between the curly brackets } |
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:
1 2 3 4 5 6 7 | a == b // Tests if a is eqaul to b. Note you must use TWO equal signs a != b //Tests if a in not equal to b. a < b //Tests if a is less than b a > b //Tests if a is greater than b a <= b //Tests if a is less than or equal to b a >= b //Tests if a is greater than or equal to b |
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:
1 2 3 4 5 | if (a>b && a>c) { put your commands here } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | int redPin=11; //set red LED pin to 11 int greenPin=10; //set green LED pin to 10 int bluePin=6; //set blue LED pin to 6 int brightness=255; //Set brightness to 75 String colorChoice; //Will hold users input of color choice void setup() { // put your setup code here, to run once: Serial.begin(9600); //Turn on Serial port pinMode(redPin, OUTPUT); //Set redPin to be an output pinMode(greenPin, OUTPUT); //Set greenPin to be an output pinMode(bluePin, OUTPUT); //set bluePin to be an output } void loop() { Serial.println("What color would you like the LED? (red, green, or blue)"); //Prompt user for color while (Serial.available()==0) { } //Wait for input colorChoice = Serial.readString(); if (colorChoice=="red") { analogWrite(redPin, brightness); //turn on red pin analogWrite(greenPin, 0); //turn off green pin analogWrite(bluePin, 0); //write off blue pin } if (colorChoice=="blue") { analogWrite(redPin, 0); //turn off red pin analogWrite(greenPin, 0); //turn off green pin analogWrite(bluePin, brightness); //write on blue pin } if (colorChoice=="green") { analogWrite(redPin, 0); //turn off red pin analogWrite(greenPin, brightness); //turn on green pin analogWrite(bluePin, 0); //write off blue pin } if (colorChoice!="red" && colorChoice!="green" && colorChoice != "blue") { Serial.println("That is not a valid color choice, please try again"); Serial.println(""); } } |
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.