Close

Dicebag Make Code For Circuit Playground Express

A project log for Circuit Playground Dice Bag

Dice Bag uses Adafruit's Circuit Playground to roll dice of several type, d100, d20, d12 or d6.

tomwsmftomwsmf 05/15/2017 at 07:470 Comments

In preparing for Adafruit's new version of the Circuit Playground I started using the web browser based dev environment over at http://makecode.adafruit.com

You can find this up there at https://makecode.com/_MJjJrg9eRTmi

/*
The Circuit Playground Dice Bag  - CC Attribution-ShareAlike - Tom Higgins and Sons



User Manual
Select the type of die you would like to throw by setting the Select Switch
and pressing one of the Buttons



Select Switch Left and Button A = d100
Select Switch Left and Button B = d20
Select Switch Right and Button A = d12
Select Switch Right and Button B = d6



You can hold down the Button and it will flash through many tosses.
Release the Button and your results will show on the Circuit Playground as
two (or one) lit NeoPixel



Red=Tens Place (0-9)
Blue=Ones Place (0-9)
Green=Tens and Ones are the same (00,11,22,33,44,55,66,77,88,99)



The NeoPixels are arranged from 0 - 9 . If you are holding your Circuit Playground with the USB port facing Up
0 is the top left NeoPixel closest to the USB port



1 is to the left of that
..
4 is the bottom let NeoPixel closest to the Battery Jack
5 is the bottom right NeoPixel closest to the Battery Jack
..
9 is the top tight NeoPixel closest to the USB port
*/

let troll = 0
let tens = 0
let ones = 0
let switcher = 0
loops.forever(() => {
    music.stopAllSounds()
    if (input.buttonA.isPressed()) {
        if (switcher) {
            tossd12()
        } else {
            tossd100()
        }
    } else {
        if (input.buttonB.isPressed()) {
            if (switcher) {
                tossd6()
            } else {
                tossd20()
            }
        }
    }
})
input.onSwitchMoved(SwitchDirection.Left, () => {
    switcher = 0
})
input.onSwitchMoved(SwitchDirection.Right, () => {
    switcher = 1
})
function tossd100() {
    troll = Math.random(99)
    prepthrow(troll)
    return
}
function tossd20() {
    troll = (Math.random(20)) + 1
    prepthrow(troll)
    return
}
function tossd12() {
    troll = Math.random(11) + 1
    prepthrow(troll)
    return
}
function tossd6() {
    troll = Math.random(6) + 1
    prepthrow(troll)
    return
}
function showtoss(ones: number, tens: number) {
    light.pixels.clear()
    music.playTone((ones * 100) + 1, 50)
    music.stopAllSounds()
    if (ones == tens) {
        light.pixels.setPixelColor(tens, Colors.Green)
    }
    else {
        light.pixels.setPixelColor(ones, Colors.Blue)
        light.pixels.setPixelColor(tens, Colors.Red)
    }
    light.pixels.show()
    music.stopAllSounds()
    return
}

function prepthrow(troll: number) {
    ones = troll % 10
    tens = troll / 10
    showtoss(ones, tens)
    return
}

Discussions