microbit counter

Dependencies:   microbit

Committer:
nhat200687
Date:
Wed Aug 12 07:14:55 2020 +0000
Revision:
1:c9986319b0ea
Parent:
0:0b0dbfb5f32a
update counter (2)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nhat200687 0:0b0dbfb5f32a 1 #include "MicroBit.h"
nhat200687 0:0b0dbfb5f32a 2
nhat200687 0:0b0dbfb5f32a 3 MicroBit uBit;
nhat200687 1:c9986319b0ea 4 int counter = 0;
nhat200687 0:0b0dbfb5f32a 5
nhat200687 0:0b0dbfb5f32a 6 void showCounter() {
nhat200687 1:c9986319b0ea 7 uBit.display.print(counter);
nhat200687 0:0b0dbfb5f32a 8 }
nhat200687 0:0b0dbfb5f32a 9
nhat200687 0:0b0dbfb5f32a 10 void button_A_Clicked(MicroBitEvent me) {
nhat200687 0:0b0dbfb5f32a 11 counter-= (counter == 0 ? 0 : 1);
nhat200687 0:0b0dbfb5f32a 12 showCounter();
nhat200687 0:0b0dbfb5f32a 13 }
nhat200687 0:0b0dbfb5f32a 14
nhat200687 0:0b0dbfb5f32a 15 void button_B_Clicked(MicroBitEvent me) {
nhat200687 0:0b0dbfb5f32a 16 counter+= (counter == 99 ? -99 : 1);
nhat200687 0:0b0dbfb5f32a 17 showCounter();
nhat200687 0:0b0dbfb5f32a 18 }
nhat200687 0:0b0dbfb5f32a 19
nhat200687 0:0b0dbfb5f32a 20
nhat200687 0:0b0dbfb5f32a 21 int main() {
nhat200687 0:0b0dbfb5f32a 22 //muon viet gi viet, truoc het phai khoi dong.
nhat200687 0:0b0dbfb5f32a 23 uBit.init();
nhat200687 0:0b0dbfb5f32a 24
nhat200687 0:0b0dbfb5f32a 25 //dang ky lang nghe su kien khi nhan nut A hoac B
nhat200687 0:0b0dbfb5f32a 26 uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, button_A_Clicked);
nhat200687 0:0b0dbfb5f32a 27 uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, button_B_Clicked);
nhat200687 0:0b0dbfb5f32a 28
nhat200687 0:0b0dbfb5f32a 29 //giai phong tien trinh nay, tuy nhien, main van se nhan tin hieu tu cac EVENT
nhat200687 0:0b0dbfb5f32a 30 release_fiber();
nhat200687 0:0b0dbfb5f32a 31 }