September 04, 2010 by sporkalicious So i wrote up a simple little "knight rider" code after doing the Led blink guide, and though successful it raised a few questions, first here's my code ``````#define F_CPU 14745600 #include #include #include "../libnerdkits/delay.h" #include "../libnerdkits/lcd.h" // PIN DEFINITIONS: // // PC4 -- LED anode int main() { // LED as output DDRC |= (1< 1; x -= x/2) // Decrement x by power of 2 { PORTC = x; // set the port to the current value delay_ms(150); } } `````` In your code, there would have been a few problems but you are definately on the right track. 1st, in C, you have to declare each variable with a type Instead of ``````x=0 y=0 `````` You would need something like ``````uint8_t x=0; uint8_t y=0; (start a loop) // turn on LED X `````` Here you have the right idea, but you don't need the PC"X" just x. Such as.. ``````PORTC |= (1< #include #include "../libnerdkits/delay.h" #include "../libnerdkits/lcd.h" int main() { uint8_t x=0; // define Current working led number uint8_t y=0; // define directional variable DDRC =0xFF; // set port C to output while(1) { // loop keeps looking forever PORTC |= (1< 1; x -= x/2) `````` this one really throws me off though, i understand that once x=128 it does this loop instead which would halve x intil it is equalt or less than 1, what i really don't understand is the halving operation itself. theoreticly though applying this to my project should follow along the lines of ``````uint8_t x=0; DDRC = 0xFF; for(;;) { for(x=0;x<5;x+=1) { PORTC = x; delay_ms(150); } for(x=5;x>0;x-=1) { PORTC = x; delay_ms(150); } } `````` Please explain/correct me if I'm wrong, and thanks again for your help. okay so after trying my attempt I was abit confused by the results, until i realized i had made a binary counter similar to one of the binary led clocks.. now i think i understand a bit more.. modified your code to create the following(128 would cycle 8 leds i'm only using 6) ``````for(;;) { for(x=1;x<32;x*=2) { PORTC = x; delay_ms(150); } for(x=32;x>1;x-=x/2) { PORTC = x; delay_ms(150); } } `````` what i still don't understand is the "-" before the equals sign in the second for statement, Having way too much fun with this now.. should be asleep.. lol Hi sporkalicious, The - sign is part of the -= operator which works just like the much more common += operator. These operators are just shorthand that first apply to operator to the variable, and then set the operator equal to the result. So doing ``````x += 2 `````` is the same as doing ``````x = x + 2 `````` In this case ``````x -= x/2 `````` would expand to ``````x = x - x/2 `````` For this particular example, it turns out that the - is completely irrelevant, since the right side of the equation simplifies to x/2. In other words subtracting half from a number is the same as just dividing the number by two. Hope that makes sense! Humberto I would also like to toss a thank you out to Rich. That example was well explained and detailed line by line. sporkalicious: I do the same thing you do. I won't use a block of code I don't understand. Unfortunately, I don't know C at all so I'm starting from ground zero. If you come across any C examples for the micro-controller like Rich posted, please post them here. There are some good ones in the manual but I need more details sometimes. Thanks. (sporkalicious: if you don't mind, i'm going to steal your code and try to understand it. :)