Close

About leds

A project log for Dice badge

Put a bit of colorful randomness on your clothes !

pierre-loup-mPierre-Loup M. 02/22/2023 at 22:250 Comments

My first thought was "we need seven dots if we want to display the numbers from 1 to 6 on a dice". When you wanna use a microcontroller that has only six pins available, and one being the reset (so that's five), you have to find a solution. I was ready to dive into charlyplexing, but I quickly realized that six of these dots were always in pair : we in fact need just four pins. one for the central dot for odd numbers, and three for the pairs used for even numbers. See the schematics on the previous log to see how it's routed.

I also wanted to have a smooth on and off dimming. That implies PWM, which is easy to do on a microcontroller when you have one or two leds. Usually you tie the led to an output driven by the uC. but the ATTiny only has three of its pins that can be driven in such a way. After a bit of thinking, I implemented a quite simple PWM for all leds at once. A timer is used with two of its interrupts :

It's probably a bit less efficient than driving the leds directly from the PWM pins, but all the leds are dimmable. The only drawback is they are not independent. That's not a big deal in that case.

Here is the code of the interrupts. As you can see it's straightforward :

ISR(TIMER1_OVF_vect){
	// Clearing all four leds)
	PORTB &= ~(0b1111);
	// overflow counter
	if(++overflowCounter >= queueOut->fade.duration){
		overflowCounter = 0;
		overflow = true;
	}
}

ISR(TIMER1_COMPA_vect){
	// Set all four leds to their predefined values
	if(OCR1A != 255) PORTB |= pinState;
} 

Discussions