Switch LED off in Python

Forums:

The helpful examples in Python show loops that run forever.
I want to make a sequence that sets the LED to a random colour 10 times, waits 2 seconds and then turns off the LED.
The loop and the change of colours are no problem.
When the code has exited the loop, the LED stays on at the last random colour selected.
I have tried LedBorg.write('000') to no avail, also creating a colour called off with value "ooo" and using LEDBorg.write(off) but the LED stays on at the last random colour in the loop, The code exits the loop correctly. What am I doing wrong pls?

piborg's picture

The sequence you describe sounds like it should work fine, would you mind posting the code you have written so we can see it?

Kyle's picture

Hi,
Try making sure you import os at the start and adding this code after your loop.

# Import random, time and os
import random, time, os

# After loop has exited wait 2 seconds and switch LED to black
time.sleep(2)
os.system('echo "000" > /dev/ledborg')

Hope this helps.

Very helpful, thanks.
Here is my code listing - not elegant, just playing around. I have shown what works and what does not.

import time, random, sys, os

colors = ['100','010','001','011','021']
off = "000"

n = 0
while n < 10:
#All lines in the loop are indented - not shown clearly below but they are!
    print(n)
    x = random.randint(0,4)
    mycolour = colors[x]
    LedBorg = open('/dev/ledborg', 'w')
    LedBorg.write(mycolour)
    LedBorg.close()
    time.sleep(1)
    n +=1

print('Finished sequence, now turning LedBorg off...')
#These lines don't work
LedBorg = open('/dev/ledborg', 'w')
LedBorg.write(off)


# But courtesy of the forum, this does
os.system('echo "000" > /dev/ledborg')
time.sleep(5)

os._exit(0)
piborg's picture

Glad to hear you have a solution that works, I think the original Python should be

print('Finished sequence, now turning LedBorg off...')
LedBorg = open('/dev/ledborg', 'w')
LedBorg.write(off)
LedBorg.close()

Without the close Python is holding on to the data and not actually writing it to the file (it normally buffers until it sees a newline character, '\n')

Kyle's picture

I am also very new to python but here is my adaptation of the random script (any pointers welcomed)

#! /usr/bin/env python
# File name = rand_led.py
# LED random colours ------ By Cathal Stewart Jan 2013
# LED board from ----- http://www.piborg.org/ledborg


import random, time, os, sys




print "----------------WELCOME-----------------"
print "----------------------------------------"
print "****************************************"
print "----------------------------------------"
print "Random LED Sequence by Cathal Stewart"
print "----------------------------------------"
print "http://cathalstewart.co.uk"
print "----------------------------------------"

repeat = int(raw_input("How many times shall I flash the LED's? "))
print "----------------------------------------"
print "****************************************"
print "----------------------------------------"
speed = float(raw_input("How fast should I flash the LED's? "))

total_time = repeat * speed
total_min = total_time / 60

count = 1


while (count <= repeat):
	
	
	
    red = random.randrange(0,3)
    green = random.randrange(0,3)
    blue = random.randrange(0,3)

    led_colour = str(red)+str(green)+str(blue)

    ledborg = open('/dev/ledborg', 'w')
    ledborg.write(led_colour)
    del ledborg

    print "Colour code = (%s) ----- And the Count is = (%s)" % (led_colour, count)
    time.sleep(speed)
    count += 1

os.system('echo "000" > /dev/ledborg')

print "----------------------------------------"
print "****************************************"
print "----------------------------------------"    
print "You just saw (%s) random led colours at an interval speed of (%s) seconds!" % (repeat, speed)
print "Which took (%s) Seconds or (%s) Minutes to execute!" % (total_time, total_min)
print "----------------------------------------"
print "****************************************"
print "LED's from -- http://www.piborg.org/ledborg"
print "----------------------------------------"

y =raw_input("Would you like to run this program again? (y/n) ")
if y == 'y':
	execfile("rand_led.py")
else:
	print "----------------------------------------"
	print "****************************************"
	print "----------------------------------------"
	print "OK, hope you had fun, Goodbye!"
	sys.exit()
Subscribe to Comments for &quot;Switch LED off in Python&quot;