### You are not logged in. [log in]

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

Microcontroller Programming » knight rider LED animation

 December 01, 2010 by archee Hi, As my first project I wanted to make my 6 leds work like those on KITT car from "Knight Rider" And I sucsesfuly made all these six leds work like those on KITT(runing forward and backwords) only modifying code from "led_blink.c" But in my opinion my code is too long, I know it can be modified and made like two times shorter. ``````// PIN DEFINITIONS: // // PC4 -- LED anode int main() { // LED as output DDRC |= (1<>= 1; } delay_ms(50); // pause at PC0 `````` because the original code had it sitting at PC0 and PC5 for two times 50ms. I find the following approach to be the most self-explanatory: ``````uint8_t pattern[12] PROGMEM = {PC0, PC1, PC2, PC3, PC4, PC5, PC5, PC4, PC3, PC2, PC1, PC0}; for (i = 0; i < 12; i++) { PORTC = 1 << pgm_read_byte(&pattern[i]); delay_ms(50); } `````` This approach also lets you easily define more complex patterns. @exussum: for(i=1;i<7;i++){ PORTC = PORTC<<1; delay_ms(50); } I dont uderstand how this will be working... What I see is that PORTC will be set to 0000001 for 7 times..? in my mind it looks like --> PORTC = 1; and this line done 7 times. Your offered final code should look like this: ``````while(1) { PORTC = 1; delay_ms(50); int i; for(i=1;i<4;i++) { PORTC *= 2; delay_ms(50); } //reverse countdown for(i=1;i<5;i++) { PORTC /= 2; delay_ms(50); } } `````` @bretm: I`m not woried about forgetting what each line means in your code, I`m woried that it will decrese battery`s life due to many calculations it have to do all the time Thanks for answering to my 3 questions @ archee "I dont uderstand how this will be working... What I see is that PORTC will be set to 0000001 for 7 times..?" If PORTC=1 (00000001) then PORTC bitshifted one value left is 00000010 I didnt know you could do <<= Learn something new all the time. Bitshift 00000010 one to the left is 00000100. The way in nerdkits code is usually 1< #include #include "../libnerdkits/delay.h" #include "../libnerdkits/lcd.h" // PIN DEFINITIONS: // // PC4 and PC5 -- LED anode int main() { // LED as output int count = 0; int fore = 1; int maxLEDs = 2; DDRC = 63; PORTC |= (1<>1; if(count == 0){ fore = 1; } } } return 0; } `````` I have a lot of questions about some of the parameters but for fear of hijacking this thread, I'll make a new post. -Makoto DDRC = 63; You dont want that many outputs, And really it should be ``````DDRC |= 7 `````` 7 in binary is 00000111 so only the ones you want are outputs. But the rest seems fine :) there is so many solutions for this one little animation, but how to know which solution is the best for the job.. ? My personal favorite is the one where all otput port adresses are stored in array... In my head it should be the best solution. @:Makoto DDRC=63; was for my six leds 63 -> 0111111 Ok, I am starting to get a clearer picture now, this is good. Thank you exussum and archee for the explanations. Its funny, my initial intention for the LED array was to generate a Knight Rider light setup with several different patterns that it could cycle thru, I guess most nerds think alike. :) I will be trying to create a 3x40 grid for my project and incorporate the brightness action applied in the valentines demo. -Makoto the "best" solution taking everything in to consideration would be a timer based interrupt firing every 50 ms doing either the array style one or a bitshift. I highly doubt theres much performance difference between the two

## Post a Reply

Please log in to post a reply.

 Did you know that you can generate hundreds of volts AC from your microcontroller with a little bit of circuitry? Learn more...