PLEASE NOTE: This site is no longer being actively maintained. For frequently updated BeagleBone resources please visit Beaglebone.org.

Wednesday, May 21, 2014

On 10:24 AM by Anonymous


Proto Cape for workshop and tutorials

Overview

Having a hard time deciding what to do?
This simple decision maker project will make it easier for you to decide.  Three LEDs blink rapidly (thinking). Once you press the button, one-of-three LEDs will be ON. Red=No, Yellow=Maybe, Green=Go. Shall we get started?

What you will need


These components are included in the parts kit (PTK-027)
  • (1) Green LED
  • (1) Red LED
  • (1) Yellow LED (use RGY Tri-color LED )
  • (1) Push-button
  • (4) Resistors 330 Ohms

Instructions

Step 1

Wire the circuit as shown in the diagram. (Or make a permanent version with a proto cape.)

RGY LED and Push-button wiring

Step 2

Project Software Program

Download rand_led.py File

Program Listing

#!/usr/bin/python
#Author : Richard St-Pierre
#
#
import Adafruit_BBIO.GPIO as GPIO
import time
import random

PB_pin  = "P8_26"  # has internal pull-up !
RED_led = "P9_14"
YEL_led = "P9_16"
GRN_led = "P8_19"

GPIO.setup (RED_led, GPIO.OUT)
GPIO.setup (YEL_led, GPIO.OUT)
GPIO.setup (GRN_led, GPIO.OUT)
GPIO.setup (PB_pin,  GPIO.IN)

print "BBB LED DECISION MAKER"
print "======================"
print "RED    =    N O       "
print "YELLOW =   MAYBE      " 
print "GREEN  =    YES       "
print "  "
print "Press Ctrl-c to exit  "

def setLed ( num ):
       if (num == 1):
          GPIO.output(RED_led, GPIO.HIGH)
          GPIO.output(YEL_led, GPIO.LOW)
          GPIO.output(GRN_led, GPIO.LOW)
       elif (num == 2):
          GPIO.output(RED_led, GPIO.LOW)
          GPIO.output(YEL_led, GPIO.HIGH)
          GPIO.output(GRN_led, GPIO.LOW)
       elif (num == 3):
          GPIO.output(RED_led, GPIO.LOW)
          GPIO.output(YEL_led, GPIO.LOW)
          GPIO.output(GRN_led, GPIO.HIGH)
       else:
          GPIO.output(RED_led, GPIO.LOW)
          GPIO.output(YEL_led, GPIO.LOW)
          GPIO.output(GRN_led, GPIO.LOW)



setLed(1)
time.sleep(0.5)
setLed(2)
time.sleep(0.5)
setLed(3)
time.sleep(0.5)
setLed(0)

while (True):


  while (GPIO.input(PB_pin) == 1):
    setLed(random.randint(1,3))
    time.sleep(0.01)

  while (GPIO.input(PB_pin) ==0):
     time.sleep(0.5)

Step 3

Python: Command line Mode

Python is pre-loaded with the BeagleBone Linux Distribution. To start python, simply open a terminal session and enter "python". Commands are interpreted as they are entered. Control-C will exit Python command mode.

To start the program


 python rand_led.py


When the program starts each LED is turned ON in sequence to test the LEDs. Then a fast flashing rate starts until the button is pressed. With the button held down only one LED is ON, corresponding to the decision.

To exit the program


 Ctrl-c