# Pedal-Based Fountain Control Implementation ## Overview This implementation replaces the previous automatic fountain control with a **direct pedal-based control system**. When the pedal is in AUTO mode, the chocolate fountain directly follows the pedal state - ON when pedal is active, OFF when pedal is inactive. ## โœ… Expected Behavior When pedal is in **AUTO mode**: - **Pedal ON** (pedalState = 0) โ†’ **Chocolate ON** (fountain flows) - **Pedal OFF** (pedalState = 1) โ†’ **Chocolate OFF** (fountain stops) - **No blinking/flashing** of the Chocolate button - **Clean alternating ON/OFF** based on pedal timing settings ## ๐Ÿ”ง Implementation Details ### 1. Direct Fountain Control Logic Located in the main monitoring loop (`pedalMotor == 1` section): ```csharp else if (pedalMotor == 1) // auto Pedal { // Set auto mode flag isPedalAutoMode = true; // Direct fountain control based on pedal state var fount = _mapping.Find(x => x.Name.ToLower() == "Helix".ToLower()); if (fount != null && fount.BitNumbers.Count > 0) { if (pedalState == 0) // Pedal ON - Turn fountain ON { foreach (var bit in fount.BitNumbers) { holdingRegister.motor |= (ushort)(1 << bit); // Set the motor bit ON } isFountainMotorOn = true; startFountainMotor = 1; sendComFountainMotor = 1; startFountainMotorFlashing = -1; // No flashing // ... UI updates and hardware commands } else if (pedalState == 1) // Pedal OFF - Turn fountain OFF { foreach (var bit in fount.BitNumbers) { holdingRegister.motor &= (ushort)~(1 << bit); // Clear the motor bit OFF } isFountainMotorOn = false; startFountainMotor = 0; sendComFountainMotor = 0; startFountainMotorFlashing = -1; // No flashing // ... UI updates and hardware commands } } // Handle pedal timing (alternating ON/OFF based on recipe settings) // ... timer management logic } ``` ### 2. Pedal Timing Configuration From the UI, users can configure: - **PEDAL OFF TIME**: Duration fountain stays OFF (e.g., 1 unit) - **PEDAL ON TIME**: Duration fountain stays ON (e.g., 2 seconds) The system automatically alternates between these states using timers. ### 3. UI Feedback #### Chocolate Button State - **ON**: Shows "ON" with active color (magenta underline) - **OFF**: Shows "OFF" with passive color (gray) - **No flashing/blinking** during automatic operation #### Temperature Display When fountain is ON, the UI also shows: - Current Temp: [actual temperature] - Target Temp: [target temperature] ### 4. Manual Override When user manually clicks the fountain button: - **Resets pedal auto mode** (`isPedalAutoMode = false`) - **Stops all pedal timers** - **Switches to manual mode** (`pedalMotor = -1`) - **User gains full manual control** ### 5. Protection Logic #### Fountain Control Protection Normal fountain control logic is disabled when pedal auto mode is active: ```csharp // Temperature-based control (disabled in pedal auto mode) if (checkFountainTMT_PMT && !isPedalAutoMode) // Manual control (disabled in pedal auto mode) if (!checkFountainTMT_PMT && !isPedalAutoMode) // Flashing processing (disabled in pedal auto mode) if (startFountainMotorFlashing == 1 && !isPedalAutoMode) ``` #### UI Flashing Protection The InteractiveUILoop prevents button flashing in pedal auto mode: ```csharp // Only flash when not in pedal auto mode if (_mainWindow.startFountainMotorFlashing == 1 && !_mainWindow.isPedalAutoMode) ``` ## ๐Ÿ”„ Control Flow 1. **User sets pedal to AUTO mode** - Pedal timers start alternating - `isPedalAutoMode = true` 2. **Pedal timer triggers state change** - `pedalState = 0` (ON) or `pedalState = 1` (OFF) 3. **Main monitoring loop processes state** - Directly controls fountain motor based on pedal state - Updates UI immediately - Sends hardware commands 4. **Fountain responds immediately** - ON: Chocolate flows - OFF: Chocolate stops - No delays or intermediate states 5. **Cycle repeats based on timing settings** - OFF for configured duration - ON for configured duration ## ๐Ÿ›ก๏ธ Safety Features 1. **Manual Override**: User can always take manual control 2. **Timer Cleanup**: Pedal timers are properly stopped when needed 3. **State Reset**: Pedal auto mode is reset when starting/stopping recipes 4. **No Conflicts**: Other fountain control mechanisms are disabled during pedal auto mode 5. **Immediate Response**: No delays or buffering - fountain responds instantly to pedal state ## ๐Ÿšซ Previous Logic Removed The following complex logic was removed and replaced with direct control: - โŒ Temperature threshold-based fountain control in pedal auto mode - โŒ Automatic fountain control based on second control box - โŒ Complex state management with multiple flags - โŒ Delayed or conditional fountain control - โŒ Multiple control mechanisms that could conflict ## ๐Ÿ“ฑ User Interface ### Settings Panel Shows current pedal configuration: - **PEDAL**: AUTO/MANUAL toggle button - **PEDAL OFF TIME**: Adjustable duration (e.g., 1 unit) - **PEDAL ON TIME**: Adjustable duration (e.g., 2 seconds) ### Fountain Button - **Steady state display** (ON/OFF) - **Color coding**: Active (magenta) / Passive (gray) - **No blinking/flashing** during automatic operation - **Click to override** and take manual control ## ๐Ÿงช Testing Scenarios ### Scenario 1: Basic Auto Mode Operation 1. Set pedal to AUTO mode 2. Configure timing (e.g., 2s ON, 1 unit OFF) 3. **Expected**: Fountain alternates cleanly between ON/OFF states ### Scenario 2: Manual Override 1. While in auto mode, click fountain button 2. **Expected**: Auto mode stops, user gains manual control ### Scenario 3: Recipe Integration 1. Start recipe with pedal in AUTO mode 2. Complete all phases 3. **Expected**: Fountain follows pedal timing throughout ### Scenario 4: Visual Feedback 1. Observe fountain button during auto operation 2. **Expected**: Clean ON/OFF display without blinking ## ๐Ÿ“‹ Configuration ### Pedal Timing Settings - Configured through Settings UI - **PEDAL OFF TIME**: How long fountain stays OFF - **PEDAL ON TIME**: How long fountain stays ON - Values are saved in recipe table ### Control Mode - **AUTO**: Fountain follows pedal timing automatically - **MANUAL**: User controls fountain manually via button clicks ## ๐Ÿ”ง Key Improvements ### Simplified Logic โœ… - **Direct control**: Pedal state directly controls fountain - **No intermediate states** or complex decision trees - **Immediate response** with no delays ### User Experience โœ… - **Predictable behavior**: ON when pedal ON, OFF when pedal OFF - **Visual clarity**: Clean state display without blinking - **Easy override**: Click fountain button to take manual control ### Reliability โœ… - **No conflicts**: Other control mechanisms properly disabled - **Clean state management**: Single source of truth for pedal auto mode - **Proper cleanup**: Timers and states reset when needed ### Performance โœ… - **Efficient processing**: Minimal logic overhead - **Instant response**: No waiting or buffering - **Resource management**: Proper timer lifecycle management ## ๐Ÿ“ Technical Notes ### Timer Management - `pedalOnTimer`: Controls ON duration - `pedalOffTimer`: Controls OFF duration - Timers are properly disposed when not needed ### State Variables - `isPedalAutoMode`: Main flag indicating pedal auto mode is active - `pedalState`: Current pedal state (0=ON, 1=OFF, -1=reset) - `pedalMotor`: Pedal mode (1=AUTO, -1=MANUAL) ### Hardware Control - Direct bit manipulation of `holdingRegister.motor` - Immediate serial communication with hardware - Synchronized UI updates ## โœ… Requirements Fulfilled โœ… **Direct pedal control**: Fountain follows pedal state exactly โœ… **Timing-based operation**: Uses configured ON/OFF durations โœ… **No blinking**: Clean visual feedback without flashing โœ… **Manual override**: User can always take control โœ… **Proper integration**: Works with existing recipe system โœ… **Clean UI**: Professional appearance without distractions โœ… **Reliable operation**: No conflicts or unexpected behavior