01-01-2014, 03:05 AM
I got my beaglebone black a few days ago, and I finally wrote a program for it. It is very ugly and I have no idea why it works, but it does.
When you start it, it asks you about how much LEDs you have connected (which I should rewrite, because its the GPIO pin that the program ends on) and the GPIO pin that the first LED is on. Then it turns on the LEDs between those two pins in order, then turns them off in order, etc. until you press a button to stop it.
Here is the code:
As you can see, the code sucks. I really hate that list of globals I have and really want to fix that. I already have noticed I have some unnecessary lines and operations, and I think there are some more.
Also, don't know what the beaglebone black is? LOOK IT UP.
When you start it, it asks you about how much LEDs you have connected (which I should rewrite, because its the GPIO pin that the program ends on) and the GPIO pin that the first LED is on. Then it turns on the LEDs between those two pins in order, then turns them off in order, etc. until you press a button to stop it.
Here is the code:
Code:
import Adafruit_BBIO.GPIO as GPIO
import time
leds = int(raw_input("LEDs (starting LED + this): "))
ledStart = int(raw_input("Starting LED: "))
if ledStart < 10: #Don't you dare choose a value less than 10
print "Error! Starting position less than 10."
exit()
curLed = ledStart
curState = GPIO.HIGH
step = 0
GPIO.setup("P8_9", GPIO.IN) #Setup the button
GPIO.add_event_detect("P8_9", GPIO.FALLING)
def swapstate(c): #Swap states
if c == GPIO.HIGH:
c = GPIO.LOW
elif c == GPIO.LOW:
c = GPIO.HIGH
else:
c = "This is a horrible way to pass an error"
return c
def reset(): #infitely ugly reset code
global step
global leds
global curState
global curLed
global ledStart
if step == leds:
step = 0
curLed = ledStart
curState = swapstate(curState)
else:
curLed = ledStart + step
while step < leds: #Setup all LEDS
curLed = ledStart + step
GPIO.setup("P8_" + str(curLed), GPIO.OUT)
step += 1
curLed = ledStart #Prepare for next set of code
step = 0
while True: #Da thing that actually controlls the LEDS
if GPIO.event_detected("P8_9"): #Shutdown mechanism
exit()
GPIO.output("P8_" + str(curLed), curState) #Control LEDS
step += 1
curLed = ledStart + step
reset()
time.sleep(0.5)
As you can see, the code sucks. I really hate that list of globals I have and really want to fix that. I already have noticed I have some unnecessary lines and operations, and I think there are some more.
Also, don't know what the beaglebone black is? LOOK IT UP.