> ## 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.

# Fly a flight path

> Program the drone to fly shapes and patterns - and use loops to do it the smart way.

## Make the drone fly a shape

Now that your drone takes off and lands from code, let's send it on a journey. You'll program it to fly a **square** - and then discover how **loops** make it far easier.

<Warning>
  Fly in a big, clear space. A square needs room in every direction. Keep the red LAND NOW button handy.
</Warning>

## The movement blocks

Two Flight blocks do all the moving:

* **fly \[forward] for \[1] second(s) at \[50] % speed** - flies a direction for a set time
* **spin \[left / right] for \[1] second(s) at \[50] % speed** - rotates in place for a set time

<Info>
  Notice turns are measured in **time**, not degrees. To turn about a quarter-circle, you spin for a short time and tune it. Start with a small spin time and adjust until the turn looks like a corner.
</Info>

## Fly a square, the long way

A square is four "fly forward, then spin" steps. Build it block by block:

```text theme={null}
take off
fly forward for 1 second(s) at 50 % speed
spin right for 1 second(s) at 50 % speed
fly forward for 1 second(s) at 50 % speed
spin right for 1 second(s) at 50 % speed
fly forward for 1 second(s) at 50 % speed
spin right for 1 second(s) at 50 % speed
fly forward for 1 second(s) at 50 % speed
spin right for 1 second(s) at 50 % speed
land
```

Run it and adjust the **spin time** until each turn is about a corner. That's a lot of repeated blocks, though...

## Fly a square, the smart way: loops

Notice you repeated the exact same two blocks four times. A **loop** does the repeating for you. Open the **Loops** category and use a **repeat \[4] times** block:

<Frame caption="The Loops blocks: repeat, repeat while, count with, for each, and break">
  <img src="https://mintcdn.com/intellectualpoint/eW7U9shBQKKM3XX9/images/drones/echo-loops-blocks.png?fit=max&auto=format&n=eW7U9shBQKKM3XX9&q=85&s=511fdbe31141817edaa0bab2aa5c263f" alt="echo.pitsco.com Loops block category" width="1024" height="623" data-path="images/drones/echo-loops-blocks.png" />
</Frame>

```text theme={null}
take off
repeat 4 times
  fly forward for 1 second(s) at 50 % speed
  spin right for 1 second(s) at 50 % speed
land
```

Same square, way less work. This is one of the most powerful ideas in all of coding: **don't repeat yourself - loop.**

<Frame caption="Nine blocks the long way, or three blocks with a loop - same square">
  <img src="https://mintcdn.com/intellectualpoint/eW7U9shBQKKM3XX9/images/drones/loop-vs-longway.png?fit=max&auto=format&n=eW7U9shBQKKM3XX9&q=85&s=63b39f50fc525c114559c08c8e2e847f" alt="Comparison of writing a square the long way versus using a repeat loop" width="1536" height="1024" data-path="images/drones/loop-vs-longway.png" />
</Frame>

<Info>
  The two blocks inside "repeat 4 times" run four times total - four sides and four turns make a square back to the start.
</Info>

## Try other shapes

Change the **repeat count** and the **spin time** and you can fly almost any shape. The more sides a shape has, the more repeats you need - and the *shorter* each turn.

<Frame caption="Repeat counts and turn amounts for common shapes">
  <img src="https://mintcdn.com/intellectualpoint/eW7U9shBQKKM3XX9/images/drones/shapes-gallery.png?fit=max&auto=format&n=eW7U9shBQKKM3XX9&q=85&s=cdfbbe0227f0f9c06b8f698a7b45fd40" alt="Gallery of shapes showing loop counts and turn angles for triangle, square, hexagon, and circle" width="1536" height="1024" data-path="images/drones/shapes-gallery.png" />
</Frame>

<CardGroup cols={2}>
  <Card title="Triangle" icon="triangle">
    Repeat **3 times** with a slightly longer spin time for sharper turns.
  </Card>

  <Card title="Hexagon" icon="hexagon">
    Repeat **6 times** with a shorter spin time between sides.
  </Card>

  <Card title="Rectangle" icon="rectangle-horizontal">
    Alternate a long and short **fly forward** time inside the loop.
  </Card>

  <Card title="Spin in place" icon="rotate-3d">
    Just repeat several small spins with no forward flight.
  </Card>
</CardGroup>

## Debug like a pro

If your shape comes out wrong, don't guess - **change one thing at a time**:

<Steps>
  <Step title="Watch which step fails" icon="eye">
    Run it and note exactly where the path goes wrong.
  </Step>

  <Step title="Adjust one value" icon="sliders-horizontal">
    Tweak a single spin time or fly time, then run again.
  </Step>

  <Step title="Compare" icon="git-compare">
    See if it improved. Repeat until the shape is clean.
  </Step>
</Steps>

<Frame caption="The debugging loop every engineer uses: watch, change one thing, compare, repeat">
  <img src="https://mintcdn.com/intellectualpoint/eW7U9shBQKKM3XX9/images/drones/debug-loop.png?fit=max&auto=format&n=eW7U9shBQKKM3XX9&q=85&s=faba7ad9f218c252ec15997f292e6a7b" alt="Debugging cycle diagram: watch, change one thing, compare, repeat" width="1536" height="1024" data-path="images/drones/debug-loop.png" />
</Frame>

<Warning>
  The classic beginner mistake is changing three things at once, then not knowing which one helped. **One change, one test.** It feels slower but it's how you actually solve the problem.
</Warning>

<Tip>
  Because turns are timed, the perfect spin time depends on your drone and battery level. Finding it by testing and tuning is real engineering - and exactly how pilots calibrate autonomous drones.
</Tip>

<Check>
  You programmed the drone to fly a square both the long way and with a loop, and you tuned the spin time to shape the turns.
</Check>
