Page 1 of 1

Python clock

Posted: Thu Sep 20, 2018 7:17 pm
by LogosCZ
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.

Re: Python clock

Posted: Sat Sep 22, 2018 7:18 pm
by paddyg
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