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.

random image

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.

Let's see this in an example.

random image

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.

random image

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.Ā 

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.