Arduino LESSON 14: If Statements and Conditionals in Arduino

Arduino LESSON 14: If Statements and Conditionals in Arduino

Arduino LESSON 14: If Statements and Conditionals in Arduino

We changed the colour of the LED in our LED project in Lesson 13 by altering lines of code in the programme. As you are aware, communicating with the user via the serial port is exactly what you want to accomplish. In this project, the user will set the colour by expressing his chosen colour in the serial monitor. In our updated image below, we've covered the LED with a ping pong ball. We did this to make the LED stand out more in the movie, but after doing so, I really enjoy it! It's a neat tiny lamp. You may accomplish this by drilling a tiny hole in the ping pong ball the size of the LED and then simply putting the ball on the LED. That's awesome!

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

This project makes use of the same circuit that we built in Project 13. We'll simply change the code.

Circuit used to control RGB LED from an arduino

So, for this project, we'll ask the user to select a colour for the LED. The LED will then be changed to the colour he desired. To do this, we must first learn a new programming command known as the if statement. The if statement has a clause, or group of commands, enclosed by curly brackets. It only executes those commands if a certain condition is satisfied. In arduino, an if statement might look like this:

if ( some condition) {

put all your commands between the curly brackets

}

If the condition within the parenthesis is true, the commands inside the curly brackets will be performed. Assume you have two variables, a and b. Assume you only intended to run the instructions in the clause if a is > or = to b. In that scenario, you'd place that condition in parentheses, and your code would look something like this:

if ( a>=b) {

put all your commands between the curly brackets

}

If the condition was true, the arduino would only execute the code. Otherwise, it would go to the first line of code following the closing curly bracket. There are many condition tests that may be used in the parenthesis:

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

Using the logical operators "and" or "or," you may combine several conditions.
The symbol for "and" operation in Arduino is &&, and the symbol for "or" operation is ||.
(On the keyboard, this is the sign above the backslash.)
As an example, suppose a is greater than b AND a is greater than c.
This is how it would look:

if (a>b && a>c) {

put your commands here

}

So that's how if statements and conditional statements operate. It is critical to remember that when testing for equality in an if statement, you must use two equal signs. One of the most common errors I see students make is forgetting to utilise more than one equal sign in a conditional.

What you should do now is build a program that asks the user what colour he wants the LED to be. Let him know that his options are Red, Green, or Blue in the prompt. Then, change the colour of the LED. You could also include a "test"... if he does not select one of the available options, he must do it again. Make every effort to create the code on your own, but if you get stuck, have a look at my code below. You should not copy and paste my code, but rather use it as a reference if you get stuck.

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 it's your turn to go on and give the program more colour possibilities. Have it set up such that you can change the colour of the LED to red, green, blue, cyan, magenta, yellow, orange, purple, or pink. Make sure you're hitting these hues by doing some internet study. Your grade will be determined in part by how closely you match the colours.

What's Your Reaction?

like
0
dislike
0
love
0
funny
0
angry
0
sad
0
wow
0