

#include <avr/io.h>
 
#define LED1 PB0                   
#define LED2 PB2                   
#define LED3 PB4                   
#define LED4 PB3                   

#define PhotoRes PB1

int a;
int b;
int c;
int d;
int x;
int y;

int PWMA;
int PWMB;
int PWMC;
int PWMD;


int fA;
int fB;
int fC;
int fD;


void LedAcount(void)   // 0 = 0%  255 = 100%
{

if (fA == 0) PWMA = PWMA - 1;
if (PWMA == 0) fA = 1;
if (fA == 1) PWMA = PWMA + 1;
if (PWMA > 700) fA = 0;

a = 0;

}

void LedBcount(void)   // 0 = 0%  255 = 100%
{

if (fB == 0) PWMB = PWMB - 1;
if (PWMB == 0) fB = 1;
if (fB == 1) PWMB = PWMB + 1;
if (PWMB > 800) fB = 0;
b = 0;

}

void LedCcount(void)   // 0 = 0%  255 = 100%
{

if (fC == 0) PWMC = PWMC - 1;
if (PWMC == 0) fC = 1;
if (fC == 1) PWMC = PWMC + 1;
if (PWMC > 900) fC = 0;

c = 0;

}

void LedDcount(void)   // 0 = 0%  255 = 100%
{

if (fD == 0) PWMD = PWMD - 1;
if (PWMD == 0) fD = 1;
if (fD == 1) PWMD = PWMD + 1;
if (PWMD > 900) fD = 0;

d = 0;

}



int main(void) {

DDRB |= (1 << LED1);               
DDRB |= (1 << LED2);              
DDRB |= (1 << LED3);              
DDRB |= (1 << LED4);   

DDRB &= ~(1<<PhotoRes);                

fA = 0 ;
fB = 0 ;
fC = 0 ;
fD = 0 ;


PWMA = 500;
PWMB = 1300;
PWMC = 700;
PWMD = 1000;


for (;;)                          
{

PORTB |= (1 << LED1);         
PORTB |= (1 << LED2);         
PORTB |= (1 << LED3);       
PORTB |= (1 << LED4);


if (bit_is_set(PINB, PhotoRes))  // Read SWT pin (if SWT pressed, do the loop one time)
 
    {
	for (y = 0 ; y < 5000; y++)
	{

PORTB |= (1 << LED1);         
PORTB |= (1 << LED2);         
PORTB |= (1 << LED3);       
PORTB |= (1 << LED4);



	for ( x = 0 ; x < 255 ; x++ )

	{

		if (PWMA < x ) PORTB &= ~(1 << LED1);  
		if (PWMB < x ) PORTB &= ~(1 << LED2);  
		if (PWMC < x ) PORTB &= ~(1 << LED3);  
		if (PWMD < x ) PORTB &= ~(1 << LED4);

  

		if (a > 900) LedAcount();
		a++;

		if (b > 500) LedBcount();
		b++;

		if (c > 750) LedCcount();
		c++;

		if (d > 600) LedDcount();
		d++;



	}}}
}
 
return 0;
}


 
 



