--- name: sketch-patterns description: Canonical Arduino sketch templates for the Uno Q (blink, fade/PWM, button, analog read) and how to upload them. Load this whenever the user asks to write, run, upload, or flash a sketch, or asks for blink/fade/button/sensor code. --- # Uno Q Sketch Patterns Target the MCU with FQBN `arduino:zephyr:unoq`. Sketches are 3.3 V. ## Uploading To run a sketch on the board, call the **`uno_q_flash`** tool with `action` = `upload` and the full `.ino` in `code`. It compiles and flashes over SWD. Do not describe the steps — emit the tool call. ## Blink (built-in LED, D13) ```cpp void setup() { pinMode(LED_BUILTIN, OUTPUT); } void loop() { digitalWrite(LED_BUILTIN, HIGH); delay(500); digitalWrite(LED_BUILTIN, LOW); delay(500); } ``` ## Fade (PWM — only D3, D5, D6, D9, D10, D11) ```cpp const int PIN = 9; // must be a ~ PWM pin void setup() { pinMode(PIN, OUTPUT); } void loop() { for (int v = 0; v <= 255; v++) { analogWrite(PIN, v); delay(4); } for (int v = 255; v >= 0; v--) { analogWrite(PIN, v); delay(4); } } ``` ## Button (input with pull-up) ```cpp const int BTN = 2; void setup() { pinMode(BTN, INPUT_PULLUP); pinMode(LED_BUILTIN, OUTPUT); } void loop() { digitalWrite(LED_BUILTIN, digitalRead(BTN) == LOW ? HIGH : LOW); } ``` ## Analog read (0–3.3 V only!) ```cpp void setup() { Serial.begin(115200); } void loop() { int raw = analogRead(A0); // 0..1023 float volts = raw * 3.3 / 1023.0; // NEVER exceed 3.3 V on A0-A5 Serial.println(volts); delay(200); } ```