LogosCZ
Posts: 2
Joined: Tue Sep 04, 2018 11:09 am

Python clock

Thu Sep 20, 2018 7:17 pm

Hi all, I am trying to write program for driving WS2812 led strip as digital clock (7 diodes per digit). I am able to turn each led separately on and I am able to "print" digits on my testing digit.

But I am printing digits like idiot (simple counting example):

Code: Select all

while True:

	strip.setPixelColor(0, Color(0,0,0))
	strip.setPixelColor(1, Color(255,255,255))
	strip.setPixelColor(2, Color(255,255,0))
	strip.setPixelColor(3, Color(255,255,255))
	strip.setPixelColor(4, Color(255,255,0))
	strip.setPixelColor(5, Color(255,255,255))
	strip.setPixelColor(6, Color(255,255,0))
	strip.show()
	time.sleep(1)
	strip.setPixelColor(0, Color(0,0,0))
	strip.setPixelColor(1, Color(255,255,255))
	strip.setPixelColor(2, Color(0,0,0))
	strip.setPixelColor(3, Color(0,0,0))
	strip.setPixelColor(4, Color(0,0,0))
	strip.setPixelColor(5, Color(0,0,0))
	strip.setPixelColor(6, Color(255,255,0))
	strip.show()
	time.sleep(1)
and so on...

Can someone please help me to understand and create logic of this simple program? I wanna reach to "print" hours & minutes in 24h format on my strip. My idea is declare own function for diplaying numbers and add some logic before, but I am noob Python programes.

User avatar
paddyg
Posts: 2554
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Python clock

Sat Sep 22, 2018 7:18 pm

The standard way would be to put all of your pre-defined stuff at the beginning like

Code: Select all

WHITE = Color(255, 255, 255)
RED = Color(255, 0, 0)
GREEN = Color(0, 255, 0)
# etc, then
DIGITS = [(BLACK, WHITE, YELLOW, WHITE, YELLOW, WHITE, YELLOW),
	(RED, BLACK, GREEN...
	(WHITE, GREEN...
	)]
while True:
	for c, i in enumerate(DIGITS[1]): # presumably the actual digits chosen are set by some other process
		strip.setPixelColor(i, c)
	strip.show()
	time.sleep(1.0)
	# etc
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”