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

Two-button Control


Did You Know? Two-button Control

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.  

  • Review the LED Lights: Intro to Lists’s Script and How It Works sections.
  • Think about how you used lists and indexes in Blink Sequencing.

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]

Your Turn

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.

  • Enter the buttons_make_leds_rotate script into the micro:bit Python Editor.
  • Set the project name field to buttons_make_leds_rotate, then click Save.
  • Click Send to micro:bit.
  • Verify that the on/off sequence is green-yellow-red-green-yellow-red… while the P9 pushbutton is held down.
  • Verify that the on/off sequence is red-yellow-green-red-yellow-green… while the P6 pushbutton is held down.

                                                    

 


Printer-friendly version
How it Works
Prev
Review and Practice
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2025 Parallax Learn • Built with GeneratePress