{
  "schema": "tinyskiff.lessonPacket.v0",
  "lessonCode": "TSK-DAY16-SOFTCOLOUR",
  "course": "TinySkiff ESP32-S3 Lab",
  "day": 16,
  "title": "Soft colourful light: map input to colour",
  "status": "published",
  "learnerProfile": "adult beginner; curious and capable; no electronics assumed",
  "estimatedTimeMinutes": 25,
  "mission": "Wire three knobs to three input pins and the RGB LED to three PWM pins, upload the Arduino sketch, and turn the knobs to mix any colour by hand — a small mixing desk where each knob owns one channel of light.",
  "mainPath": "Arduino/C++",
  "optionalSidePath": "MicroPython",
  "sourceMetadata": {
    "officialPdf": "source/Freenove_Super_Starter_Kit_for_ESP32_S3-main/C/C_Tutorial.pdf",
    "chapter": "Chapter 10 Potentiometer & LED",
    "page": 113,
    "arduinoSketch": "source/Freenove_Super_Starter_Kit_for_ESP32_S3-main/C/Sketches/Sketch_10.2_SoftColorfulLight/Sketch_10.2_SoftColorfulLight.ino",
    "micropythonFile": "source/Freenove_Super_Starter_Kit_for_ESP32_S3-main/Python/Python_Codes/09.2_Soft_Colorful_Light/Soft_Colorful_Light.py",
    "imageAsset": "docs/course/assets/day-16/circuit-page-113.png",
    "imageAlt": "Official Freenove circuit for soft colourful light: three rotary potentiometers with their middle pins on GPIO 12, 13, and 14, and an RGB LED whose red, green, and blue legs run through 220 ohm resistors to GPIO 38, 39, and 40, with the long common pin joined to 3.3 volts, shown as both a schematic and a breadboard photo.",
    "licenseNote": "Based on Freenove official material released under CC BY-NC-SA 3.0; preserve attribution and non-affiliation language.\n"
  },
  "parts": [
    {
      "name": "ESP32-S3 board",
      "imageAsset": "docs/course/assets/shared/item-esp32-s3-board.jpg",
      "explanation": "A development board is a friendly package around a tiny computer chip. The ESP32-S3 runs your uploaded sketch, controls pins, reads sensors, and can also use USB, Wi-Fi, and Bluetooth. In this lesson, it sends the ping signal and measures the echo time."
    },
    {
      "name": "GPIO extension board",
      "imageAsset": "docs/course/assets/shared/item-gpio-extension-board.png",
      "explanation": "GPIO means general-purpose input/output. These pins are connection points your code can use. The extension board makes them easier to see, reach, and wire without cramming everything onto the ESP32-S3 itself."
    },
    {
      "name": "3 × rotary potentiometers",
      "imageAsset": "docs/course/assets/shared/item-potentiometer.png",
      "explanation": "A potentiometer is a resistor with a third pin on a movable contact. Turning the shaft slides that contact along the resistor, so the voltage on the middle pin sweeps smoothly between the two ends. Wire the ends to 3.3V and ground and the knob becomes a voltage dial your ADC pin can read."
    },
    {
      "name": "RGB LED",
      "imageAsset": "docs/course/assets/shared/item-rgb-led.png",
      "explanation": "An RGB LED is three tiny LEDs — red, green, and blue — in one dome. Set each one's brightness with PWM and your eye blends them into a single colour. All off is dark; all full is white; everything else is a mix — the same trick a screen uses."
    },
    {
      "name": "3 × 220 Ω resistors",
      "imageAsset": "docs/course/assets/shared/item-resistor.png",
      "explanation": "A resistor limits how much current flows. Placed in series with the LED, the 220 Ω one keeps the current gentle enough to light the LED instead of burning it out. The bands of colour print its value, and it has no direction."
    },
    {
      "name": "Jumper wires",
      "imageAsset": "docs/course/assets/shared/item-jumper-wire.png",
      "explanation": "Jumper wires make temporary connections without soldering. Female-to-male wires have a socket on one end and a pin on the other, useful when a sensor module and board use different connector styles."
    },
    {
      "name": "Arduino IDE",
      "imageAsset": "docs/course/assets/shared/item-arduino-ide.png",
      "explanation": "Arduino IDE is the desktop app that opens the sketch, compiles it, uploads it to the ESP32-S3, and shows messages from the board in Serial Monitor."
    }
  ],
  "wiring": [
    {
      "partPin": "Red knob middle pin",
      "connectTo": "GPIO 12",
      "reason": "The board reads this knob to set how much red shows."
    },
    {
      "partPin": "Green knob middle pin",
      "connectTo": "GPIO 13",
      "reason": "The board reads this knob to set how much green shows."
    },
    {
      "partPin": "Blue knob middle pin",
      "connectTo": "GPIO 14",
      "reason": "The board reads this knob to set how much blue shows."
    },
    {
      "partPin": "Each knob's outer legs",
      "connectTo": "3.3V and GND",
      "reason": "The two ends give the middle pin a full sweep from zero to full voltage."
    },
    {
      "partPin": "Red leg (via 220 Ω)",
      "connectTo": "GPIO 38",
      "reason": "PWM on this pin sets the red brightness."
    },
    {
      "partPin": "Green leg (via 220 Ω)",
      "connectTo": "GPIO 39",
      "reason": "PWM on this pin sets the green brightness."
    },
    {
      "partPin": "Blue leg (via 220 Ω)",
      "connectTo": "GPIO 40",
      "reason": "PWM on this pin sets the blue brightness."
    },
    {
      "partPin": "Common (long) pin",
      "connectTo": "3.3V",
      "reason": "All three colours share this positive pin so a colour lights when its pin goes LOW."
    }
  ],
  "coachInstructions": [
    "Anchor the pairing early — knob on GPIO 12 drives red on 38, 13 drives green on 39, 14 drives blue on 40 — and have the learner say it back before debugging.",
    "If one colour never changes, check that knob's middle wire on 12/13/14 and its outer legs on 3.3V and GND before touching code.",
    "If turning a knob moves the wrong colour, the RGB legs are swapped on 38/39/40 — fix the order rather than the sketch.",
    "If the dome is always dark, check the common pin reaches 3.3V; also confirm all three knobs turned down is correct dark behaviour before treating it as a fault.",
    "The 256-minus inversion is Day 9's common-anode trick — explain it on request and keep the sketch's map upper bound of 4096 exactly as written.",
    "Keep the Arduino path primary; MicroPython pairs the knobs to colours in reverse order (12 drives 40) and that difference is expected, so never \"fix\" it."
  ],
  "steps": [
    "Seat the ESP32-S3 on the GPIO extension board and keep USB unplugged while you wire.",
    "Place the three potentiometers on the breadboard with room to turn each knob.",
    "Wire each knob's outer legs to 3.3V and GND so its middle pin can sweep the full range.",
    "Run the middle pins to GPIO 12, 13, and 14 — red, green, and blue in that order.",
    "Wire the RGB LED's red leg through a 220 Ω resistor to GPIO 38, green to GPIO 39, blue to GPIO 40.",
    "Run the LED's long common pin to 3.3V.",
    "Compare every wire to the chart before you plug in USB.",
    "Open Sketch_10.2_SoftColorfulLight.ino in Arduino IDE and upload it.",
    "Turn each knob in turn and watch its colour rise and fall in the dome."
  ],
  "codeFocus": {
    "arduino": [
      {
        "line": "const byte adcChns[] = {12, 13, 14}",
        "explanation": "Three matched arrays pair each knob with a colour leg — position 0 links the knob on 12 to the red leg on 38 and so on down the line."
      },
      {
        "line": "colors[i] = map(analogRead(adcChns[i]), 0, 4096, 0, 255)",
        "explanation": "Reads one knob (0–4095) and rescales it onto the 0–255 brightness range in a single line."
      },
      {
        "line": "ledcWrite(ledPins[i], 256 - colors[i])",
        "explanation": "Day 9's common-anode inversion — this dome lights when its pin goes LOW so the sketch subtracts the value before writing it."
      },
      {
        "line": "ledcAttachChannel(ledPins[i], 1000, 8, pwmChns[i])",
        "explanation": "Gives each colour leg its own hardware PWM channel at 1 kHz with 8-bit brightness."
      }
    ],
    "micropython": [
      {
        "line": "pwm0.duty(1023-remap(adc0.read(),0,4095,0,1023))",
        "explanation": "The whole chain in one line — read the knob, rescale to the 10-bit duty range, and subtract for the common-anode dome."
      }
    ]
  },
  "theoryModel": {
    "plainLanguage": "The same read-map-write chain runs three times per pass — arrays hold the pin numbers and the loop walks them.",
    "formula": "colour = three knob readings → three mapped brightnesses",
    "notes": [
      {
        "title": "Why arrays and a loop",
        "body": "Writing the pins as three matched arrays lets one loop serve every channel. Adding a fourth channel would mean one more entry in each array and nothing else."
      },
      {
        "title": "The same inversion as Day 9",
        "body": "The common pin sits at 3.3V, so a colour is brightest when its own pin is pulled LOW. Subtracting the value from 256 flips the scale so a bigger knob reading still means a brighter colour."
      }
    ]
  },
  "test": {
    "expectedOutputExample": [
      "Turn one knob and one colour sweeps smoothly from dark to full while the other two hold steady.",
      "Set the knobs in combinations and the dome mixes the shades — red and green up makes yellow.",
      "All three knobs up glows white; all three down leaves the dome dark."
    ],
    "successCriteria": "Every knob turned down means darkness by design — white takes all three channels at full."
  },
  "troubleshooting": [
    {
      "symptom": "One colour never changes",
      "firstChecks": [
        "Check that knob's middle wire on GPIO 12, 13, or 14 — the board is reading air."
      ]
    },
    {
      "symptom": "Turning the red knob changes the wrong colour",
      "firstChecks": [
        "The RGB legs are in the wrong order — red goes to GPIO 38, green to 39, blue to 40."
      ]
    },
    {
      "symptom": "The dome stays dark with knobs up",
      "firstChecks": [
        "Check the common pin — it goes to 3.3V."
      ]
    },
    {
      "symptom": "Upload fails",
      "firstChecks": [
        "Swap in a data-capable USB cable."
      ]
    }
  ],
  "challenge": "Dial the dome to sunset orange and record the three knob levels, then rewrite one line so two knobs mix the whole colour.",
  "logbookPrompts": [
    "What three numbers made your sunset orange?",
    "Which colour surprised you most when you mixed it?",
    "With blue riding the green knob, which colours could you no longer reach?"
  ]
}