2018年9月25日 星期二

micro:bit 示範程式


早前想買一塊 BBC 的 micro:bit 來玩玩;到大阪日本橋時找到它,但最後沒有購買。昨天,女兒從中學帶回來一塊 micro:bit 及 robot:bit,於是我研究了一下,並編寫了第一個程式:

//----------------------------------------------------------------------------------------
//  micro:bit DEMO Program 01
//----------------------------------------------------------------------------------------
//  Platform: micro:bit Javascript or Blocks
//  Written by Pacess HO
//  Copyright Pacess Studio, 2018.  All rights reserved.
//----------------------------------------------------------------------------------------

let x = 0
let y = 0
let led1 = 0
let led2 = 0
let led3 = 0
let led4 = 0
let step = 0
let offset = 0
let direction = 0

led1 = 0
led2 = 50
led3 = 100
led4 = 150
step = 20
offset = 0
direction = 1

music.beginMelody(music.builtInMelody(Melodies.Birthday), MelodyOptions.OnceInBackground)
basic.forever(() => {

   offset = offset + direction
   if (offset <= 0) {
      direction = 1
   }
   if (offset >= 5 * 5 - 1) {
      direction = -1
   }

   x = offset % 5
   y = offset / 5

   led.plot(x, y)
   basic.pause(80)
   led.unplot(x, y)

   robotbit.rgb().setPixelColor(0, neopixel.hsl(led1, 50, 30))
   robotbit.rgb().setPixelColor(1, neopixel.hsl(led2, 50, 50))
   robotbit.rgb().setPixelColor(2, neopixel.hsl(led3, 50, 50))
   robotbit.rgb().setPixelColor(3, neopixel.hsl(led4, 50, 30))
   robotbit.rgb().show()

   led1 = led1 + step
   led2 = led2 + step
   led3 = led3 + step
   led4 = led4 + step
})

沒有留言: