NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Project Help and Ideas » Twinkle Heart Random Code

April 29, 2013
by cacope
cacope's Avatar

Can someone help me understand the relationship between the duty cycle and the incrementor counter in the twinkle heart code. After reading the tutorial and examining the code, it appears the the duty cycle can be greater than 64 due to the brightness calculations. I was expecting the duty cycle to only count up to 64 than back to 2.

Base on the code, the greatest brightness can be approximately 320 and a maximum brightness valve can be 330. These number were based on RAND_MAX Is equal to 32767.

My end goal is to make a larger led array with rgb LEDs. i just need to better understand this code.

April 29, 2013
by pcbolt
pcbolt's Avatar

Hi cacope -

I think you're right, the duty cycle count can be greater than 64 by the calculations in the code. Since the duty array holds 8-bit integers, it can max out at 255. There are a bunch of casting operations in the code so maybe the "avr-gcc" compiler has different rules but at first glance I'd say 255 is the max. There is a line in the code that is commented out where the calculations are sent over the UART so it should be easy to see if the theory is correct.

May 09, 2013
by cacope
cacope's Avatar

After further review of the code, maximum value of the Duty is 32. This is because the brightness calculation is only done when the LighIt in value is 1. Therefore, the rand number has to be less than limit which is 3277.

Thanks for the advice

May 09, 2013
by pcbolt
pcbolt's Avatar

Good catch. (There is also the +10 on the next line.)

Post a Reply

Please log in to post a reply.

Did you know that a square wave sounds different than a sine wave? Learn more...