> ## Documentation Index
> Fetch the complete documentation index at: https://stem-docs.intellectualpoint.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Advanced programs

> Build real missions in AT-66BL - loops, variables, decisions, operators, broadcasts, and custom blocks - with complete step-by-step programs.

## From commands to missions

You can already take off, move, and land. Now you'll combine blocks into **smart, flexible programs** using the ideas real programmers use every day: **loops**, **variables**, **decisions**, **operators**, **broadcasts**, and **custom blocks**.

Work through these programs in order. Each one adds exactly one new idea, and each ends with a challenge to make it your own. That's four-plus hours of building - pick up wherever your class is.

<Frame caption="Each program climbs one rung up the coding ladder - loops, variables, decisions, and beyond">
  <img src="https://mintcdn.com/intellectualpoint/eW7U9shBQKKM3XX9/images/drones/concept-ladder.png?fit=max&auto=format&n=eW7U9shBQKKM3XX9&q=85&s=7164d369780cd68dfad180b31a5a3e04" alt="A ladder of coding concepts building from sequence up to custom blocks" width="1536" height="1024" data-path="images/drones/concept-ladder.png" />
</Frame>

<Warning>
  Every program below flies the drone. Run in a clear space, keep people back, and keep the 🔴 **stop** and 🚫 **no-entry** buttons in reach.
</Warning>

## Program 1 - Fly a square with a loop

Instead of writing "move, turn" four times, use a **repeat** loop. This is the single most useful pattern in drone coding.

<Frame caption="Control blocks: repeat, forever, if-then, wait until, and repeat until">
  <img src="https://mintcdn.com/intellectualpoint/i7Ng90YWtTAWa_94/images/drones/at66bl-control-blocks.png?fit=max&auto=format&n=i7Ng90YWtTAWa_94&q=85&s=90055b95abf226a69e400952fb3d044a" alt="AT-66BL Control category blocks including repeat and forever" width="1024" height="811" data-path="images/drones/at66bl-control-blocks.png" />
</Frame>

<Steps>
  <Step title="Start and take off" icon="flag">
    Add **when 🏳 clicked**, then **take off**.
  </Step>

  <Step title="Add a repeat loop" icon="repeat">
    From **Control**, drag a **repeat \[10]** block and change it to **4**.
  </Step>

  <Step title="Fill the loop" icon="shapes">
    Inside the loop, add **horizontal move \[Forward] \[50] cm speed \[1]**, then **turn \[Right] \[90] degree speed \[1]**.
  </Step>

  <Step title="Land" icon="plane-landing">
    Snap **land** below the loop.
  </Step>
</Steps>

```text theme={null}
when 🏳 clicked
take off
repeat 4
  horizontal move Forward 50 cm speed 1
  turn Right 90 degree speed 1
land
```

<Card title="Challenge" icon="trophy" horizontal>
  Change the repeat to **3** and the turn to **120°** to fly a triangle. What repeat count and turn make a hexagon? (Hint: 360 ÷ sides.)
</Card>

## Program 2 - One number, any shape (variables)

A **variable** is a named box that holds a value. Store the number of sides once, and the whole program adapts.

<Frame caption="Variables blocks: make, set, and change your own values">
  <img src="https://mintcdn.com/intellectualpoint/eW7U9shBQKKM3XX9/images/drones/at66bl-variables-blocks.png?fit=max&auto=format&n=eW7U9shBQKKM3XX9&q=85&s=c13cad7b2c0e3bcbab7b79c3ed9df8ef" alt="AT-66BL Variables category blocks" width="1024" height="811" data-path="images/drones/at66bl-variables-blocks.png" />
</Frame>

<Steps>
  <Step title="Make a variable" icon="box">
    Click **Make a Variable** and name it `sides`.
  </Step>

  <Step title="Set it" icon="pen">
    After take off, add **set \[sides] to \[4]**.
  </Step>

  <Step title="Use it in the loop" icon="repeat">
    Use **repeat \[sides]**. For the turn, drop an **Operators** division block in and build **turn Right (360 / sides) degree**.
  </Step>
</Steps>

```text theme={null}
when 🏳 clicked
set sides to 4
take off
repeat (sides)
  horizontal move Forward 50 cm speed 1
  turn Right (360 / sides) degree speed 1
land
```

Now change `sides` from 4 to 6 and the **same program** flies a hexagon. **One change reshapes the whole flight** - that's the power of variables.

<Frame caption="Change one variable, get a totally different shape - the same code flies them all">
  <img src="https://mintcdn.com/intellectualpoint/PocgpVBYuYokR5M6/images/drones/variable-any-shape.png?fit=max&auto=format&n=PocgpVBYuYokR5M6&q=85&s=98a18a00d210d3ed1733e35d6df012bf" alt="Diagram showing how changing one variable value changes the flown shape" width="1536" height="1024" data-path="images/drones/variable-any-shape.png" />
</Frame>

<Card title="Challenge" icon="trophy" horizontal>
  Add **set \[size] to \[50]** and use it for the move distance too. Now one program flies any shape at any size.
</Card>

## Program 3 - Loops inside loops (a spiral tower)

Put a loop inside a loop to build patterns. Fly a square, rise a little, repeat.

```text theme={null}
when 🏳 clicked
take off
repeat 3
  repeat 4
    horizontal move Forward 40 cm speed 1
    turn Right 90 degree speed 1
  vertical move up 20 cm speed 1
land
```

The inner loop flies a square; the outer loop stacks three squares into a climbing tower.

<Card title="Challenge" icon="trophy" horizontal>
  Make each layer smaller than the last using a `size` variable and **change size by -10** each loop.
</Card>

## Program 4 - Make it decide (if-then + operators)

An **if-then** block lets the drone choose what to do. Combine it with **Operators** to build a condition.

<Frame caption="Operators blocks: random, comparisons, and logic">
  <img src="https://mintcdn.com/intellectualpoint/eW7U9shBQKKM3XX9/images/drones/at66bl-operators-blocks.png?fit=max&auto=format&n=eW7U9shBQKKM3XX9&q=85&s=f25b765e9a2705a700d8e0be9643852f" alt="AT-66BL Operators category blocks" width="1024" height="811" data-path="images/drones/at66bl-operators-blocks.png" />
</Frame>

Use **pick random** so every run is different:

```text theme={null}
when 🏳 clicked
take off
repeat 4
  if (pick random 1 to 2) = 1 then
    turn Right 90 degree speed 1
  else
    turn Left 90 degree speed 1
  horizontal move Forward 40 cm speed 1
land
```

Each lap the drone flips a coin and turns left or right - the path is a surprise every time.

<Card title="Challenge" icon="trophy" horizontal>
  Use **pick random 1 to 4** and multiply by 90 to pick a random turn each time.
</Card>

## Program 5 - Time a routine (Sensing + timer)

The **timer** reports seconds since the program started. Pair it with a loop to run for a set time.

<Frame caption="Sensing blocks: timer and reset timer">
  <img src="https://mintcdn.com/intellectualpoint/eW7U9shBQKKM3XX9/images/drones/at66bl-sensing-blocks.png?fit=max&auto=format&n=eW7U9shBQKKM3XX9&q=85&s=42cc410f773bd4c7166f14a426d5d530" alt="AT-66BL Sensing category blocks" width="1024" height="811" data-path="images/drones/at66bl-sensing-blocks.png" />
</Frame>

```text theme={null}
when 🏳 clicked
reset timer
take off
repeat until (timer > 10)
  fly Front speed 1
  wait 1 seconds
  turn Left 45 degree speed 1
land
```

The drone loops its routine until 10 seconds have passed, then lands - no matter how many laps it managed.

<Card title="Challenge" icon="trophy" horizontal>
  Show the timer on screen with **show variable**, and make the drone do a different move every 3 seconds.
</Card>

## Program 6 - Two scripts talking (broadcast)

**Broadcast** lets one part of your program trigger another. This keeps big programs organized.

<Frame caption="Events blocks: broadcast and when I receive">
  <img src="https://mintcdn.com/intellectualpoint/i7Ng90YWtTAWa_94/images/drones/at66bl-events-blocks.png?fit=max&auto=format&n=i7Ng90YWtTAWa_94&q=85&s=030cdef9e22988bab003c05bc046b8b6" alt="AT-66BL Events category blocks" width="1024" height="811" data-path="images/drones/at66bl-events-blocks.png" />
</Frame>

```text theme={null}
when 🏳 clicked
take off
broadcast dance and wait
land
```

```text theme={null}
when I receive dance
repeat 4
  turn Right 90 degree speed 1
  vertical move up 10 cm speed 1
```

The main script handles take off and land; the "dance" script handles the moves. Send **dance** whenever you want that routine.

## Program 7 - Build your own block (My Blocks)

Package a routine into one **custom block** you name yourself. This is how pros keep code readable.

<Frame caption="Make a Block - name your own reusable command">
  <img src="https://mintcdn.com/intellectualpoint/eW7U9shBQKKM3XX9/images/drones/at66bl-make-block.png?fit=max&auto=format&n=eW7U9shBQKKM3XX9&q=85&s=6789bc27d02e620b21dce90bb1c6e5e7" alt="AT-66BL Make a Block dialog" width="1024" height="811" data-path="images/drones/at66bl-make-block.png" />
</Frame>

<Steps>
  <Step title="Make a Block" icon="puzzle">
    In **My Blocks**, click **Make a Block** and name it `square`. Click **OK**.
  </Step>

  <Step title="Define it" icon="pen">
    Under the new **define square** hat, put the four blocks that fly a square.
  </Step>

  <Step title="Use it" icon="repeat">
    Now your main program can just call `square` - as many times as you like.
  </Step>
</Steps>

```text theme={null}
define square
  repeat 4
    horizontal move Forward 40 cm speed 1
    turn Right 90 degree speed 1

when 🏳 clicked
take off
square
vertical move up 20 cm speed 1
square
land
```

<Tip>
  Great programmers build in small, tested steps. Get a simple version flying, run it, then add **one** new feature at a time. If it breaks, you know exactly which change did it.
</Tip>

## Instructor cheat sheet

<AccordionGroup>
  <Accordion title="If the green flag does nothing" icon="flag">
    Check for a **when 🏳 clicked** hat at the top of the stack, and confirm the drone is still paired (Bluetooth icon).
  </Accordion>

  <Accordion title="If the drone drifts on shapes" icon="wind">
    Fly slower (lower the speed), use larger distances, and start with a fully charged battery. Indoor drafts and low battery both cause drift.
  </Accordion>

  <Accordion title="If a loop never ends" icon="repeat">
    A **repeat until** needs a condition that eventually becomes true. Test the condition with **show variable** or a shorter timer.
  </Accordion>

  <Accordion title="Keep the energy up" icon="bolt">
    Turn each challenge into a quick "show me" - students fly their version for the class. Fast feedback keeps everyone building.
  </Accordion>
</AccordionGroup>

<Check>
  You built programs using a **loop**, a **variable**, an **if-then decision**, the **timer**, a **broadcast**, and a **custom block**, and you can explain what each one adds.
</Check>

<Card title="Next: the capstone project" icon="arrow-right" href="/drone-programming/day-5/capstone">
  Design, code, and present your own drone mission.
</Card>
