LED Running Animation in C

 avatar
subratasarkar20
c_cpp
a month ago
1.2 kB
2
Indexable
Never
/*
 * runningLED2.cpp
 *
 * Created: 12-09-2024 12:59:06
 * Author : subrs
 */ 

#include <avr/io.h>
#include <util/delay.h>

int main (void)
{
	DDRB = 0xFF; 		// set all the pin from register B as i/p
	PORTB = 0x00; 		// clear register initially
	
	int bitPosition; 		// variable to count the bit/pin position
	
	int direction = 0; 		// variable to keep track of the LED's direction
	
	while (1)
	{
		if (direction == 0) 
		{
			for (bitPosition = 0; bitPosition < 8; bitPosition++) 		// first loop to turn on all the LEDs from PINBO to PINB7 
			{
				PORTB |= 1 << bitPosition; 			// to set the register pins
				_delay_ms(100);
				
				PORTB &= ~(1 << bitPosition); 		// to reset the register pins
				_delay_ms(100);
			}	
		}
		
		direction = 1;
		
		if (direction == 1)
		{
			for (bitPosition = 6; bitPosition > 0; bitPosition--) 			// second loop to turn off all the LEDs from PINB7 to PINB0 (ignoring PINB7 and last PINB0 in this case to avoid overlapping) 
			{
				PORTB |= 1 << bitPosition; 			// to set the register pins
				_delay_ms(100);
				
				PORTB &= ~(1 << bitPosition); 		// to reset the register pins
				_delay_ms(100);
			}
		}
		
		direction = 0;
	}
}


Leave a Comment