Project 2: Simulated Indoor Lighting Circuit
๐ฏ Project Objectivesโ
Build a light switch simulation that:
- Uses a push button to toggle a light on/off
- Controls a full-color LED to emit white light when "on"
- Displays current switch status on screen
๐ Required Hardwareโ
| Component | Quantity | Connection |
|---|---|---|
| Steamania JoyKit V2 | 1 | Main Controller |
| Push Button Module | 1 | Digital Input Pin |
| WS2812B Full-Color LED | 1 | On-board or External |
๐ง Circuit Setupโ
- Push Button: Connect to one of the digital input pins (e.g., DI0)
- WS2812B LED: Use on-board LED or connect to dedicated pin
๐ก Core Tasksโ
Task 1: Configure Button as Digital Inputโ
- Use opcode
0x10to configure IO pins - Set appropriate pin as digital input
Task 2: Implement Toggle Logicโ
- Detect button press (transition from HIGH to LOW or vice versa)
- Toggle a state variable (0 = OFF, 1 = ON)
- Implement debouncing to avoid false triggers
Task 3: Control LED Stateโ
- Turn LED white (0xFFFFFF) when switch is ON
- Turn LED off (0x000000) when switch is OFF
Task 4: Display Statusโ
- Show "ON" or "OFF" text on screen
- Optionally display current state indicator
๐ Reference Solutionโ
Program Codeโ
000 C7 0001 0000 ; Enable WS2812B LED control
001 10 0025 00F0 ; Configure IO pins: 4 inputs (0-3), 4 outputs (4-7)
002 10 0020 0000 ; Initialize state variable R20 = 0 (OFF)
003 10 0021 0000 ; Initialize previous button state R21 = 0
004 10 0028 FFFF ; Set foreground color to white (for text)
005 0E 0000 0010 ; Jump to main loop at line 010
; Main Loop - Start at line 010
010 14 0000 0000 ; Read digital inputs to R00
011 17 0000 0001 ; R00 = R00 AND 0x01 (check only DI0)
012 09 0000 0020 ; If R00 == R00 (button state), jump to line 020
; Actually: compare with previous state in R21
013 0E 0000 0030 ; Jump to update display (no change detected)
; Button Press Detected - Toggle State
020 11 0022 0020 ; Copy current button state to R22 (temp storage)
021 12 0020 0001 ; Toggle R20: if 0 then 1, if 1 then 0 (simplified)
022 0E 0000 0030 ; Jump to update LED and display
; Update LED and Display - Line 030
030 04 0000 0040 ; If R00 (state) > 0, jump to turn LED on
031 E4 0000 0000 ; Turn LED OFF (black)
032 55 000A 0000 ; Display "OFF" at position (10, 0)
033 0E 0000 0050 ; Jump to end of loop
040 E4 00FF FFFF ; Turn LED ON (white)
041 55 000A 0001 ; Display "ON" at position (10, 0)
; End of Loop - Line 050
050 11 0021 0000 ; Store current button state to R21 (previous)
051 02 0064 0000 ; Delay 100ms for debouncing
052 0E 0000 0010 ; Jump back to line 010 (main loop)
Improved Solution with Proper Toggle Logicโ
000 C7 0001 0000 ; Enable WS2812B LED control
001 10 0025 00F0 ; Configure IO: lower 4 bits as input, upper 4 as output
002 10 0020 0000 ; R20 = Light state (0=OFF, 1=ON)
003 10 0021 0001 ; R21 = Previous button state (1 = not pressed)
004 10 0028 FFFF ; Set foreground color to white
005 0E 0000 0010 ; Jump to main loop
; Main Loop
010 14 0000 0000 ; Read DI to R00
011 17 0000 0001 ; R00 = R00 AND 0x01 (check DI0 only)
012 06 0001 0020 ; If R00 == 1 (button not pressed), jump to 020
013 06 0001 0050 ; If R00 == 1 (prev state also not pressed), jump to 050
; Button Pressed (transition from 1 to 0)
014 12 0020 0001 ; Toggle R20 (light state): 0โ1 or 1โ0
015 04 0000 0030 ; If R20 > 0 (light ON), jump to 030
016 E4 0000 0000 ; Turn LED OFF (black)
017 55 0A00 0000 ; Display "OFF"
018 0E 0000 0050 ; Jump to update previous state
020 0E 0000 0050 ; Button not pressed, skip toggle
030 E4 00FF FFFF ; Turn LED ON (white)
031 55 0A00 0001 ; Display "ON"
050 11 0021 0000 ; R21 = R00 (store current button state)
051 02 0096 0000 ; Delay 150ms (debounce)
052 0E 0000 0010 ; Loop back to start
Simplified and Corrected Solutionโ
000 C7 0001 0000 ; Enable WS2812B LED control
001 10 0025 00F0 ; Configure IO pins: bits 0-3 as input, 4-7 as output
002 10 0020 0000 ; R20 = Light state (0=OFF, 1=ON)
003 10 0028 FFFF ; Set foreground color to white (for text display)
004 0E 0000 0010 ; Jump to main loop at line 010
; Main Loop - Check Button and Update State
010 14 0000 0000 ; Read digital inputs to R00
011 17 0000 0001 ; Mask to get only bit 0 (DI0): R00 = R00 AND 0x01
012 06 0001 0020 ; If DI0 == 1 (button NOT pressed), jump to 020 (skip)
; Button IS pressed (DI0 = 0)
013 12 0020 0001 ; Toggle R20: R20 = R20 + 1 (0โ1, 1โ2)
014 04 0001 0018 ; If R20 > 1, jump to 018 (reset to 0)
015 0E 0000 0019 ; Jump to update LED
018 10 0020 0000 ; R20 = 0 (reset state)
; Update LED Based on State
019 04 0000 001A ; If R20 > 0 (ON), jump to 01A
01A E4 0000 0000 ; LED OFF (black)
01B 55 0A00 0000 ; Display "OFF" at (10, 0)
01C 0E 0000 001E ; Jump to delay
01E E4 00FF FFFF ; LED ON (white: R=255, G=255, B=255)
01F 55 0A00 0001 ; Display "ON" at (10, 0)
; Delay and Loop
020 02 0096 0000 ; Delay 150ms (debounce)
021 0E 0000 0010 ; Jump back to line 010
Final Optimized Solution (Recommended)โ
000 C7 0001 0000 ; Enable WS2812B LED control
001 10 0025 00F0 ; Configure IO: lower 4 pins as input
002 10 0020 0000 ; R20 = Light state (0=OFF, 1=ON)
003 10 0021 0001 ; R21 = Previous button state (1=released)
004 10 0028 FFFF ; Set text color to white
005 0E 0000 0010 ; Jump to main loop
; Main Loop
010 14 0000 0000 ; Read DI to R00
011 17 0000 0001 ; R00 = R00 AND 0x01 (check DI0 only)
012 06 0001 0020 ; If button released (1), jump to 020
013 06 0001 0020 ; If previous also released, skip toggle
; Toggle state on button press
014 12 0020 0001 ; R20 = R20 + 1 (toggle)
015 04 0001 0017 ; If R20 > 1, jump to reset
016 0E 0000 0018 ; Jump to update
017 10 0020 0000 ; Reset: R20 = 0
; Update LED and display
018 04 0000 001A ; If R20 > 0, turn LED on
019 E4 0000 0000 ; LED OFF
01A 55 0A00 0000 ; Display "OFF"
01B 0E 0000 001D ; Jump to end
01C E4 00FF FFFF ; LED ON (white)
01D 55 0A00 0001 ; Display "ON"
; End loop
01E 11 0021 0000 ; R21 = R00 (store button state)
01F 02 0096 0000 ; Delay 150ms (debounce)
020 0E 0000 0010 ; Loop back
๐ Detailed Explanationโ
Initialization (Lines 000-005)โ
Line 000: C7 0001 0000
- Enable WS2812B full-color LED control module
- Required before using opcode
0xE4
Line 001: 10 0025 00F0
- Configure IO pin directions
R25= IO configuration register00F0= Binary11110000= Lower 4 pins as input, upper 4 as output
Line 002: 10 0020 0000
- Initialize light state variable
R20to 0 (OFF) R20will toggle between 0 (OFF) and 1 (ON)
Line 003: 10 0021 0001
- Initialize previous button state
R21to 1 (released) - Used for edge detection
Line 004: 10 0028 FFFF
- Set foreground color to white (
0xFFFFin RGB565) - Used for text display
Line 005: 0E 0000 0010
- Jump to main loop at line
010
Button Detection and Toggle (Lines 010-017)โ
Line 010: 14 0000 0000
- Read digital input values to
R00 - Each bit represents one DI pin status
Line 011: 17 0000 0001
R00 = R00 AND 0x01- Mask to check only DI0 (bit 0)
- Result:
R00= 0 (pressed) or 1 (released)
Line 012: 06 0001 0020
- If
R00 == 1(button released), jump to020 - Skip toggle logic when button not pressed
Line 013: 06 0001 0020
- If previous state also released, skip
- This creates edge detection (only toggle on press, not hold)
Line 014: 12 0020 0001
- Toggle light state:
R20 = R20 + 1 - Alternates between 0 and 1
Line 015: 04 0001 0017
- If
R20 > 1, jump to reset line017 - Ensures
R20stays in range 0-1
Line 016: 0E 0000 0018
- Jump to LED update section
Line 017: 10 0020 0000
- Reset
R20to 0 (wraps around from 2 to 0)
LED Control (Lines 018-01D)โ
Line 018: 04 0000 001A
- If
R20 > 0(light is ON), jump to01A
Line 019: E4 0000 0000
- Turn LED OFF
- RGB = (0, 0, 0) = Black
Line 01A: 55 0A00 0000
- Display "OFF" text at position (10, 0)
- Value
0000represents OFF state
Line 01B: 0E 0000 001E
- Jump to end of loop
Line 01C: E4 00FF FFFF
- Turn LED ON
- RGB = (255, 255, 255) = White
Line 01D: 55 0A00 0001
- Display "ON" text at position (10, 0)
Loop Management (Lines 01E-020)โ
Line 01E: 11 0021 0000
- Store current button state to
R21 R21 = R00(save for next iteration)
Line 01F: 02 0096 0000
- Delay 150 milliseconds
0096= 150 in decimal- Provides debouncing to prevent false triggers
Line 020: 0E 0000 0010
- Jump back to line
010 - Creates infinite loop for continuous monitoring
๐จ State Transition Tableโ
| Current State | Button Press | New State | LED Action |
|---|---|---|---|
| OFF (0) | Pressed | ON (1) | Turn ON (White) |
| ON (1) | Pressed | OFF (0) | Turn OFF (Black) |
| OFF (0) | Not Pressed | OFF (0) | No change |
| ON (1) | Not Pressed | ON (1) | No change |
๐ Key Learning Pointsโ
- Digital Input Reading: How to read button states from IO pins
- Edge Detection: Detecting button press vs. button hold
- Toggle Logic: Implementing on/off state machine
- Debouncing: Preventing false triggers with delays
- Conditional Control: Using comparisons for decision making
- RGB Color: Setting LED colors for different states
๐งช Testing Checklistโ
- Button is properly configured as digital input
- LED turns on (white) when button is pressed
- LED turns off when button is pressed again
- Display shows correct "ON" or "OFF" status
- Button debouncing works (no false triggers)
- System responds reliably to repeated presses
๐ก Extension Challengesโ
- Add Dimming: Use button hold to adjust LED brightness
- Multiple Colors: Cycle through different colors instead of just on/off
- Dual Button System: Add separate on and off buttons
- Auto-Off Timer: Automatically turn off after 5 minutes
- Status Indicator: Add a second LED to show power status
- Remote Control: Use infrared or Bluetooth for wireless control
๐ Common Issues and Solutionsโ
Issue 1: LED Flickers Rapidlyโ
Cause: No debouncing or insufficient delay Solution: Increase delay time (line 01F) to 200-300ms
Issue 2: Button Doesn't Respondโ
Cause: Incorrect IO configuration Solution: Verify line 001 sets correct pins as input
Issue 3: LED Toggles Multiple Timesโ
Cause: Button bounce or delay too short Solution: Increase debounce delay or add additional filtering
Issue 4: Display Shows Wrong Textโ
Cause: Incorrect state variable or comparison Solution: Check R20 value and comparison logic
๐ Related Documentationโ
Instructions Used in This Projectโ
| Opcode | Name | Description | Documentation Link |
|---|---|---|---|
0x02 | Delay | Set delay time in milliseconds | Delay |
0x04 | Greater Than Jump | Jump if R00 > immediate value | Conditional Jumps |
0x06 | Equal Jump | Jump if R00 == immediate value | Conditional Jumps |
0x0E | Unconditional Jump | Jump to specified line | Unconditional Jump |
0x10 | Write Register | Write immediate value to register | Register Operations |
0x11 | Copy Register | Copy value from one register to another | Register Operations |
0x12 | Add Register | Add immediate value to register | Register Operations |
0x14 | Read DI | Read digital input pins to R00 | Digital I/O |
0x17 | AND Register | Bitwise AND operation | Bitwise Operations |
0x55 | Display Text | Display register value on screen | Text Output |
0xCB | Enable WS2812B | Enable LED control module | External Devices |
0xE4 | Set LED Color | Set RGB color for specific LED | Lights & Music |
Additional Referenceโ
- IO Configuration Register (R25) - Pin direction control
- Register Map - Complete register documentation
- Instruction Set Summary - Full instruction set overview