In this activity, you will:
If you completed all of Inside the Pushbutton, all you will have to do here is replace a black jumper wire with a red one.
(1) Jumper wire - red
If you completed the Inside the Pushbutton > Your Turn [1] section, the circuit you built will have to be modified for this activity. For those of you who partially completed or skipped that activity, you will either be adding three or four of the parts shown below.
The pushbutton_test_x2 script displays the states of both pushbuttons in the Serial Monitor.
# pushbutton_test_x2 from microbit import * state9 = 0 state6 = 0 display.off() pin9.set_pull(pin9.NO_PULL) pin6.set_pull(pin6.NO_PULL) while True: state9 = pin9.read_digital() state6 = pin6.read_digital() print("state9:", state9, " state6:", state6) sleep(250)
The pushbutton_test_x2 script started as pushbutton_test, and was adapted for two pushbuttons through the following changes:
The button_light_blink_x2 script is the two-button, two-light version of the button_light_blink script from the Build and Test a Pushbutton activity’s Your Turn section [3].
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.
1. Solution.
# rotate_leds_leds_off from microbit import * pin = [pin13, pin14, pin15] time = [200, 200, 200] index = 0 state9 = 0 state6 = 0 display.off() pin9.set_pull(pin9.NO_PULL) pin6.set_pull(pin6.NO_PULL) while True: state9 = pin9.read_digital() state6 = pin6.read_digital() if state9 is 1: index = index + 1 if index > 2: index = 0 pin[index].write_digital(1) sleep(time[index]) pin[index].write_digital(0) if state6 is 1: index = index - 1 if index < 0: index = 2 pin[index].write_digital(1) sleep(time[index]) pin[index].write_digital(0)
2. Solution.
#project_solution from microbit import * pin = [pin13, pin14, pin15] time = [1500, 1000, 500] index = 0 state9 = 0 state6 = 0 display.off() pin9.set_pull(pin9.NO_PULL) pin6.set_pull(pin6.NO_PULL) while True: state9 = pin9.read_digital() state6 = pin6.read_digital() if state9 is 1: index = index + 1 if index > 2: index = 0 pin[index].write_digital(1) sleep(time[index]) pin[index].write_digital(0) sleep(time[index]) if state6 is 1: index = index - 1 if index < 0: index = 2 pin[index].write_digital(1) sleep(time[index]) pin[index].write_digital(0) sleep(time[index])
3: Solution.
# buttons_with_leds_and_zip from microbit import * pin_list = [pin13, pin14, pin15] time_list = [500, 1000, 1500] state9 = 0 display.off() pin9.set_pull(pin9.NO_PULL) while True: state9 = pin9.read_digital() for pin, time in zip(pin_list, time_list): if state9 is 1: pin.write_digital(1) sleep(time) pin.write_digital(0) sleep(time)
Links
[1] https://learn.parallax.com/tutorials/language/python/sense-pushbutton-presses/inside-pushbutton/active-vs-resting-states
[2] https://python.microbit.org/v/2
[3] https://learn.parallax.com/tutorials/language/spin/sense-pushbutton-presses/build-and-test-pushbutton/input-register-and
[4] https://learn.parallax.com/tutorials/led-lights/intro-lists
[5] https://learn.parallax.com/tutorials/language/python/led-lights/intro-lists/how-it-works
[6] https://learn.parallax.com/tutorials/language/python/led-lights/blink-sequencing