August 09, 2009 by Nerdful_com This code will chase a light back and forth with 6 LEDs that came with your nerdkit. ``````/* www.nerdful.com - I get called a nerd now and then, but I am only a bit nerdful... */ #include #include void sleep(uint8_t millisec) { while(millisec) { delay_ms(1); millisec--; } } main() { DDRC |=1< #include main() { //DDRC = 3; DDRC = 0b0000111; // enable pins 23-25 while(1) { PORTC = 0b001; delay_ms(100); PORTC = 0b010; delay_ms(100); PORTC = 0b100; delay_ms(100); } } `````` It's not that you're original method was 'the hard way' that method it excellent for dealing with individual pins, without changing the others. But for this type of example it's useful to see how to make the code, a little simpler (although initially it may look more confusing). Using binary notation it helpful when learning as you can visualize what you are turning 'on'. Next we can look at using decimal and hex notation, to make the code shorter and hence less coding for us!! Nerdful_com, that's a nice idea for a simple project! I rewrote your code to illustrate a few common programming paradigms; it's shorter now, and if you try to figure out how it works, you'll learn quite a bit. Cheers, Zoran ``````#include #include /* This can go up to 8, but not further! */ #define NUMBER_OF_LEDS 6 main() { DDRC |= (1 << NUMBER_OF_LEDS) - 1; // for 6 LEDs: 00100000-1 = 00011111 (5 lowest bits on) PORTC &= ~((1 << NUMBER_OF_LEDS) - 1); // ditto int light_location = 0; // LED 0 will be lit initially int light_direction = +1; // next LED is the one to the right while(1) { PORTC |= (1 << light_location); // LED on delay_ms(50); // ... sleep PORTC &= ~(1 << light_location); // LED off light_location += light_direction; // determine the next LED if (light_location < 0) { // LED hit "left edge" while traveling left? light_location = 1; // bounce LED to 2nd from the left light_direction = +1; // and set direction to right } else if (light_location == NUMBER_OF_LEDS) { // hit "right edge"? light_location = NUMBER_OF_LEDS - 1; // bounce LED to 2nd from the right light_direction = -1; // and set direction to left } } } `````` Ok I'll have a bash - not as elegant as wayward's, but works : ``````#include #include "../libnerdkits/delay.h" #define F_CPU 14745600 // 6 led chaser - PC0-5 main() { int i; DDRC = 0x3F; while(1) { for (i=0; i <= 5; i++) { PORTC = (1 << i); // so PORTC = 0,1,2,4 delay_ms(50); } for (i=5; i >= 0; i--) { PORTC = (1 << i); // so PORTC = 0,1,2,4 delay_ms(50); } } `````` } Thanks guys, assume codes. I love having various ways to do the same thing. Obviously my way is the hard line by line way and not as small, much less typing with these other codes. I've been playing around with various code examples, trying to learn other methods, so for completeness heres another example of the same type of program, using a slightly different method. The idea behind this method originally came from a discussion on the IRC channel about strobing a number of digits on a display, since I didn't understand it, I thought this would be a simple way to help me learn (and I think it worked). The challenge I set myself was to use a ternary operation. So here it is : ``````#define F_CPU 14745600 #include #include "../libnerdkits/delay.h" #define NUM_OF_LEDS 6 #define SPEED 50 main() { int state=1; // chase state 1=up 0=down DDRC |= (1<>1; state = (PORTC==(1< #include uint8_t shift(uint8_t in, uint8_t d){ if(!d){ return (in>>1); }else { return (in<<1); } } int main(){ DDRC = 0b00111111; PORTC = 0; int in = 0; PORTC |= (1< #include "../libnerdkits/delay.h" #define NUM_OF_LEDS 6 #define SPEED 50 main() { int state=1; DDRC |= (1<>1; state = (PORTC==(1< #include void sleep(uint8_t millisec) { while(millisec) { delay_ms(1); millisec--; } } main() { DDRC |=1<make make -C ../libnerdkits make[1]: Entering directory `C:/Users/Jeramy/Desktop/nerdkits/Code/Code/libnerdkits' make[1]: Nothing to be done for `all'. make[1]: Leaving directory `C:/Users/Jeramy/Desktop/nerdkits/Code/Code/libnerdkits' avr-gcc -g -Os -Wall -mmcu=atmega168 -Wl,-u,vfprintf -lprintf_flt -Wl,-u,vfscan f -lscanf_flt -lm -o led_chase.o led_chase.c ../libnerdkits/delay.o ../libnerdki ts/lcd.o ../libnerdkits/uart.o In file included from led_chase.c:4: c:/winavr-20090313/lib/gcc/../../avr/include/avr/delay.h:36:2: warning: #warning "This file has been moved to ." led_chase.c: In function 'sleep': led_chase.c:9: warning: implicit declaration of function 'delay_ms' led_chase.c: At top level: led_chase.c:15: warning: return type defaults to 'int' led_chase.c:35:14: error: invalid suffix "PC3" on integer constant make: *** [led_chase.hex] Error 1 `````` And then it stops. Any help will be appreciated. THANKS! Looks like you moved your code 1 level too low in your folder structure. The Nerdikits folder libnerdkits needs to be one level above where your program code resides to find the nerdkits libraries. Rick Thank you! Fixed and works