How to Program the Remote Control
This lesson will go over how to program the remote control found inside the Rokit Smart Kit.
First, begin the code by including the smart inventor library. Enter the following before the [crayon-670b153aa729a864748933-i/] method.
[crayon-670b153aa72a0389714556/]
Learn More: What’s a library?
A library is a saved set of [crayon-670b153aa72a1196631043-i/] and variables that provide users with improved functionality when coding.
Any code that starts with [crayon-670b153aa72a2282596263-i/] is a function in the SmartInventor library. Any variable that highlights ORANGE, is an initialized variable from an included library.
Next, inside [crayon-670b153aa72a4894963463-i/] ,enter the following to initialize the use of the remote control within the sketch.
[crayon-670b153aa72a5870763924/]
After establishing use of the remote control, you will have to establish serial communication with the board.
[crayon-670b153aa72a6883540889/]
Serial.begin( )
Serial.Begin( BAUD rate);
This function will establish a high transfer BAUD connection between the remote and the board. The higher the BAUD number the faster the board will process information, but may yield a high chance for errors. The lower the BAUD number the slower the board will process information, but may yield a low chance for errors.
Acceptable BAUD rates range from 300 to 250000. Make sure that the BAUD rate is a number that is found within the lower right corner of the Serial Monitor.
Next, input a [crayon-670b153aa72a7188066883-i/] to assign a time step for how fast the board should check for entries from the remote control.
[crayon-670b153aa72a8601236895/]
Next, inside [crayon-670b153aa72aa666532083-i/], enter the following
[crayon-670b153aa72b4333783374/]
This will create a global variable named “keyData” that will store the information from the remote regarding the most recent button pressed.
Next, we will go over how to create and use an “if, else if, and else statement”
This will make it so that “if” you press a particular button on the remote control, then a corresponding text will be printed. The first “if” statement will be as follows
[crayon-670b153aa72b5470005444/]
This initializes a condition statement to check that “if” an action is met perform the action within the braces { }. In this case add a print command into the position of the [crayon-670b153aa72b6701778367-i/] and it will perform the desired function. The “if” statement should resemble the following
[crayon-670b153aa72b7702935151/]
Next, create an “else if” statement but for “KEY_D”.
[crayon-670b153aa72b8203664238/]
Lastly, assign a command for what happens when none of the above conditions are met. This is done by creating an “else” statement.
[crayon-670b153aa72b9964977093/]
if, else if, and else
If statements allow for the user to create conditions, and assign actions when those conditions are met.
[crayon-670b153aa72ba661873426/]
Else if statements allow the user to stay within the original “if” statement and indicate alternate conditions to perform a new action based on different conditions.
[crayon-670b153aa72bb478387379/]
Else statements will encompass all conditions not specified. This will be the go-to action for conditions other than those listed in the if and else if statements.
[crayon-670b153aa72bc700810413/]
When creating “if” statements keep in mind that they must meet the following
- For every if statement, there can only be one else statement
- There can be more than one else if statement within the same if statement
Activity:
Program an action for all of the available key inputs on the remote: [crayon-670b153aa72bd296356162-i/]
Final Code
[crayon-670b153aa72be900145186/]