Warm-Up no.07, Seeing (Infra)red
Introduction
This will be the last Warm-Up that we do. Instead of questions for homework, due to the nature of what we're learning, we'll do a collaborative exercise. What we'll be learning about is the IR sensor(s) on the CPX.
IR?! I heardly know-R
IR stands for Infrared. This is a wavelength of light beyond the visible part of the spectrum. This allows us to transmit and receive light in pulses that we don't see. This is the same technology that you see in a remote control for a television. One thing to note is that you need a direct line of sight between the two objects (sending and receiving respectfully)
Example (in 2 CPX's)
We'll need 2 CPX's for the below example. One person will be the sender. Their CPX will use the "ir_send()" function in order to send an arbitrary signal.
Here's the code for the "sender" CPX and the "receiver" CPX, this is about the simplest that we can do:
- Sender sends an arbitrary code
- Lights a light to show we sent
- Receiver waits for any code
- When it gets something it shows the code it got
- It also flashes a light to let us know what's up
# --------------------------
# IR EXAMPLE
# THE SENDER
# --------------------------
# imports
import time
from adafruit_circuitplayground import cp
# MAIN LOOOO O O O P!
while True:
# wait for a button press
if cp.button_a:
#send an arbitrary 16bit code
cp.ir_send(0xA5A5)
#feedback for pressing
cp.pixels.fill((255,0,255))
time.sleep(0.2)
cp.pixels.fill((0,0,0))
time.sleep(0.2)
# --------------------------
# IR EXAMPLE
# THE RECEIVER
# --------------------------
# imports
import time
from adafruit_circuitplayground import cp
# MAIN LOOO O O O O O P
while True:
# waits to receive anything
code = cp.ir_receive()
# if we got....not nothing
if code is not None:
# let the serial know what's up
print("Received:", hex(code))
# flash on receive
cp.pixels.fill((0, 255, 0))
time.sleep(0.2)
cp.pixels.fill((0, 0, 0))
time.sleep(0.2)
# short wait give sensor
# a chance to chill
time.sleep(0.05)
In-Class Exercise
What we'll do in class is the following, is create a kind of primitive "laser tag" type deal
-
select 5 arbitrary (hex)codes for each person in class
-
share the codes with each other
-
select 5 arbitrary color codes for each person in class
-
share the color values with each other
-
set each of our individual CPX's to send our specific code with button A
-
set each individual CPX to properly "decode" each other person's code
-
create a little "hit" animation that shows the coe of who hit you.
-
test that this works
-
add some functionality:
- give each person some amount of "life" (3, 5?)
- each person's life's reduces on hit
- play sounds for shooting and hitting
- play a sound for "dying" (life = 0)
- use button B to "respawn"
- play sound for "respawning"