Module 6: Conditionals with Buttons and Switches

Buttons and Switches on the Y-Badge

There are 3 buttons and 2 switches we can use to control lights and sound on the Y-Badge board. There is also a switch for powering the board from the battery, and two buttons used to configure the board. All of them are labeled.

Functions

The function to determine if a button is pressed is:

Yboard.get_button(<button_number>);

And the function to determine if a switch is ON is:

Yboard.get_switch(<switch_number>);

Each of these functions returns true if the button/switch is pressed or ON, and false otherwise. <button_number> or <switch_number> should be the number of the button or switch whose position you want to check as an int (eg. 1, 2, or 3).

What are Conditionals?

We use if statements to evaluate conditionals that are true or false:

if (Yboard.get_button(1)) {
    Yboard.set_led_color(1, 255, 0, 0);
}
More Details

This block of code tells our board to turn on LED1 if Button1 is pressed. The function Yboard.get_button() can be used as a conditional because it returns a true or false value.

Notice that in an if statement the conditional is placed in parenthesis and the commands to execute if it is true are placed in curly braces.

Additionally, we can chain conditionals together using && or ||. The && symbol is called a “logical AND”, and the || symbol is called “logical OR”. Here’s an example of using each:

if(Yboard.get_button(1) && Yboard.get_button(2)) {
    Yboard.set_led_color(1, 255, 0, 0);
}

if(Yboard.get_switch(1) || Yboard.get_switch(2)) {
    Yboard.set_led_color(1, 0, 0, 255);
}
More Details

The first if statement tells the board “if button 1 AND button 2 are pressed, turn led 1 red”. The second if statement tells the board “if switch 1 OR switch 2 is on, turn led 1 blue”.

There are several other important logical operators. For example, we can compare numbers or values using ==. For example, if (1 == 3)... will always be false and if (1 < 3)... will always be true. On the other hand, if (Yboard.get_button(1) == true)... will be true if button 1 is pressed and false if it is not. Because Yboard.get_button(1) already tells us this without comparing it, the == true part in this condition is not necessary.

If you want to learn about the other logical operators, google “cpp logical operators”.

Examples

We can use an else block to make the board do something else when the if block is false:

while(true) {
    if (Yboard.get_button(1)) {
        Yboard.set_led_color(1, 255, 0, 0);
    } else {
        Yboard.set_led_color(1, 0, 0, 0);
    }
}
Why The Infinite Loop?

If you copy one of the above examples into your program, and then try to upload it to your board and press the buttons, LED1 won’t turn on! The reason why is that the if statement is run immediately when you upload your code and it is only run once. In order to get the board to continuously check the state of Button1 and turn LED1 on if it is pressed, we need to wrap our if statement in a while loop.

Let’s try combining more loops and if statements!

We could get the board to only check the state of our button for the first 10 seconds after we program it, and then light up LED15 after those 10 seconds, like this:

for(int count = 0; count < 100; count++) {
    if (Yboard.get_button(1)) {
        Yboard.set_led_color(1, 255, 0, 0);
    } else {
        Yboard.set_led_color(1, 255, 0, 0);
    }
    delay(100);
}
Yboard.set_led_color(15, 255, 0, 0);
More Details

This code block only checks if Button1 is pressed in 0.1 second increments for 10 seconds. 0.1 seconds is faster than the average person can press the button, so it will work just fine, and once the for loop is done, you can continue writing your code as normal.

We can chain as many conditionals together as we want by using else if blocks:

while (true) {
    if (Yboard.get_button(1) && Yboard.get_button(2)) {
        Yboard.set_led_color(1, 255, 0, 0);
    } else if (Yboard.get_button(2) && Yboard.get_button(3)) {
        Yboard.set_led_color(1, 255, 255, 0);
    } else if (Yboard.get_button(1) && Yboard.get_button(3)) {
        Yboard.set_led_color(1, 0, 255, 0);
    } else {
        Yboard.set_led_color(1, 0, 0, 0);
    }
}
More Details

Chaining conditionals gives us a lot of freedom to make our code do exactly what we want it to. The above example turns LED1 red, yellow, or green depending on which two buttons we press.

Let’s try one final example using a switch instead of a button:

while (true) {
    if(Yboard.get_switch(2)) {
        while(Yboard.get_switch(2)) {
            Yboard.set_led_color(1, 255, 0, 0);
        }
    } else {
        while(Yboard.get_switch(2) == false) {
            Yboard.set_led_color(1, 0, 0, 0);
        }
    }
}
More Details

Woah! That looks a little scary! We have if statements inside of infinite loops, and more while loops inside of them. However, this code does exactly the same thing as our first example with a while loop above. If Switch2 on our board is ON, LED1 will turn on. And if Switch2 is OFF, LED2 will turn off.

There are a few important things to notice in this example:

  • The second conditional checks if Switch2 is NOT on using Yboard.get_switch(2) == false
  • There is almost always more than one way to write code that works. Be creative, and try to find a solution that is simple, clear, and fast.

Exploration

Remember to change main.cpp before continuing…

📝 NOTE: You will need to go to main.cpp and change the comments to call the correct activity function:

// loop_activity();
conditionals_activity();
  1. Reuse your code from the last module to make a single light circle around the board, but only if Switch1 is turned on.

  2. Make the light circle around in the opposite direction if Switch2 is on but if both switches are on or off, do nothing.

Challenges

Remember to comment/uncomment the correct function calls…

Remember to comment out the conditionals_exploration(); call in the conditionals_activity function and uncomment the correct challenge function:

conditionals_exploration();
// conditionals_challenge1();
// conditionals_challenge2();

Challenge 1: Make your board play a C note if Button1 is pressed, D note if Button2 is pressed, and E note if Button3 is pressed.

Hint

You can use a while loop inside your if statements to play the note for as long as you are holding the button. Use the same condition for both your if statements and your nested while loop. Set the duration on speaker_play_note to something small like 20ms.

Challenge 2: Use && and || to chain conditions together so that if any one button is pressed LED1 turns red, if any two are pressed, LED1 turns yellow, and if all three are pressed LED1 turns green. Can you make all of the lights do this instead of just LED1?

More Details

Use a for loop inside of your if statements.