Cube: Code it yourself using micro:bit!

I created this document for people (adults, children, code clubs, etc.) to code the cube. I will set some dates where the cube will be ready and assembled.

Anyone can send me the code – send me a Direct Message over Twitter and I’ll give you my email.  Any code that’s sent to me will be run on the cube and I’ll video it and post it to Twitter.

Code the Cube

I can’t add hex files to MakeCode so here’s the JavaScript for the function getLightNumber:

let y = 0
let z = 0
let x = 0
let lightNumber = 0
let newX = 0
let newY = 0
let Cubert: neopixel.Strip = null
input.onButtonPressed(Button.B, () => {
rainbow()
})
function rainbow() {
Cubert.setPixelColor(0, neopixel.colors(NeoPixelColors.Red))
Cubert.setPixelColor(1, neopixel.colors(NeoPixelColors.Orange))
Cubert.setPixelColor(2, neopixel.colors(NeoPixelColors.Yellow))
Cubert.setPixelColor(3, neopixel.colors(NeoPixelColors.Green))
Cubert.setPixelColor(4, neopixel.colors(NeoPixelColors.Blue))
Cubert.setPixelColor(5, neopixel.colors(NeoPixelColors.Indigo))
Cubert.setPixelColor(6, neopixel.colors(NeoPixelColors.White))
Cubert.show()
}
function getLightNumber() {
newY = 0
newX = 0
lightNumber = 0
if (z % 2 == 1) {
newX = 7 – x
} else {
newX = x
}
if (newX % 2 == 1) {
newY = 7 – y
} else {
newY = y
}
lightNumber = z * 64 + newX * 8 + newY
}
Cubert = neopixel.create(DigitalPin.P0, 512, NeoPixelMode.RGB)
basic.forever(() => {
Cubert.rotate(1)
Cubert.show()
basic.pause(100)
})

One thought on “Cube: Code it yourself using micro:bit!

Leave a comment