Coding for your reaction game
Duration: ~10 minutes
Concepts
- Infrared communication
- RGB color values
Blocks
network.onInfraredPacketReceived( ({ receivedNumber }) => {})
network.infraredSendNumber(0)
light.rgb(0, 0, 0)
Step 1: Make the transmit color variables
First, we need make some numbers to represent colors. We will send the color numbers as RGB values to other boards as infrared messages.
- Open MakeCode in your web browser.
- Go to
||loops:LOOPS||
and pull out and||loops:on start||
block. - In
||variables:VARIABLES||
, click the make a variable… button. - Give the new variable the name of
rgbRed
. - Back in
||variables:VARIABLES||
you’ll see a||variables:set rgbRed to||
block. Drag that out into the||loops:on start||
block. - Repeat steps 3 to 5 to make another variable that we’ll name
rgbYellow
.
let rgbRed = 0
let rgbYellow = 0
Set the RGB values
- Let’s make an RGB value for Red. Go to
||light:LIGHT||
and get a||light:red green blue||
block and replace the0
in||variables:set rgbRed to||
with it. - Set the values for
green
andblue
to0
. This RGB value has only red light. - Repeat step 1 for our Yellow value and put a
||light:red green blue||
block into||variables:set rgbYellow to||
. - This time, set just the value for
blue
to0
. The combination of red and green light makes the yellow color.
let rgbRed = light.rgb(255, 0, 0)
let rgbYellow = light.rgb(255, 255, 0)
Step 2: Program the buttons to send colors
- Pull out an
||input:on button A click||
from||input:INPUT||
. - From
||light:LIGHT||
, drag a||light:set all pixels to||
block and place it inside the||input:on button A click||
. - From
||network:NETWORK||
, drag a||network:infrared send number||
block and place it below the||light:set all pixels to||
block inside the||input:on button A click||
.
input.buttonA.onEvent(ButtonEvent.Click, function () {
light.setAll(0xff0000)
network.infraredSendNumber(0)
})
Build the block
- Change the color in
||light:set all pixels||
togreen
. - From
||variables:VARIABLES||
, drag the||variables:rgbRed||
block and place it inside the||network:infrared send number||
.
The ||network:infrared send number||
block allows the board to communicate with another Adafruit Circuit Playground Express using its infrared sensors. When it is programmed to send the color red, the signal will be transmitted by infrared to another waiting board.
let rgbRed = light.rgb(255, 0, 0)
let rgbYellow = light.rgb(255, 255, 0)
input.buttonA.onEvent(ButtonEvent.Click, () => {
light.setAll(0x00ff00)
network.infraredSendNumber(rgbRed)
})
Add more buttons and on shake
Now, let’s repeat the previous steps to make two more button blocks. This time we’re going to change it up a little bit! Change the buttons of the two new blocks to B and A+B. Oh yeah, pull out an ||input:on shake||
block too.
input.buttonB.onEvent(ButtonEvent.Click, function () {
light.setAll(0xff0000)
network.infraredSendNumber(0)
})
input.buttonsAB.onEvent(ButtonEvent.Click, function () {
light.setAll(0xff0000)
network.infraredSendNumber(0)
})
input.onGesture(Gesture.Shake, function () {
})
Build the block
- Inside the
||input:on button B click||
, change||light:set all pixels||
toviolet
. - From
||variables:VARIABLES||
, drag a||variables:rgbYellow||
block and place it inside the||network:infrared send number||
.
let rgbRed = light.rgb(255, 0, 0)
let rgbYellow = light.rgb(255, 255, 0)
input.buttonB.onEvent(ButtonEvent.Click, () => {
light.setAll(0x8a2be2)
network.infraredSendNumber(rgbYellow)
})
Build the block
- Inside the
||input:on button A+B click||
, change||light:set all pixels||
towhite
. - Here, we want to send the color Black. Since black is no light at all, the RGB value is just
0
. So, we can use the0
that’s already inside the||network:infrared send number||
.
input.buttonsAB.onEvent(ButtonEvent.Click, () => {
light.setAll(0xffffff)
network.infraredSendNumber(0x000000)
})
Build the block
- From
||light:LIGHT||
, drag a||light:set all pixels to||
block and place it inside the||input:on shake||
block. - Change
||light:set all pixels||
toblue
.
input.onGesture(Gesture.Shake, () => {
light.setAll(0x0000ff)
})
Step 3: Setup the events to receive infrared messages
Now that you’ve programmed your Adafruit Circuit Playground Express to send over infrared, we need to program it to receive infrared messages with the RGB color values sent from other boards.
- From
||network:NETWORK||
, drag an||network:on infrared received||
block to your editor. - From
||light:LIGHT||
, drag a||light:set all pixels to||
block and place it inside the||network:on infrared received||
block.
network.onInfraredPacketReceived( ({ receivedNumber }) => {
light.setAll(0xff0000)
})
Build the block
- From
||variables:VARIABLES||
, drag out a||variables:receivedNumber||
block and replace the red color block inside the||light:set all pixels to||
block.
network.onInfraredPacketReceived( ({ receivedNumber }) => {
light.setAll(receivedNumber)
})
And now you’re done! Download the code to two Adafruit Circuit Playground Expresss and get ready to duel. Who has the fastest reaction time? Who will be able to change the other’s colors?