Search code examples
cmicrocontrolleravravr-gccatmega

unable to hold some LED'S : ON , when flickering others


I am using AVR as the micro controller and ATMEGA8 as the processor (inside the micrcontroller). The board that has micro controller has 4 LEDS. I am able to burn the program and light up the LEDS. But I am unable to achieve a particular thing.

L1 L2  L3 L4

These are 4 LEDS. In the first round each LED lights up after a gap of 3 seconds.The last LED (L4) keeps lighting after the first round.As the third round starts each LED lights at a gap of 3 seconds and L3 keeps lighting when L4 is also lighting and it goes on....till L1.

L1 L2 L3 L4
         On
      On On
   On On On
On On On On

But I am unable to achieve this. Because as i set one LED ON other gets OFF. I even tried adding a small time gap as 10 milliseconds. How do I do this ? Here is what I have till now :

    #include<avr/io.h>
    #include<util/delay.h>
    DDRB = 0xFF; // input
 //PORTB = 0xFF;

    // ob00011110 --> on all --> binary

    int i=0;

    while(i<1) {
      PORTB = 0b00010000; // first led on
      _delay_ms(3000);
      PORTB = 0b00001000; // second led on
      _delay_ms(3000);
      PORTB = 0b00000100; // third on
      _delay_ms(3000);
      PORTB = 0b00000010; // fourth on
      _delay_ms(3000);
      i += 1;
    }

    PORTB = 0b00000010; // keep the 4th on and start all over again and reach til 3rd LED

Solution

  • It looks like your sequence is wrong. When you turn the 2nd LED on you are turning the first off. The sequence should be:

      PORTB = 0b00010000; // first led only
      _delay_ms(3000);
      PORTB = 0b00011000; // first and second led on
      _delay_ms(3000);
      PORTB = 0b00011100; // first, second, and third on
      _delay_ms(3000);
      PORTB = 0b00011110; // first, second, third, and fourth on
      _delay_ms(3000);