////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// PIC LIGHT CONTROLLER
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// controll 3 lights via the RS232 port
//// RS232 settings : 9600,8,N,1
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////	command		description
////
////	1		LIGHT1		-	increases brightness of lights (depends on mode setting a or r)
////	2		LIGHT2
////	3		LIGHT3
////
//// 	a		absolute mode	-	each hit turns on light to 100%
////	r (default)	relative mode	-	each hit increases light by 20% or time when max is reached
////
////	f		1						(2 s)
////	s 		10		-	time of main loop	(20 s)
////	m(default)	100						(3 min)
////	l		2000						(66 min)
////	u		5000						(166 min)
////	x		15000						(500 min)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include <16F84.H>
#fuses xt,nowdt,noprotect
#use delay(clock=4000000)

#use rs232(baud=9600 ,xmit=PIN_A1,rcv=PIN_A0)	

void main() {
	char x;
	int j, flag;
	long int k, i, max;
	long int l1=100;
	long int l2=100;
	long int l3=100;
	
    // bootcode
	output_high(PIN_B0);
	output_high(PIN_B1);
	output_high(PIN_B2);
	delay_ms(500);
	output_low(PIN_B0);
	output_low(PIN_B1);
	output_low(PIN_B2);
	delay_ms(500);

printf("\r\n START 1 : \r\n");
   	
flag=1;
max = 100;

while (TRUE) {
	for (i=1;i<=max;i++){
		
		output_high(PIN_B0);
		output_high(PIN_B1);
		output_high(PIN_B2);	

		for (j=1;j<=100;j++){

			if (j>=l1)	output_low(PIN_B0);
			if (j>=l2)	output_low(PIN_B1);
			if (j>=l3)	output_low(PIN_B2);

			for(k=1;k<=10;k++){
				if ( kbhit() ){
					x = getc();
					putc(x);

					// absolute mode - each hit puts it on
					if (x=='a') flag=0;
					// relative mode each hit increases light and time
					if (x=='r') flag=1;

					// time of the loop
					if (x=='l') max=2000;
					if (x=='u') max=5000;
					if (x=='x') max=15000;
					if (x=='f') max=1;
					if (x=='s') max=10;
					if (x=='m') max=100; 		//default

					if (flag==0) {			//absolute mode
						if (x=='1') l1=100;
						if (x=='2') l2=100;
						if (x=='3') l3=100;
					} 
					if (flag==1) {			//relative mode
						if ((x=='1') && (l1 <500)) l1=l1+20;
						if ((x=='2') && (l1 <500)) l2=l2+20;
						if ((x=='3') && (l1 <500)) l3=l3+20;
					}
				}
			}

		}

	}

	if  (0<l1)  l1--;
	if  (0<l2)  l2--;
	if  (0<l3)  l3--;

	//printf("l1 %i l2 %i l3 %i x %c",l1,l2,l3,x);

}//while(true)
}//main
