# 2 - Cycling Through All Segments

### Video

{% embed url="<https://youtu.be/RQaOa9D5hpA>" %}

### Writing the Code

<figure><img src="/files/Ne6r19KbWO8NAvFKkyRK" alt=""><figcaption></figcaption></figure>

Start by importing modules and objects we'll be using.

<figure><img src="/files/GmAUTMWpfA9XqgnmCjBg" alt=""><figcaption></figcaption></figure>

Create a DigitalInOut object for each of the I/O pins we need to control. By default they all start off as inputs but we need to make them outputs if we're going to send signals with them. We could do this line-by-line by using each object's switch\_to\_output() method, but there's faster to get this done and set us up for easier control later on.

<figure><img src="/files/ECXfb2HdnMyOu9KFLxhg" alt=""><figcaption></figcaption></figure>

We're going to create two lists, one for segments and one for digits. This will make it easy to iterate over the objects in the list using for-loops!

<figure><img src="/files/Ot8wosbSCnq6sa9pH7cR" alt=""><figcaption></figcaption></figure>

These for-loops let us run the .switch\_to\_output() method for each of the objects in the list. Now all our segments and digits are ready to be used as outputs.

<figure><img src="/files/WBM3r8GZyIHZ1bw8ZKPo" alt=""><figcaption></figcaption></figure>

Now that our setup is done, we'll write a while-loop that runs forever.

<figure><img src="/files/dYSwI0UAsNAfr6OHsngL" alt=""><figcaption></figcaption></figure>

We know we can only have one digit active at a time, so we'll make a for-loop that iterates over the list of digits, activates the current digit, then deactivates it, and so on and so forth for each digit, forever.&#x20;

<figure><img src="/files/rWywRS7z4CHNJLRAeSef" alt=""><figcaption></figcaption></figure>

Next we'll create a for-loop that iterates through our entire list of segments, each time we activate a new digit. Placing a loop within a loop is known as 'nesting'. Here we've created nested-for-loops. This inner loop is where we need to put our delay, otherwise it would cycle through every segment so quickly it would appear as though they're all on at the same time. We have 9 segments and 4 digits, each being shown for 0.1 seconds, so it should take 3.6 seconds to complete the entire cycle.

### Code

```python
import board
from digitalio import DigitalInOut
import time

# Create DigitalInOut objects for each segment
A = DigitalInOut(board.GP10)
B = DigitalInOut(board.GP14)
C = DigitalInOut(board.GP5)
D = DigitalInOut(board.GP2)
E = DigitalInOut(board.GP4)
F = DigitalInOut(board.GP11)
G = DigitalInOut(board.GP13)
DP = DigitalInOut(board.GP3)   # Decimal Point
COL = DigitalInOut(board.GP12)  # Colon

# Create DigitalInOut objects for each digit
D1 = DigitalInOut(board.GP6)
D2 = DigitalInOut(board.GP7)
D3 = DigitalInOut(board.GP8)
D4 = DigitalInOut(board.GP9)

# Create a list containing all the segments
segments = [A,B,C,D,E,F,G,DP,COL]

# Create a list containing all the digits
digits = [D1,D2,D3,D4]

# Set all the segments as outputs
for seg in segments:
    seg.switch_to_output()
    
# Set all digits as outputs
for dig in digits:
    dig.switch_to_output()
    

while True:                 # Loop Forever:
    for dig in digits:          # Iterate through all digits:
        dig.value = True            # Activate the current digit
        for seg in segments:        # Iterate through all segments:
            seg.value = True            # Activate the current segment
            time.sleep(0.1)             # Do nothing for 0.1 seconds
            seg.value = False           # Deactivate the current segment
        dig.value = False           # Deactivate the current digit

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learn.breadstick.ca/breadstick/pico-slices/slice-2-stopwatch/coding-lessons/2-cycling-through-all-segments.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
