Tuesday, March 19, 2013

Teensy Basics 4: If Statements

Overview
This article assumes that you have read Teensy Basics 1 and Teensy Basics 2 and Teensy Basics 3.

'If statements' are structural functions within a Teensy program that can branch your program to perform varying blocks of code depending on the state of 'something' - a variable, an input pin, whatever.




Usage
For example, maybe you've connected a series of buttons to your Teensy. Perhaps you want a different action to occur whenever you press each of these buttons. You can use If statements to add conditions to your - i.e. whether a given button is pressed or not - and then execute a given action - e.g. turn on an LED, play a sound, send a MIDI Note - if and only if a particular button is pressed.


If statements are powerful and very useful structural components to our code, and the great thing is that - as long as we think about things logically - they are easy to deal with.

If statements use boolean expressions and operators, to make a comparison and determine whether a particular condition is TRUE or FALSE. If it has been determined that a particular condition is TRUE at the time of testing, then a block of code is executed.

The syntax for a single, simple If statement is as follows:
if(value operator value) {
    do code
}







Logical Comparisons
As a concrete example, here is this syntax written in Arduino / Teensy:


This code basically asks the question: Is our pin 0 EQUAL TO 1?

And the response is, if the pin 0 is equal to 1, let's turn the onboard Teensy LED to ON. The following boolean operators to form our logical comparison are possible:

• ==    equals
• !=        does not equal
• > greater than, >= greater than or equal to
• < less than, <= less than or equal to


With this in mind, consider the following four statements. 






• In which cases is the code executed if time_value is equal to 100? Why?
• In which cases is the code executed if time_value is equal to 101? Why?
• In which cases is the code executed if time_value is equal to 99? Why?


IMPORTANT: Do not confuse '==' with '='. The first is a comparison.
e.g. Time_value == 100 is asking the question "does Time_value equal 100?"
whereas the second is setting the value of Time_value
e.g. Time_value = 100 is saying "let Time_value = 100"


Do not confuse these two!






Linking If Statements Together
If statements can be made more complex by adding additional logical components. Boolean expressions link multiple variables together, thereby effectively adding additional conditions that have or might have to become true in order for the If statement to execute its code.

The AND (&&) indicates that BOTH conditions must come TRUE in order for the code to execute, for example:


In this example, time_value must be less than 100 AND digitalRead(10) must equal 1 in order for this IF statement to be TRUE - i.e. both conditions must be true because of the && statement


The OR (||) indicates that EITHER OR condition must come TRUE in order for the code to execute, for example:
In this example, time_value must be less than 100 OR digitalRead(1) must equal 1 in order for this IF statement to be TRUE - i.e. either condition can be true, but both don't have to be true because of the || statement






Using the Else / Else If Statement
The else statement can come after an If statement, and contains code that will execute if the conditions in the original If statement are not TRUE.

If that sounds complex, consider the following sentence.

"I will go to the go to the shop IF it is OPEN, ELSE I will go to the park."

This summarises the link between the IF and ELSE statements quite concisely. In terms of a concrete example in Teensy code, consider the following block:


This is the code equivalent of:
"If button 1 is pressed, turn the LED on, else turn the LED off"



We can further add a logical structure by using an Else If statement, like so:


These are very simple examples of these types of statements, but more complex programming will involve using more complex arrangements of If statements. The basic structure, however, will remain the same.




Nested If Statements
Keep in mind that you can have one If statement located within the code of another If statement - we can nest our If statements within each other.

This is really useful for testing for a number of different types of conditions that should only occur after another condition comes true first.

On a music data level, we might use nested to program a Teensy to react to a certain controller number on a certain controller channel.

For example:


In this case, first we test for MIDI channel 1, followed by controller 1. If it is controller 1, then we do something. If the controller is number 11, however, then we do something else.





Conclusion
With all of these If statements, the key is to 1) think about your problem or outcome that you want your program to achieve 2) develop a logical flow of how to achieve that outcome 3) match up If statements, conditions and comparisons to the appropriate point in the program flow.

If statements are meant to be easy to read - like a sentence - and follow a straightforward logic. Don't be overwhelmed by If statements with multiple parts or an If statement followed by an Else If and then an Else, simply break it down and think about each part individually. 

0 comments: