Visačka na diskotéku: Rozdiel medzi revíziami
Zo stránky SensorWiki
Bez shrnutí editace |
Bez shrnutí editace |
||
Riadok 27: | Riadok 27: | ||
</source></tab> | </source></tab> | ||
<tab name="Python"><source lang="python" style="background: LightBlue;"> | <tab name="Python"><source lang="python" style="background: LightBlue;"> | ||
from microbit import * | from microbit import * | ||
display.show(Image.YES) | |||
while True: | |||
if button_a.is_pressed() and button_b.is_pressed(): | |||
display.show(Image.HEART) | |||
sleep(500) | |||
elif button_a.is_pressed(): | |||
display.show(Image.HAPPY) | |||
elif button_b.is_pressed(): | |||
display.show(Image.SAD) | |||
</source></tab> | </source></tab> | ||
<tab name="Arduino C++"><syntaxhighlight lang=c style="background: Cornsilk"> | <tab name="Arduino C++"><syntaxhighlight lang=c style="background: Cornsilk"> |
Verzia z 12:24, 21. jún 2020
Vyrobíme jednoduchú visačku na diskotéku. Po zapnutí sa na displeji zobrazí znak ✓, ktorým dáme na javo, že zariadenie je pripravené. Emocionálny stav nositeľa tejto visačky sa dá meniť tlačidlami. Po stlačení ľavého tlačidla 🅐 sa zobrazí na displeji veselý smajlík 😀, po stlačení 🅑 sa zobrazí smutný smajlík ☹ a po stlačení oboch 🅐+🅑 súčasne sa zobrazí "tajný symbol" ❤ pre toho pravého...
-
Prvý program...
-
...bude vyzerať v simulátore takto.
Visacka na diskoteku:
basic.showIcon(IconNames.Yes)
input.onButtonPressed(Button.A, function () {
basic.showIcon(IconNames.Happy)
})
input.onButtonPressed(Button.B, function () {
basic.showIcon(IconNames.Sad)
})
input.onButtonPressed(Button.AB, function () {
basic.showIcon(IconNames.Heart)
})
from microbit import *
display.show(Image.YES)
while True:
if button_a.is_pressed() and button_b.is_pressed():
display.show(Image.HEART)
sleep(500)
elif button_a.is_pressed():
display.show(Image.HAPPY)
elif button_b.is_pressed():
display.show(Image.SAD)
#include "MicroBit.h"
MicroBit uBit;
MicroBitImage HEART("0,1,0,1,0 \n"
"1,1,1,1,1 \n"
"1,1,1,1,1 \n"
"0,1,1,1,0 \n"
"0,0,1,0,0 \n");
MicroBitImage SMILEY_HAPPY("0,0,0,0,0\n 0,1,0,1,0\n 0,0,0,0,0\n 1,0,0,0,1\n 0,1,1,1,0\n");
MicroBitImage SMILEY_SAD("0,0,0,0,0\n 0,1,0,1,0\n 0,0,0,0,0\n 0,1,1,1,0\n 1,0,0,0,1\n");
MicroBitImage YES("0,0,0,0,0\n 0,0,0,0,1\n 0,0,0,1,0\n 1,0,1,0,0\n 0,1,0,0,0\n");
void onButtonA(MicroBitEvent e)
{
uBit.display.print(SMILEY_HAPPY);
}
void onButtonB(MicroBitEvent e)
{
uBit.display.print(SMILEY_SAD);
}
void onButtonAB(MicroBitEvent e)
{
uBit.display.print(HEART);
}
int main()
{
uBit.init(); // setup
// Register to receive events when any buttons are clicked, including the A+B virtual button (both buttons at once).
uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
uBit.messageBus.listen(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_CLICK, onButtonAB);
uBit.display.print(YES);
// We're done, so just enter a power efficient sleep while we wait for an event.
while (1)
uBit.sleep(10000);
}
Späť na zoznam príkladov....