Permalink
Browse files

Simplify example code, add comments

  • Loading branch information...
1 parent 3add21e commit 74ef50424858fe8ea19a18b7f59a0c89f4dcddcd @mrichardson23 committed Jul 1, 2012
Showing with 32 additions and 23 deletions.
  1. +32 −23 example.py
View
@@ -1,28 +1,37 @@
#!/usr/bin/python
+
+# mrBBIO Example Code
+#
+# By Matt Richardson, July 1, 2012
+#
+# https://github.com/mrichardson23/mrBBIO
+#
+# Repeatedly reads if a button is pressed. when it's pressed, it will light
+# an LED for one second and print the time (in ms) since the script was
+# started.
+#
+# Circut:
+# * An LED is connected to pin 12 on header P8.
+# * A button is connected to pin 45 on header P8. Use a pull-down resistor
+# (around 10K ohms) between pin 45 and ground. 3.3v for the other side of
+# the button can be taken from pins 3 or 4 on header P9. Warning: Do not
+# allow 5V to go into the GPIO pins.
+
+# import the functions from the mrbbio module:
from mrbbio import *
-def setup():
- pinMode("P8.12", OUTPUT)
- pinMode("P8.14", OUTPUT)
- pinMode("P8.16", OUTPUT)
- pinMode("P8.18", OUTPUT)
- pinMode("P8.20", OUTPUT)
- pinMode("P8.45", INPUT)
+def setup(): # this function will run once, on startup
+ pinMode("P8.12", OUTPUT) # set up pin 12 on header P8 as an output
+ pinMode("P8.45", INPUT) # set up pin 45 on header P8 as an input
+
+def loop(): # this function will run repeatedly, until user hits CTRL+C
+ if (digitalRead("P8.45") == HIGH): # was the button pressed? (is 3.3v making it HIGH?) then do:
+ print "Button Pressed at: %d" % millis() # print to console, milliseconds since start up
+ digitalWrite("P8.12", HIGH) # set pin 12 high, illuminating the LED
+ delay(1000) # wait for 1 second
+ digitalWrite("P8.12", LOW) # set pin 12 low, turning the LED off
+ delay(10) # don't "peg" the processor checking pin
-def loop():
- if (digitalRead("P8.45") == HIGH):
- print "Button Pressed at: %d" % millis()
- digitalWrite("P8.12", HIGH)
- digitalWrite("P8.14", HIGH)
- digitalWrite("P8.16", HIGH)
- digitalWrite("P8.18", HIGH)
- digitalWrite("P8.20", HIGH)
- delay(1000)
- digitalWrite("P8.12", LOW)
- digitalWrite("P8.14", LOW)
- digitalWrite("P8.16", LOW)
- digitalWrite("P8.18", LOW)
- digitalWrite("P8.20", LOW)
- print "analog reading:" + analogRead("P9.33")
- delay(10) # don't peg the processor checking pin
+# the line below is required. It passes the functions above into a function that handles
+# the overall setup/loop structure of the script. It also handles termination gracefully.
run(setup, loop)

0 comments on commit 74ef504

Please sign in to comment.