Button flipper

Step 1

Drag out a on button A click block to add code that runs when button A is pressed.

onbutton Aclick

Step 2

Grab an if then else block and put it inside the on button A click. We’ll use this, along with a random number, to decide if you’re going to win or lose.

ifthenelsetrueonbutton Aclick

Step 3

Go over to LOGIC again and get a 0 < 0 comparison block. Use it to replace the true condition in the if then else block.

input.buttonA.onEvent(ButtonEvent.Click, function () {
    if (0 < 0) {
    } else {
    }
})

Step 4

In the MATH toolbox drawer, pull out a pick random block and place it into the first slot of the 0 < 0 block. In the second slot, type 5.

This picks a random number between 0 and 10. If the number is less than 5 it will run the code in the if part of our if then else block. But, if it’s 5 or greater, it will run the code in the else part of our if then else block.

input.buttonA.onEvent(ButtonEvent.Click, function () {
    if (Math.randomRange(0, 10) < 5) {
    } else {
    }
})

Step 5

Drag the play sound and show animation blocks out to create the win and lose effects. You can pick the sounds and animations. Be creative!

input.buttonA.onEvent(ButtonEvent.Click, function () {
    if (Math.randomRange(0, 10) < 5) {
        music.powerUp.play()
        light.showAnimation(light.sparkleAnimation, 2000)
    } else {
        music.wawawawaa.play()
        light.showAnimation(light.runningLightsAnimation, 2000)
    }
})

Step 6

Click Download to transfer your code to the Adafruit Circuit Playground Express. Press button A to try your luck…

Step 7

Awesome! Congratulations on completing this challenge on the Adafruit Circuit Playground Express.

ifthenelsetrueonbutton Aclick