Pressing the buttons
Every Ringo is equipped with 18 buttons and a two-axis joystick, so it would be a shame not to use them.
We're going to focus here on using the functions of drawing on the screen, but this time we're controlling it with a simple press of a button.
This gives us much more flexibility and dramatically improves the possibilities of a program.
Here is the first time we're introduced with the most basic logical function of all - if. This one, along with other logical functions can be found in the Loop section at the top of the section list.
What if does is it checks the condition and if the condition is true, executes the code inside it.
If the condition is not met and considered false, the program will not execute the code inside the function.
There is also a more advanced version of this function called if-else, which executes another part of the code if the original condition is not met.
Our conditions in these examples are checking whether the buttons have been pressed or not.
For that, we're using the function mp.buttons.pressed(BUTTON) whose block can be found in the I/O section.
This function will return a true boolean value if the specified button has been pressed and false if it hasn't.
It can also be modified that it checks whether the button has been released.
Every time you press the button the screen changes color and you get an indicator which button has been pressed.
Pretty much every button works this way, besides, of course, the joystick.
Joystick
This component works very differently than the buttons.
It has two main values, X-axis and Y-axis. Both of those variables can take a value between 0 and 1023.
When the joystick is in the middle, they will take the middle value. Since the precision of it isn't perfect, that will be somewhere around 500.
As you move the joystick left the value of the X-axis increases and while moving it to the right, decreases.
Moving the joystick up decreases the Y value and moving in down increases it.
This logic is a little bit different than the default screen x and y pixel location values, so be careful when working with it.
Where is joystick right now?
Function mp.buttons.getJoystickX() and mp.buttons.getJoystickY() returns the current value of those variables, thus allowing you to check the joystick's current location.
Green Math functions are used for changing variable values, just like we did here.
The code is simple enough and it should be no problem to understand it.
Well, of course you can!
The logic behind this next example is a bit more complex, so make sure everyone pays close attention.
Even though we cannot do much with the exact value, we can use it for comparison.
The timer starts counting milliseconds every time we turn on the phone, so when this function is called, we'll get a specific timestamp back.
Then we can call that function a little bit later to determine how many milliseconds have been since the last time we've called it.
Function mp.buttons.getJoystickX() and mp.buttons.getJoystickY() returns the current value of those variables, thus allowing you to check the joystick's current location.
Let's see this in an example.
Every time the joystick is pushed up or down, the circle will change its location until it gets to the edge of the screen.
Green Math functions are used for changing variable values, just like we did here.
The code is simple enough and it should be no problem to understand it.
Buttons and time
Just pressing the buttons is cool, but what about long presses? Can you create a program that does different things depending on how long the button has been pressed?
Well, of course you can!
The logic behind this next example is a bit more complex, so make sure everyone pays close attention.
Here we see the appearance of one very important function - millis(). What millis() does is it returns some unknown time value depending on when it's called.
Even though we cannot do much with the exact value, we can use it for comparison.
The timer starts counting milliseconds every time we turn on the phone, so when this function is called, we'll get a specific timestamp back.
Then we can call that function a little bit later to determine how many milliseconds have been since the last time we've called it.
Comparing two timestamps
The values are compared just like two regular numbers. So if we want to know has it been more than two seconds between the two calls, just add 2000 milliseconds to the first value or subtract 2000 milliseconds from the second value before comparing.
The values are compared just like two regular numbers. So if we want to know has it been more than two seconds between the two calls, just add 2000 milliseconds to the first value or subtract 2000 milliseconds from the second value before comparing.
In this example, we enter the specific subsection of the code only after the number one has been pressed for two seconds.
At that moment the LED color changes and we get a cool effect. You can use this function in many ways, not only for buttons.
BackNext