A pair of pushbuttons can be used to control whether a variable counts up or down. Take a look at the while True loop below:
while True: state9 = pin9.read_digital() state6 = pin6.read_digital() if state9 is 1: index = index + 1 if index > 2: index = 0 if state6 is 1: index = index - 1 if index < 0: index = 2
Each if statement also has a nested if statement that restarts the count when it reaches a certain point. When up-counting, when the index variable reaches 3, it gets changed back to 0. When down-counting, when index reaches -1, it gets changed back to 2.
If you guessed that counting up or down might work with lists of pins and times, you are right! In the next Your Turn section, that index value will be used to access the 0, 1, and 2 elements in these pin and time lists (below). When counting up, the index variable will be used to fetch pin13, pin14, and pin15, then repeat again starting with pin13. When counting down, the index variable will be used to fetch pin15, pin14, pin13, pin15, pin14, pin13, and so on.
pin = [pin13, pin14, pin15] time = [200, 200, 200]
With this application, you can hold the P9 pushbutton to make the lights turn on and off in a repeating green-yellow-red pattern. If you instead hold down the P6 pushbutton, the lights turn on and off in a repeating red-yellow-green pattern.
Links
[1] https://learn.parallax.com/tutorials/led-lights/intro-lists
[2] https://learn.parallax.com/tutorials/language/python/led-lights/intro-lists/how-it-works
[3] https://learn.parallax.com/tutorials/language/python/led-lights/blink-sequencing
[4] https://python.microbit.org/v/2