精品导航

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.

友情链接:蘑菇视频入口 | 蘑菇影视 | 蘑菇視頻 | 天天综合 | 蘑菇网页版入口 | 蘑菇tv网页版 | 天堂在线 | mogu | 蜜芽视频 | 日本高清 | 综合影院 | 琵琶妞 | 在线亚洲 | 玖玖爱 | 红桃视频 | 资源导航 | 迷妹网 | 福利盒子 | dna0126.com | 久久爱 | dk99999.com | 宅男影院 | zpzb2008.com | 六月婷婷 | hebeijiaoyuwang.com | 在线午夜 | 色妞 | 超碰视频 | 摩天轮社区 | 汤姆影院 | zhuoyemall.com | 伊人色 | mogu视频 | 樱花视频 | tissu-premier.com | 小草社区 | 久久香蕉 | gzdksw.com | 协和影院 | 秋霞影院 | 龙物视频 | 4008108885.com | 蘑菇视频ios | 蘑菇网 | 久久视频 | 亚洲精品 | 色啪 | 无名网 | 老司机视频 | 蘑菇视频网站 | 五福影院 | tsxhyq.com | 久久久久久 | mogutv | 青青伊人 | 亚洲网 | 秋霞网 | 永久地址 | 精品免费 | p站视频 | 午夜在线观看 | 导航大全 | 番号网 | rencai98.com | 五月丁香 | 蘑菇传媒 | 最新av | 蘑菇在线观看网页版 | mogus.cc | 酒色世界 | fltou.com | woqidz.com | 色婷婷 | 四虎影视 | jxty00.com | 琳琅导航 | 欧美午夜 | 蘑菇短视频入口 | 青草青青 | sanzhucare-zhpg.com | 看片神器 | 蘑菇传媒在线观看 | 第一福利 | 永久网站 | 青娱 | btupic.com | bt搜索 | 扒哥黑料 | 性知音 | 屁屁影院 | 玖玖资源站 | 老王影院 | 福利精品 | 青草草 | lf397.com | 蘑菇app | 蓝色导航 | 草莓视 | 小蓝视频