Skip to content

Swida-Alba/light_controller_v2

Repository files navigation

Light Controller V2.2

A flexible Arduino-based light control system with native pulsing support, pattern compression, automatic calibration management, precise timing control, and pulse frequency modulation.

Version: 2.2.2
Last Updated: December 12, 2025
Status: Production Ready βœ…


πŸ“‘ Table of Contents


🎯 What's New in v2.2

✨ Automatic Calibration System (NEW!)

  • πŸ€– Board identification - Unique ID per Arduino (serial number/VID:PID)
  • πŸ’Ύ Database storage - Calibrations saved to calibration_database.json
  • ♻️ Auto-retrieval - Stored calibrations automatically loaded
  • ⏰ 3-month expiration - Auto-recalibration every 90 days for accuracy
  • πŸ”§ Multi-board support - Different calibrations for different boards
  • πŸ“Š Database management - View, export, delete calibrations

Why 3 months? Crystal oscillators drift Β±1-5 ppm/year. Over 3 months, this accumulates to 0.25-1.25 ppm drift, potentially causing 1-4 seconds error over 12-hour protocols. Regular recalibration ensures <1 second timing accuracy.

πŸ“– Automatic Calibration Guide - Complete system documentation
πŸ“– Backward Compatibility - How old protocols still work

Pattern-Based Compression System

  • Automatic pattern detection - Reduces hundreds of commands to just a few
  • Compression ratios up to 37:1 - 97%+ reduction in transmitted data
  • pattern_length parameter - Optimize for 2-element (default) or 4-element patterns
  • Efficiency analysis - System recommends optimal pattern_length

πŸ“– Pattern Compression Guide - Complete compression details

Automatic Verification

  • Arduino compatibility checking - Prevents pattern_length mismatches
  • Early error detection - Catches issues before execution
  • Clear error messages - Tells you exactly how to fix problems

Enhanced Architecture

  • Class-based design - LightControllerParser class for reusability
  • 70% smaller entry point - protocol_parser.py now just 49 lines
  • Command preview - Test protocols without hardware

πŸ“– Refactoring Guide - Architecture details


πŸš€ Quick Start

1. Installation

# Clone repository
git clone https://github.com/Swida-Alba/light_controller_v2.git
cd light_controller_v2.2

# Install Python dependencies
pip install -r requirements.txt

πŸ“– Full Installation Guide - Detailed setup instructions

2. Upload Arduino Firmware

  1. Open light_controller_v2_2_arduino/light_controller_v2_2_arduino.ino in Arduino IDE
  2. Configure settings (if needed):
    const int PATTERN_LENGTH = 2;     // 2, 4, 8, etc. (must match Python)
    #define PULSE_MODE_COMPILE 1      // 1=Enable pulses, 0=Disable (saves ~2.5KB)
  3. Select your board: Tools β†’ Board β†’ Arduino Uno/Due/Mega
  4. Select port: Tools β†’ Port β†’ (your Arduino port)
  5. Click Upload

πŸ“– Arduino Setup Guide - Board-specific instructions
πŸ“– Firmware Update Guide - Updating existing firmware

3. Run Your First Protocol

Option A: Try Automatic Calibration (Recommended)

python protocol_parser.py 2 /dev/cu.usbmodem14301 examples/auto_calibration/simple_blink_example.txt

First time:

Identifying Arduino board...
Board ID: 0852420f343bb48d
No calibration found. Calibrate now? (Y/n): y
[Calibration proceeds for ~5 minutes]
βœ“ Calibration saved: 1.025847

Future runs:

βœ“ Found calibration: 1.025847
Age: 15 days (0.5 months) - Valid βœ“
[Uses stored calibration automatically]

Option B: Use Preset Calibration (Legacy)

python protocol_parser.py 2 /dev/cu.usbmodem14301 examples/preset_calibration/simple_blink_example.txt

Shows warning about manual CALIBRATION_FACTOR, but still works!

πŸ“– Usage Guide - Complete usage instructions
πŸ“– Examples Guide - All example protocols explained

4. View Real-Time Monitoring (Automatic!)

After execution starts, an interactive HTML visualization automatically opens showing:

  • πŸ”΄ Real-time status updates every second
  • πŸ’‘ LED indicators (ON/OFF/PULSING/WAITING/COMPLETED)
  • πŸ“Š Timeline view with current position marker
  • ⏱️ Time tracking (upload time, total elapsed, protocol elapsed per channel)

πŸ“– HTML Visualization Guide - Complete visualization features

5. Manage Calibrations

# View all calibrations with age and expiration status
python utils/manage_calibrations.py list

# Test board identification
python test_board_info.py

# Export calibrations
python utils/manage_calibrations.py export backup.txt

# Delete specific calibration
python utils/manage_calibrations.py delete <board_id>

πŸ“– Database Management - Complete calibration system guide


πŸ”‘ Key Features

Calibration & Timing

  • βœ… Automatic calibration - πŸ†• Board-specific with 90-day expiration
  • βœ… Multi-board support - πŸ†• Different calibrations per Arduino
  • βœ… Precise timing - Compensates for crystal oscillator variations
  • βœ… Flexible timing - Milliseconds to hours
  • βœ… Start time scheduling - Time-of-day or countdown

Pattern & Compression

  • βœ… Pattern compression - Up to 97% reduction in command count
  • βœ… Automatic verification - Arduino compatibility checking
  • βœ… Efficiency analysis - Optimal pattern_length recommendations

Control & Monitoring

  • βœ… Multi-channel control - Up to 8 channels
  • βœ… Native pulsing support - Hardware PWM for frequency and duty cycle modulation
  • βœ… Real-time visualization - 🎨 Interactive HTML with live status tracking
  • βœ… Auto-generated timelines - Visual protocol representation

Protocols & Formats

  • βœ… Multiple formats - Excel and Text protocols
  • βœ… Command preview - Test without hardware
  • βœ… Flexible protocols - πŸ†• Automatic or preset calibration

πŸ“– Features Overview - Complete feature list with examples


πŸ“‚ Examples

The examples/ folder contains ready-to-use protocol files demonstrating both calibration approaches:

πŸ†• Auto-Calibration Examples (Recommended)

Located in examples/auto_calibration/ - Uses automatic calibration system:

File Format Description Pattern Length
simple_blink_example.txt TXT Basic ON/OFF patterns 2
simple_blink_example.xlsx Excel Same as TXT (no calibration sheet) 2
pulse_protocol.txt TXT Multi-channel pulsed patterns 4
pulse_protocol.xlsx Excel Same as TXT 4
multi_channel_pattern.txt TXT Complex 4-element patterns 4
multi_channel_pattern.xlsx Excel Same as TXT 4

Key Feature: No CALIBRATION_FACTOR (TXT) or no calibration sheet (Excel) = automatic calibration enabled!

πŸ“– Auto-Calibration Examples Guide

πŸ”§ Preset Calibration Examples (Legacy)

Located in examples/preset_calibration/ - Uses manual CALIBRATION_FACTOR:

File Format Description Pattern Length
basic_protocol.txt/.xlsx Both Simple channel control 4
simple_blink_example.txt/.xlsx Both ON/OFF blink patterns 2
pulse_protocol.txt/.xlsx Both Various pulsing effects 4
wait_pulse_protocol.txt/.xlsx Both Wait status with pulse 4
pattern_length_4_example.txt/.xlsx Both Complex 4-element patterns 4
test_8_channels_pattern_length_4.txt TXT All 8 channels 4

Contains: Manual CALIBRATION_FACTOR: 1.000000 in each protocol

⚠️ Important: Calibration factors are board-specific! Each Arduino has unique crystal oscillator characteristics. Using the wrong calibration factor can cause significant timing drift.

πŸ“– Preset Calibration Examples Guide - Includes board-specific calibration explanation

πŸ“ Root Examples

Additional examples in examples/ root:

  • clean_protocol.txt - Minimal template without comments
  • complete_protocol.txt - Fully documented with all features

πŸ“– Complete Examples Guide - All examples explained
πŸ“– Quick Reference - Fast example lookup


πŸ“š Documentation Index

πŸ“– Complete Documentation Index & Navigation Guide
Comprehensive guide to all 50+ documentation files organized by topic, user journey, and task.

Quick Navigation by Task

New Users:
Installation β†’ Arduino Setup β†’ Usage Guide β†’ Examples

Create Protocol:
Protocol Formats β†’ Templates β†’ Examples

Optimize Performance:
Pattern Compression β†’ Verification

Calibration:
Auto-Calibration Guide β†’ Backward Compatibility β†’ Calibration Methods

Troubleshoot:
Troubleshooting β†’ Common Issues

Develop:
Refactoring Guide β†’ Folder Structure

πŸ“‘ Expand Full Documentation Categories

πŸš€ Getting Started

⏱️ Calibration System (NEW!)

πŸ“– Core Documentation

🎯 Pattern Compression

🎨 Visualization & Monitoring

πŸ’Ύ Memory & Pulse Mode

πŸ—οΈ Architecture & Development


πŸ“„ Protocol File Format

Supported Formats:

  • Text (.txt) - Version control friendly, command syntax
  • Excel (.xlsx) - Visual editing with spreadsheet interface

Calibration Options

Automatic Calibration (Recommended):

# TXT: Simply omit CALIBRATION_FACTOR line
PATTERN:1;CH:1;STATUS:1,0;TIME_MS:1000,1000;REPEATS:10
START_TIME: {'CH1': 0}

# Excel: Omit 'calibration' sheet
# System automatically identifies Arduino and applies stored calibration

Preset Calibration (Legacy):

# TXT: Include CALIBRATION_FACTOR
PATTERN:1;CH:1;STATUS:1,0;TIME_MS:1000,1000;REPEATS:10
START_TIME: {'CH1': 0}
CALIBRATION_FACTOR: 1.025847  # Board-specific value!

# Excel: Include 'calibration' sheet with factor

⚠️ Important: Calibration factors are board-specific. Each Arduino has unique crystal oscillator characteristics due to manufacturing tolerances, temperature effects, and component aging. Never copy calibration factors between different boards!

Text Format (.txt)

Required Parameters

PATTERN commands (at least one per channel):

PATTERN:<id>;CH:<channel>;STATUS:<states>;TIME_MS:<durations>;REPEATS:<count>;PULSE:<optional>

START_TIME (for all channels):

START_TIME: {'CH1': '21:00', 'CH2': 60, 'CH3': '2025-11-08 21:00:00'}

Optional Parameters

WAIT_STATUS, WAIT_PULSE, CALIBRATION_FACTOR (see docs for details)

πŸ“– Complete Protocol Syntax
πŸ“– Protocol Settings Guide
πŸ“– Templates


πŸ“ Project Structure

light_controller_v2.2/
β”œβ”€β”€ protocol_parser.py           # Main execution script
β”œβ”€β”€ preview_protocol.py          # Preview without Arduino
β”œβ”€β”€ viz_protocol_html.py         # HTML visualization generator
β”œβ”€β”€ light_controller_parser.py   # Core parser class
β”œβ”€β”€ lcfunc.py                    # Utility functions
β”œβ”€β”€ test_board_info.py           # πŸ†• Arduino board identification test
β”œβ”€β”€ calibration_database.json    # πŸ†• Stored calibrations (auto-generated)
β”œβ”€β”€ requirements.txt             # Python dependencies
β”œβ”€β”€ README.md                    # This file
β”œβ”€β”€ CHANGELOG.md                 # Version history
β”‚
β”œβ”€β”€ examples/                    # Example protocol files
β”‚   β”œβ”€β”€ auto_calibration/        # πŸ†• Automatic calibration examples
β”‚   β”‚   β”œβ”€β”€ README.md            # Auto-calibration guide
β”‚   β”‚   β”œβ”€β”€ simple_blink_example.txt/.xlsx
β”‚   β”‚   β”œβ”€β”€ pulse_protocol.txt/.xlsx
β”‚   β”‚   β”œβ”€β”€ multi_channel_pattern.txt/.xlsx
β”‚   β”‚   └── create_excel_examples.py  # Script to generate Excel examples
β”‚   β”‚
β”‚   β”œβ”€β”€ preset_calibration/      # πŸ†• Manual calibration examples (legacy)
β”‚   β”‚   β”œβ”€β”€ README.md            # Preset calibration guide
β”‚   β”‚   β”œβ”€β”€ simple_blink_example.txt/.xlsx
β”‚   β”‚   β”œβ”€β”€ pulse_protocol.txt/.xlsx
β”‚   β”‚   β”œβ”€β”€ basic_protocol.txt/.xlsx
β”‚   β”‚   β”œβ”€β”€ wait_pulse_protocol.txt/.xlsx
β”‚   β”‚   └── ... (more examples)
β”‚   β”‚
β”‚   β”œβ”€β”€ README.md                # Complete examples guide
β”‚   β”œβ”€β”€ QUICK_REFERENCE.md       # Quick lookup
β”‚   β”œβ”€β”€ clean_protocol.txt       # Minimal template
β”‚   └── complete_protocol.txt    # Fully documented example
β”‚
β”œβ”€β”€ docs/                        # Documentation (50+ guides)
β”‚   β”œβ”€β”€ DOCUMENTATION_INDEX.md   # πŸ“– Complete navigation guide
β”‚   β”œβ”€β”€ AUTO_CALIBRATION_DATABASE.md  # πŸ†• Calibration system
β”‚   β”œβ”€β”€ BACKWARD_COMPATIBILITY.md     # πŸ†• Legacy protocol support
β”‚   β”œβ”€β”€ PATTERN_COMPRESSION_GUIDE.md  # Optimization
β”‚   β”œβ”€β”€ HTML_VISUALIZATION.md    # Real-time monitoring
β”‚   └── ... (40+ more guides)
β”‚
β”œβ”€β”€ utils/                       # Utility & development tools
β”‚   β”œβ”€β”€ manage_calibrations.py   # πŸ†• Calibration database manager
β”‚   β”œβ”€β”€ debug_calibration_speed_test.py
β”‚   β”œβ”€β”€ verify_pattern_length_fix.py
β”‚   └── README.md
β”‚
└── light_controller_v2_2_arduino/ # Arduino firmware
    └── light_controller_v2_2_arduino.ino

πŸ†• New in v2.2.1:

  • calibration_database.json - Auto-generated calibration storage
  • examples/auto_calibration/ - Automatic calibration examples
  • examples/preset_calibration/ - Legacy manual calibration examples
  • test_board_info.py - Arduino identification testing
  • utils/manage_calibrations.py - Database management utility
  • docs/AUTO_CALIBRATION_DATABASE.md - Complete calibration guide
  • docs/BACKWARD_COMPATIBILITY.md - Legacy protocol compatibility

πŸ†˜ Need Help?

Quick Links by Topic

Topic Documentation
Setup Installation β†’ Arduino Setup
Calibration Auto-Calibration β†’ Backward Compatibility
Creating Protocols Protocol Formats β†’ Templates β†’ Examples
Optimization Pattern Compression β†’ Verification
Timing Issues Calibration Guide β†’ Calibration Methods
Pulse Control Pulse Period vs Section Time β†’ Pulse Memory
Visualization HTML Visualization β†’ Realtime Features
Development Refactoring Guide β†’ Folder Structure
Troubleshooting Troubleshooting Guide β†’ Common Bugs

Support Resources


πŸ“Š Project Status

Version: 2.2.2
Status: Production Ready βœ…
Tested: Python 3.6-3.13, Arduino Uno/Due/Mega
License: MIT

Version History

  • v2.2.2 (Dec 12, 2025) - Improved serial communication reliability for PULSE commands, fixed calibration skip issue
  • v2.2.1 (Nov 10, 2025) - Automatic calibration system with 3-month expiration, examples reorganization
  • v2.2.0 (Nov 8, 2025) - Pattern compression, auto-verification, real-time visualization
  • v2.1.0 (Nov 3, 2025) - Text protocol support, multiple time units
  • v2.0.0 - Initial release

πŸ“– Complete Changelog


🀝 Contributing

Contributions are welcome! Please:

  1. Read the Documentation Index
  2. Review Folder Structure
  3. Understand Refactoring Guide
  4. Submit pull requests with clear descriptions

πŸ™ Acknowledgments

Built with:

  • Python - Protocol parsing and serial communication
  • Arduino - Hardware control
  • NumPy - Data processing
  • Pandas - Protocol parsing (Excel)
  • PySerial - Serial communication
  • openpyxl - Excel file handling

Happy light controlling! πŸ’‘

Need calibration help? See Automatic Calibration Guide
Want to understand why calibration factors are board-specific? See Preset Calibration Examples

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published