Skip to main content

Project 5: Voice-Controlled Electronic Candle (50 points)

Task Description​

Design a voice-controlled candle using a sound sensor, full-color LED, and music module. The LED defaults to flashing green light. Blow air once to switch to flashing red light (simulating lighting). Blow again to extinguish the LED and play a piece of music (at least three notes), then remain extinguished, simulating blowing out the candle. The process cycles.

Core Concepts: Sound triggering, multiple LED flashing mode control, and simple music playback integration.

Required Hardware​

  • Steamania JoyKit V2 (Main Controller)
  • Sound Sensor Module (Microphone)
  • WS2812B Full-Color LED Module
  • Buzzer/Music Module

Core Tasks​

  1. Initialize LED to flash green light (default state)
  2. Detect sound trigger (blowing air)
  3. Switch to red flashing mode on first blow (candle lit)
  4. Detect second blow to extinguish LED and play music
  5. Cycle through all states continuously

Reference Solution​

Program Code​

; Voice-Controlled Electronic Candle Program
; Sound sensor controls LED states and music playback

; Initialization - Lines 000-005
000 C3 0001 0000 ; Enable sound sensor
001 C7 0001 0000 ; Enable WS2812B LED control
002 C8 0001 0000 ; Enable buzzer/music module
003 10 0000 0030 ; R30 = 0 (state register: 0=green, 1=red, 2=off)
004 0E 0000 0010 ; Jump to main loop at line 010

; State 0: Green Flashing (Candle Off) - Lines 010-01F
010 11 0030 0000 ; R00 = R30 (read current state)
011 04 0000 0018 ; If R00 != 0, jump to check next state
012 E4 00FF 0000 ; Set LED to green (R=0, G=255, B=0)
013 02 01F4 0000 ; Delay 500ms
014 E4 0000 0000 ; Turn LED off
015 02 01F4 0000 ; Delay 500ms
016 04 0001 001A ; If sound detected (R01 > 0), jump to state switch
017 0E 0000 0010 ; Jump back to line 010 (continue green flash)

018 04 0001 0020 ; If sound detected, jump to state switch
019 0E 0000 0010 ; Jump back to line 010

; State 1: Red Flashing (Candle Lit) - Lines 020-02F
020 11 0030 0000 ; R00 = R30 (read current state)
021 04 0001 0028 ; If R00 != 1, jump to check next state
022 E4 00FF 0000 ; Set LED to red (R=255, G=0, B=0)
023 02 01F4 0000 ; Delay 500ms
024 E4 0000 0000 ; Turn LED off
025 02 01F4 0000 ; Delay 500ms
026 04 0001 002A ; If sound detected, jump to state switch
027 0E 0000 0020 ; Jump back to line 020 (continue red flash)

028 04 0001 0030 ; If sound detected, jump to state switch
029 0E 0000 0020 ; Jump back to line 020

; State 2: Off with Music (Candle Blown Out) - Lines 030-03F
030 11 0030 0000 ; R00 = R30 (read current state)
031 04 0002 0038 ; If R00 != 2, jump to state switch check
032 E4 0000 0000 ; Turn LED off
033 D0 0040 0000 ; Play music starting at line 040
034 02 03E8 0000 ; Delay 1000ms (music duration)
035 04 0001 003A ; If sound detected, jump to state switch
036 0E 0000 0030 ; Jump back to line 030 (stay off)

038 04 0001 0040 ; If sound detected, jump to state switch
039 0E 0000 0030 ; Jump back to line 030

; State Switch - Lines 040-04F
040 11 0030 0000 ; R00 = R30 (read current state)
041 04 0000 0048 ; If R00 == 0, jump to set state 1
042 04 0001 0050 ; If R00 == 1, jump to set state 2
043 10 0000 0030 ; R30 = 0 (reset to green state)
044 02 03E8 0000 ; Delay 1000ms
045 0E 0000 0010 ; Jump back to line 010

048 10 0001 0030 ; R30 = 1 (set to red state)
049 02 03E8 0000 ; Delay 1000ms
04A 0E 0000 0020 ; Jump back to line 020

050 10 0002 0030 ; R30 = 2 (set to off state)
051 02 03E8 0000 ; Delay 1000ms
052 0E 0000 0030 ; Jump back to line 030

; Music Data - Lines 060-06F
060 D1 0262 0000 ; Note C5 (523Hz)
061 D1 0294 0000 ; Note D5 (587Hz)
062 D1 032F 0000 ; Note E5 (659Hz)
063 D1 0000 0000 ; End of music

Detailed Explanation​

Initialization (Lines 000-005)​

Line 000: C3 0001 0000

  • Enable sound sensor module
  • Required to detect blowing air sounds

Line 001: C7 0001 0000

  • Enable WS2812B LED control
  • Required for full-color LED operation

Line 002: C8 0001 0000

  • Enable buzzer/music module
  • Required for music playback

Line 003: 10 0000 0030

  • Initialize state register R30 = 0
  • State 0 = green flashing (candle off)

Line 004: 0E 0000 0010

  • Jump to main loop at line 010

State 0: Green Flashing (Lines 010-019)​

Line 010: 11 0030 0000

  • Read current state from R30

Line 011: 04 0000 0018

  • Conditional Jump: If state != 0, check next state

Line 012: E4 00FF 0000

  • Set LED to green (R=0, G=255, B=0)

Line 013: 02 01F4 0000

  • Delay 500ms

Line 014: E4 0000 0000

  • Turn LED off

Line 015: 02 01F4 0000

  • Delay 500ms

Line 016: 04 0001 001A

  • Conditional Jump: If sound detected, switch state

Line 017: 0E 0000 0010

  • Continue green flashing loop

State 1: Red Flashing (Lines 020-029)​

Line 022: E4 00FF 0000

  • Set LED to red (R=255, G=0, B=0)
  • Simulates candle flame

Line 023-025: Flash red on/off every 500ms

Line 026: 04 0001 002A

  • Check for sound trigger to switch state

State 2: Off with Music (Lines 030-039)​

Line 032: E4 0000 0000

  • Turn LED off (candle extinguished)

Line 033: D0 0040 0000

  • Play music starting at line 040
  • Uses instruction 0xD0 for music playback

Line 034: 02 03E8 0000

  • Delay 1000ms for music duration

State Switching (Lines 040-052)​

Line 040: 11 0030 0000

  • Read current state

Line 041: 04 0000 0048

  • If state == 0 (green), switch to state 1 (red)

Line 042: 04 0001 0050

  • If state == 1 (red), switch to state 2 (off)

Line 043: 10 0000 0030

  • If state == 2 (off), reset to state 0 (green)

Music Data (Lines 060-063)​

Line 060: D1 0262 0000

  • Play note C5 (523Hz)

Line 061: D1 0294 0000

  • Play note D5 (587Hz)

Line 062: D1 032F 0000

  • Play note E5 (659Hz)

Line 063: D1 0000 0000

  • End of music marker

Key Concepts​

1. Sound Detection​

The sound sensor detects blowing air. Use register R01 to check if sound level exceeds threshold.

2. State Machine​

Three states controlled by register R30:

  • State 0 (R30=0): Green flashing - candle off
  • State 1 (R30=1): Red flashing - candle lit
  • State 2 (R30=2): LED off + music - candle blown out

3. Music Playback​

Use instruction 0xD0 to play music and 0xD1 to define notes. Each note specifies frequency.

4. LED Color Control​

  • Green: E4 00FF 0000 (R=0, G=255, B=0)
  • Red: E4 00FF 0000 (R=255, G=0, B=0)
  • Off: E4 0000 0000 (R=0, G=0, B=0)

Assessment Rubric​

CriteriaExcellent (5)Good (4)Satisfactory (3)Needs Improvement (2)
Sound DetectionReliable sound triggeringMinor detection issuesInconsistent triggeringSound sensor not working
LED ControlCorrect colors and flashingMinor color/timing issuesFlashing works with errorsLED not functioning properly
Music PlaybackClear music with 3+ notesMinor audio issuesMusic plays but incompleteMusic not working
State TransitionsSmooth state changesMinor transition issuesStates change but roughStates don't transition correctly
Code QualityClean, well-organized codeGood structureCode works but messyCode doesn't work

Extension Challenges​

  1. Variable Sensitivity: Adjust sound detection threshold for different environments
  2. Flicker Effect: Add random variation to red LED brightness for realistic flame effect
  3. Multiple Songs: Store different melodies and play randomly
  4. Auto-Off: Automatically return to green state after 30 seconds in off state