Skip to content
Parallax Learn

Parallax Learn

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence
          • Cybersecurity: Radio Data tutorialCybersecurity
          • cyber:bot + Python
          • cyber:bot + MakeCode
          • Boe-Bot Tutorial SeriesBoe-Bot
          • Arduino Shield-Bot
          • ActivityBot with C TutorialsActivityBot + C
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot + BlocklyProp
          • Scribbler 3 Tutorial SeriesScribbler 3
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education
          • Propeller C-Language BasicsPropeller C Basics
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit + C
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit + BlocklyProp
          • Badge WX Tutorial SeriesBadge WX
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • MakeCode
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses
  • Sense Pushbutton Presses

Sense Pushbutton Presses

Solutions

Questions

  1. Answer: pushbuttons
  2. Answer: The if statement is inside another if loop.

 

Exercises

  1. Answer: There will be an error and the script will not run if lines are not properly indented.
  2. Answer: The index value can be placed inside a loop with a + / – used to increase or decrease the index value.  Incrementally changing the index value will move through the list items assigned to the corresponding index values.
  3. Answer: The pushbutton states are read, 0 or 1, and that information is then passed to other components via write statements to dictate their behavior. Ex: read_digital and write_digital.

 

Projects

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)


Printer-friendly version
Review and Practice
Prev
Measure Pushbutton LED Control with an Oscilloscope
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024

© 2025 Parallax Learn • Built with GeneratePress