Skip to main content

Presenter Guide: Build a CodyNick Temperature Alarm

Session goal

Build one program in seveneight small steps. The audience first sees how easily Python can control a physical LED, then gradually adds blinking, joystick input, a display, a sensor, temperature colors, an alarm melody, and a joystick alarm control.

These first examples intentionally use the simplest possible code. They do not include connection checks, error handling, or cleanup structures. The goal is to make the first experience with CodyNick clear and approachable.

Do not paste the final program at the beginning. Type each step live, run it, invite the audience to predict the result, and then change the existing program for the next step.

Equipment

  • Raspberry Pi running CodyNick
  • CodyJoy Pro
  • Temperature sensor
  • CodyJoy ProStand-alone seven-segment display
  • Python IDE at http://10.42.0.1/code/

Presentation rhythm

For every step:

  1. Ask the engaging question before showing the new code.
  2. Let the audience suggest an answer.
  3. Type the small change.
  4. Ask the audience to predict the result.
  5. Select Run This File and observe the hardware and live terminal.

Step 1: Light One RGB LED

Ask before starting

How many lines of Python do you think we need to control a real LED?

Explain that CodyNick.CN() creates the connection and LED number 0 is the first LED on the RGB matrix. A color is written as red, green, and blue values in hexadecimal.

import time
import CodyNick

cody = CodyNick.CN()

CodyNick.RGB_Matrix.set(cody, 0, "#00FF00")
time.sleep(15)3)

CodyNick.RGB_Matrix.clear(cody)
cody.close()

Point out

  • 0 selects the first LED.
  • #00FF00 means green.
  • time.sleep(3) keeps it visible for three seconds.
  • finallyclear turns the LEDsLED off evenat whenthe an error occurs.end.

Step 2: Make the LED Blink

Ask before starting

A computer follows instructions very quickly. How can we make it wait long enough for our eyes to see an LED blink?

Replace the single LED command and three-second wait with this loop:

import time
import CodyNick

cody = CodyNick.CN()


try:
    if not cody.ensure_connected():
        raise RuntimeError("CodyNick gadget not found.")

    for count in range(5):
    CodyNick.RGB_Matrix.set(cody, 0, "#00FF00")
    time.sleep(0.5)
    CodyNick.RGB_Matrix.set(cody, 0, "#000000")
    time.sleep(0.5)

finally:
    CodyNick.RGB_Matrix.clear(cody)
    cody.close()

Point out

  • range(5) repeats the indented instructions five times.
  • #000000 means no red, green, or blue, so the LED is off.
  • The two half-second waits create an obvious rhythm.

Invite the audience to predict what changing 0.5 to 0.1 will do.


Step 3: Control LEDs with the Joystick

Ask before starting

Can one physical control choose which light Python turns on?

Move the joystick up, right, down, and left. One LED in the corresponding part of the matrix lights up in a different color. Press the joystick to light the center LED white.

import time
import CodyNick

cody = CodyNick.CN()

while True:
    direction = CodyNick.Joystick.states(cody, "CJP")
    CodyNick.RGB_Matrix.clear(cody)

    if "CLICK" in direction:
        CodyNick.RGB_Matrix.set(cody, 6, "#FFFFFF")
    elif "UP" in direction:
        CodyNick.RGB_Matrix.set(cody, 13, "#0000FF")
    elif "RIGHT" in direction:
        CodyNick.RGB_Matrix.set(cody, 4, "#00FF00")
    elif "DOWN" in direction:
        CodyNick.RGB_Matrix.set(cody, 1, "#FF0000")
    elif "LEFT" in direction:
        CodyNick.RGB_Matrix.set(cody, 8, "#FFFF00")

    time.sleep(0.1)

Point out

    states reads the current joystick direction. Up uses LED 13 in blue. Right uses LED 4 in green. Down uses LED 1 in red. Left uses LED 8 in yellow. Clicking uses LED 6 in white. if and elif choose only one LED at a time. The matrix is cleared before showing the current direction. while True keeps reading the joystick until the program is stopped.

    Step 4: Show a Fixed Number

    Ask before starting

    If Python can control one light, can it also send a complete number to a display with one instruction?

    Replace the blinking code with a seven-segment display command:

    import time
    import CodyNick
    
    cody = CodyNick.CN()
    
    try:
        if not cody.ensure_connected():
            raise RuntimeError("CodyNick gadget not found.")
    
        CodyNick.Seven_Segment.display(cody, "CJP"Stand-Alone", 25)
        time.sleep(3)
    
    finally:
        CodyNick.Seven_Segment.display(cody, "CJP", "")
        cody.close()
    

    Point out

    • "CJP"Stand-Alone" selects the CodyJoyseparate Proseven-segment display.
    • 25 is the value sent to the display.
    • Sending an empty string clears the display during cleanup.

    Step 4:5: Show a Counter

    Ask before starting

    The display is showing data from our program. What should change if we want the display to show progress instead of one fixed value?

    Change the fixed display command to a loop:

    import time
    import CodyNick
    
    cody = CodyNick.CN()
    
    
    try:
        if not cody.ensure_connected():
            raise RuntimeError("CodyNick gadget not found.")
    
        for number in range(10)1, 11):
        CodyNick.Seven_Segment.display(cody, "CJP"Stand-Alone", number)
        print("Count:", number)
        time.sleep(1)
    
    finally:
        CodyNick.Seven_Segment.display(cody, "CJP", "")
        cody.close()
    

    Point out

    • range(10)1, 11) produces the numbers 01 through 910.
    • The same variable is shown on the physical display and in the live terminal.
    • Software variables can directly represent real-world output.

    Step 5:6: Display the Measured Temperature

    Ask before starting

    So far, Python has displayed numbers that we chose. How can the physical world choose the number instead?

    Replace the counter with a continuous sensor loop:

    import time
    import CodyNick
    
    cody = CodyNick.CN()
    
    
    try:
        if not cody.ensure_connected():
            raise RuntimeError("CodyNick gadget not found.")
    
        while True:
        temperature = CodyNick.Temperature_Sensor.read(cody)
        temperature = round(temperature, 1)
    
        CodyNick.Seven_Segment.display(cody, "CJP"Stand-Alone", temperature)
        print("Temperature:", temperature, "C")
    
        time.sleep(1)
    
    finally:
        CodyNick.Seven_Segment.display(cody, "CJP", "")
        cody.close()
    
    

    Point out

    • read(cody) gets a current measurement from the sensor.
    • round(..., 1) keeps one digit after the decimal point.
    • while True continues until the presenter stops the script.
    • The one-second pause gives a readable update rate.

    Ask someone to gently warm the sensor and watch the number respond.


    Step 6:7: Add Temperature Colors

    Ask before starting

    Can someone understand whether a temperature is cold, comfortable, or hot more quickly from a number or from a color?

    Add the temperature decision and use a loop to color all 16 LEDs:

    import time
    import CodyNick
    
    cody = CodyNick.CN()
    
    
    try:
        if not cody.ensure_connected():
            raise RuntimeError("CodyNick gadget not found.")
    
        while True:
        temperature = CodyNick.Temperature_Sensor.read(cody)
        temperature = round(temperature, 1)
    
        CodyNick.Seven_Segment.display(cody, "CJP"Stand-Alone", temperature)
    
        if temperature < 18:20:
            color = "#0000FF"
        elif temperature <= 30:
            color = "#00FF00"
        else:
            color = "#FF0000"
    
        for led in range(16):
            CodyNick.RGB_Matrix.set(cody, led, color)
    
        print("Temperature:", temperature, "C")
        time.sleep(1)
    
    finally:
        CodyNick.RGB_Matrix.clear(cody)
        CodyNick.Seven_Segment.display(cody, "CJP", "")
        cody.close()
    

    Point out

    • Below 1820 C is blue.
    • From 1820 C through 30 C is green.
    • Above 30 C is red.
    • if, elif, and else turn one measurement into one of three decisions.
    • range(16) applies the chosen color to the complete matrix.

    Step 7:8: Add the Alarm and Joystick Inhibit

    Ask before starting

    A real alarm may need to be silenced while someone investigates. How can one joystick button act as both the silence control and the re-enable control?

    Start with two state variables beforethat try:remember the alarm state:

    alarm_enabled = True
    was_clicked = False
    

    Then build the final program:

    import time
    import CodyNick
    
    cody = CodyNick.CN()
    
    alarm_enabled = True
    was_clicked = False
    
    
    try:
        if not cody.ensure_connected():
            raise RuntimeError("CodyNick gadget not found.")
    
        while True:
        temperature = CodyNick.Temperature_Sensor.read(cody)
        temperature = round(temperature, 1)
    
        CodyNick.Seven_Segment.display(cody, "CJP"Stand-Alone", temperature)
    
        if temperature < 18:20:
            color = "#0000FF"
        elif temperature <= 30:
            color = "#00FF00"
        else:
            color = "#FF0000"
    
        for led in range(16):
            CodyNick.RGB_Matrix.set(cody, led, color)
    
        is_clicked = CodyNick.Joystick.click(cody, "CJP")
    
        if is_clicked and not was_clicked:
            alarm_enabled = not alarm_enabled
    
            if alarm_enabled:
                print("Alarm enabled")
            else:
                print("Alarm inhibited")
    
        was_clicked = is_clicked
    
        if temperature > 30 and alarm_enabled:
            CodyNick.CJP_Sound_Maker.play_until_done(cody, "C6", 120)
            CodyNick.CJP_Sound_Maker.play_until_done(cody, "E6", 120)
            CodyNick.CJP_Sound_Maker.play_until_done(cody, "G6", 120)
            CodyNick.CJP_Sound_Maker.play_until_done(cody, "E6", 120)
    
        print(
                "Temperature:", temperature, "C - Alarm:",
                "enabled" if alarm_enabled else "inhibited"
            C")
    
        time.sleep(0.3)
    
    finally:
        CodyNick.RGB_Matrix.clear(cody)
        CodyNick.Seven_Segment.display(cody, "CJP", "")
        cody.close()
    
    

    Point out

    • alarm_enabled remembers whether sound is permitted.
    • was_clicked allows one physical press to cause only one toggle.
    • not alarm_enabled changes enabled to inhibited and inhibited to enabled.
    • The red visual warning remains active while the audible alarm is inhibited.
    • The alarm melody is C6, E6, G6, E6.
    • The joystick is checked between complete melody cycles.

    Closing audience questions

    • Which thresholds would you use for a refrigerator, greenhouse, or classroom?
    • What other sensor could replace the temperature sensor?
    • What could the joystick control besides the alarm?
    • How could the dashboard or cloud record the temperature history?

    Presenter reminder

    Keep the code visible while demonstrating the hardware response. The purpose is not to memorize every command; it is to show the repeated pattern:

    1. Read an input.
    2. Make a decision.
    3. Control an output.
    4. Repeat safely.