/*
 * DP_TaN.c
 *
 * Created: 27.2.2016 14:56:37
 *  Author: Honza
 */ 

#include <avr/io.h>
#include <math.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#define F_CPU 11059200UL
#include <util/delay.h>
#include <compat/deprecated.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//ADC
unsigned int dataDetektor1 = 0;
unsigned int dataDetektor2 = 0;
char detektor = 0;
//displej
char input[15]; //buffer pro prijem souradnic po kliku na displej
int ind=0; //index ukayujici do bufferu input[15]
int x=0; //x-ova souradnice kliku
int y=0; //y-ova souradnice kliku
char stav =0; //stav ve kterzm se nachazi automat
char MenuToNast = 1; //ukayatel odkud pristupujem do nastaveni
char N1ToKl = 1; //ukazatel odkud pristupujem do klavesnice 
char nastaveno = 0; //ochrano proti pusteni bez nastaveny metody mereni
char hodnota[26] = "STR 590 300 30           \n"; //retezec se kerym pracuje klavesnice
int iHodnota =15; //index do retezce hodnota[26]
char kdoChceHodnotu=0; //ivformacni promenna pro klavesnici aby se po kliku na OK vedelo komu priradit zadanou hodnotu z klavesnice
char hodnotaMez[26] = "STR 410 195 30           \n";//hodnota meze ve formatu k odeslani na displej
char hodnotaCas[26] = "STR 410 306 30           \n";//hodnota casove konstantz ve formatu k odeslani na displej
int Hmez = 0; //hodnota meze
int Cas = 1; //hodnota casove konstanty
char hodnotaDO1N[17] = "STR 375 156 30 0\n";//hodnota DO1 ve formatu k odeslani na displej do nastaveni
char hodnotaDO1M[17] = "STR 356 270 30 0\n";//hodnota DO1 ve formatu k odeslani na displej do mereni
char HDO1 = 0; //hodnota DO1
char hodnotaDO2N[17] = "STR 375 218 30 0\n"; //hodnota DO2 ve formatu k odeslani na displej do nastaveni
char hodnotaDO2M[17] = "STR 356 317 30 0\n"; //hodnota DO2 ve formatu k odeslani na displej do mereni
char HDO2 = 0; //hodnota DO2
char hodnotaAO1N[26] = "STR 375 280 30 4         \n"; //hodnota AO1 ve formatu k odeslani na displej do nastaveni
char hodnotaAO1M[26] = "STR 356 364 30 4         \n"; //hodnota AO1 ve formatu k odeslani na displej do mereni
char HAO1 = 15; //hodnota AO1
char hodnotaAO2N[26] = "STR 375 342 30 4         \n"; //hodnota AO2 ve formatu k odeslani na displej do nastaveni
char hodnotaAO2M[26] = "STR 356 411 30 4         \n"; //hodnota AO2 ve formatu k odeslani na displej do mereni
char HAO2 = 15; //hodnota AO2
char metoda =0; //identifikator zvolene metody mereni
char DO1 = 0; //urcuje zda se pri mereni bude pracovat s DO1
char DO2 = 0; //urcuje zda se pri mereni bude pracovat s DO2
char AO1 = 0; //urcuje zda se pri mereni bude pracovat s AO1
char AO2 = 0; //urcuje zda se pri mereni bude pracovat s AO2
char cCommand =0; //pocitadlo odeslanych prikazu na displej
char rad = 1; //pomocna promenna pro funkci decode urcuje rad kterzm se nasobi jendotlive znak pro prevod z retezce do INT
//urcovani hodnoty OCR registru pro PWM AOn [1..2]
char a=0;//cela cast cisla
char b=0;//desetinna cast cisla 
char c=0;//carry
//Nano Socket LAN
char ethernet =0;
char ethernetCommand [50] = "------------------------------";
int indE=0;
char listen_socket=0;
char active_socket [3] = "000";
char t = 0;
char UART =0;
char pom=0;
//AT+i na dotazy klienta
char NENI_NASTAVENO[38] = "AT+iSSND%:000,21:STAV=NENI NASTAVENO\r\n";
char MERENI_STOP[35] = "AT+iSSND%:000,18:STAV=MERENI STOP\r\n";
char NASTAVENI1[34] = "AT+iSSND%:000,17:STAV=NASTAVENI1\r\n";
char LOAD_SCREEN[35] = "AT+iSSND%:000,18:STAV=LOAD SCREEN\r\n";
char MERENI_RUN[34] = "AT+iSSND%:000,17:STAV=MERENI RUN\r\n";
char TURBIDIMETR[35] = "AT+iSSND%:000,18:STAV=TURBIDIMETR\r\n";
char NEFELOMETR[34] = "AT+iSSND%:000,17:STAV=NEFELOMETR\r\n";
char POMEROVY[32] = "AT+iSSND%:000,15:STAV=POMEROVY\r\n";
char HODNOTA_MEZ[41] = "AT+iSSND%:000,24:HODNOTA_MEZ=          \r\n";
char HODNOTA_CAS[41] = "AT+iSSND%:000,24:HODNOTA_CAS=          \r\n";
char NASTAVENI2[34] = "AT+iSSND%:000,17:STAV=NASTAVENI2\r\n";
char STAV_DO1_A[29] = "AT+iSSND%:000,12:STAV_DO1=A\r\n";
char STAV_DO1_P[29] = "AT+iSSND%:000,12:STAV_DO1=P\r\n";
char STAV_DO2_A[29] = "AT+iSSND%:000,12:STAV_DO2=A\r\n";
char STAV_DO2_P[29] = "AT+iSSND%:000,12:STAV_DO2=P\r\n";
char STAV_AO1_A[29] = "AT+iSSND%:000,12:STAV_AO1=A\r\n";
char STAV_AO1_P[29] = "AT+iSSND%:000,12:STAV_AO1=P\r\n";
char STAV_AO2_A[29] = "AT+iSSND%:000,12:STAV_AO2=A\r\n";
char STAV_AO2_P[29] = "AT+iSSND%:000,12:STAV_AO2=P\r\n";
char HODNOTA_DO1[32] = "AT+iSSND%:000,15:HODNOTA_DO1= \r\n";
char HODNOTA_DO2[32] = "AT+iSSND%:000,15:HODNOTA_DO2= \r\n";
char HODNOTA_AO1[41] = "AT+iSSND%:000,24:HODNOTA_AO1=          \r\n";
char HODNOTA_AO2[41] = "AT+iSSND%:000,24:HODNOTA_AO2=          \r\n";
char HODNOTY_ALL[90] = "AT+iSSND%:000,73:DO1= ,DO2= ,AO1=          ,AO2=          ,MEZ=          ,KAL=          \r\n";
char NYNI_NELZE[49] = "AT+iSSND%:000,32:NYNI NELZE TENTO PRIKAZ POUZIT\r\n";
char NEZNAMY_PRIKAZ[33] = "AT+iSSND%:000,16:NEZNAMY PRIKAZ\r\n";
char ROZSAH1[36] = "AT+iSSND%:000,19:ROZSAH=25-200 NTU\r\n";
char ROZSAH2[37] = "AT+iSSND%:000,20:ROZSAH=200-500 NTU\r\n";
int data[600];
int idata =0;
unsigned int prumer =0;
char MR=0;
char rozsah = 1;
//__________________________________________________________________________________________
void uart_putchar(char c, FILE *stream) {
	
	//odeslani znaku
	if (c == '\n') {
		uart_putchar('\r', stream);
	}
	
	if (UART==0)
	{
		while (!(UCSR0A & (1<<UDRE0)))
		{
		}
		UDR0 = c;
	}
	else
	{
		while (!(UCSR1A & (1<<UDRE1)))
		{
		}
		UDR1 = c;
	}
		
}
//________________________________________________________________________________________
FILE uart_output = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
//_______________________________________________________________________________________
void clearHodnota() //cisteni retezce se kterzm pracuje klavesnice
{
	for (int i = 0;i<26;i++)
	{
		hodnota[i]=' ';
	}
	hodnota[0]='S';
	hodnota[1]='T';
	hodnota[2]='R';
	hodnota[4]='5';
	hodnota[5]='9';
	hodnota[6]='0';
	hodnota[8]='3';
	hodnota[9]='0';
	hodnota[10]='0';
	hodnota[12]='3';
	hodnota[13]='0';
	hodnota[25]='\n';
}
//_________________________________________________________________________________________
void send(char _uart, char message[])
{
	int znak=0;
	if (_uart==0)
	{
		UART=0;
		//povoleni odesilani z USART0
		UCSR0B |= (1<<TXEN0);
		while ((PORTE & (1<<PINE2)))
		{
		}
		while (message[znak]!='\n')
		{
			putchar(message[znak]);
			znak++;
		}
		putchar('\n');
		putchar('\n');
		cCommand++;
		x=0;
		y=0;
		//zakazani odesilani z USART0
		UCSR0B &= (0<<TXEN0);
	}
	else if (_uart==1)
	{
		indE=0;
		UART=1;
		//povoleni odesilani z USART1
		UCSR1B |= (1<<TXEN1);
		if (message[0]!='\n')
		{
			while (message[znak]!='\n')
			{
				putchar(message[znak]);
				znak++;
			}
			putchar('\n');
			putchar('\n');
		}
		//zakazani odesilani z USART1
		UCSR1B &= (0<<TXEN1);
		UCSR1B |= (1<<RXEN1)|(1<<RXCIE1);
	}
}

void send2(char _uart, char message[])
{
	int znak=0;
	if (_uart==1)
	{
		UART=1;
		//povoleni odesilani z USART1
		UCSR1B |= (1<<TXEN1);
		while (/*message[znak]!='\n'*/znak!=50)
		{
			putchar(message[znak]);
			znak++;
		}
		putchar('\n');
		UCSR1B &= (0<<TXEN1);
	}
}
//_____________________________________________________________________________________________
int decode (char znak, char rad) // funkce ktera vytvari z cisla v retezci hodnotu typu INT
{
	int odpoved=0;

	switch(znak){
		case 48:
		odpoved = 0*rad; break;
		case 49:
		odpoved = 1*rad; break;
		case 50:
		odpoved = 2*rad; break;
		case 51:
		odpoved = 3*rad; break;
		case 52:
		odpoved = 4*rad; break;
		case 53:
		odpoved = 5*rad; break;
		case 54:
		odpoved = 6*rad; break;
		case 55:
		odpoved = 7*rad; break;
		case 56:
		odpoved = 8*rad; break;
		case 57:
		odpoved = 9*rad; break;
	}
	return odpoved;
}
//____________________________________________________________________________________________
void touchPosition () //funkce ktera slouzi pro vypocet souradnic x a y mista kliku na displej
{
	int cZnak;
	//nalezeni polohy mezer a konce v input[]
	char mezera1 = 100;
	char mezera2=100;
	char konec=100;
	for (cZnak=0;cZnak<15;cZnak++)
	{
		if ((input[cZnak]==' ') && (mezera1==100))
		{
			mezera1=cZnak;
		}
		else if ((input[cZnak]==' ') && (mezera1!=100))
		{
			mezera2=cZnak;
		}
		else if ((input[cZnak]=='\n') && (mezera2!=100))
		{
			konec=cZnak;
		}
	}
	x=0;
	y=0;
	//dekodovoani souradnice x
	
	rad=1;
	for (cZnak=mezera2-1;cZnak>mezera1;cZnak--)
	{
		x+=decode(input[cZnak],rad);
		rad*=10;
	}
	
	//dekodovoani souradnice y
	rad=1;
	for (cZnak=konec-1;cZnak>mezera2;cZnak--)
	{
		y+=decode(input[cZnak],rad);
		rad*=10;
	}
}
//__________________________________________________________________________________________________________________
void automat() //stavovy automat
{
	//INICIALIZACE
	if (stav==0)
	{
		for (int i = 0;i<50;i++)
		{
			_delay_ms(20);
		}
		send(0,"LOAD 0 0 screens/1.jpg\n");
		send(0,"LOAD 0 0 screens/1.jpg\n");
		UCSR0B |= (1<<RXCIE0)|(1<<RXEN0);
		cCommand=1;
		stav=1;
		for (int i=0;i<600;i++)
		{
			data[i]=0;
		}
	}
	//HLAVNI MENU
	else if(stav==1)
	{
		if ((x>=641) && (x<=760) && (y>=45) && (y<=94))//KLIK NA TLACITKO MERENI
		{
			if (metoda!=0)
			{
				nastaveno=1;
			}
			else if (metoda==0)
			{
				nastaveno=0;
			}
			if (nastaveno==0)//neni nastaveno
			{
				send(0,"LOAD 0 0 screens/2.jpg\n");
				stav=2;
			}
			else if (nastaveno==1)//je nastaveno
			{
				//obraz
				send(0,"LOAD 0 0 screens/7.jpg\n");
				//metoda
				if (metoda==1)
				{
					if (rozsah==1)
					{
						send(0,"STR 45 170 30 TURBIDIMETR 25 - 200 NTU\n");
					}
					else if (rozsah==2)
					{
						send(0,"STR 45 170 30 TURBIDIMETR 200 - 500 NTU\n");
					}
				}
				else if (metoda==2)
				{
					if (rozsah==1)
					{
						send(0,"STR 45 170 30 NEFELOMETR 25 - 200 NTU\n");
					}
					else if (rozsah==2)
					{
						send(0,"STR 45 170 30 NEFELOMETR 200 - 500 NTU\n");
					}
				}
				else if (metoda==3)
				{
					if (rozsah==1)
					{
						send(0,"STR 45 170 30 POMEROVA 25 - 200 NTU\n");
					}
					else if (rozsah==2)
					{
						send(0,"STR 45 170 30 POMEROVA 200 - 500 NTU\n");
					}
				}
				//posledni hodnota
				char message[21] = "STR 290 170 30 XXXXX\n";
				char st[5];
				itoa(prumer,st,10);
				message[15]=st[0];
				message[16]=st[1];
				message[17]=st[2];
				message[18]=st[3];
				message[19]=st[4];
				send(0,message);
				//stav
				if (DO1==1)
				{
					send(0,"CLR 243 258 286 301 2016\n");
				}
				if (DO2==1)
				{
					send(0,"CLR 243 305 286 348 2016\n");
				}
				if (AO1==1)
				{
					send(0,"CLR 243 352 286 395 2016\n");
				}
				if (AO2==1)
				{
					send(0,"CLR 243 399 286 442 2016\n");
				}
				// hodnoty vystupu
				send(0,hodnotaDO1M);
				send(0,hodnotaDO2M);
				send(0,hodnotaAO1M);
				send(0,hodnotaAO2M);
				stav=5;
			}
		}
		else if ((x>=641) && (x<=760) && (y>=130) && (y<=179))//KLIK NA TLACITKO NASTAVENI
		{
			//obraz
			send(0,"LOAD 0 0 screens/9.jpg\n");
			//metoda
			if (metoda==1)
			{
				send(0,"CLR 254 144 297 187 2016\n");
				//send(0,"CLR 254 206 297 249 63488\n");
				//send(0,"CLR 254 268 297 311 63488\n");
			}
			else if (metoda==2)
			{
				send(0,"CLR 254 144 297 187 63488\n");
				send(0,"CLR 254 206 297 249 2016\n");
				send(0,"CLR 254 268 297 311 63488\n");
			}
			else if (metoda==3)
			{
				send(0,"CLR 254 144 297 187 63488\n");
				send(0,"CLR 254 206 297 249 63488\n");
				send(0,"CLR 254 268 297 311 2016\n");
			}
			//rozsah
			if (rozsah==1)
			{
				send(0,"CLR 254 360 297 403 2016\n");
				send(0,"CLR 254 422 297 465 63488\n");
			}
			else if (rozsah==2)
			{
				send(0,"CLR 254 360 297 403 63488\n");
				send(0,"CLR 254 422 297 465 2016\n");
			}
			//mezni hodnota
			send(0,hodnotaMez);
			//casova konstanta
			send(0,hodnotaCas);
			stav=7;
			MenuToNast=1;
		}
		else if ((x>=641) && (x<=760) && (y>=215) && (y<=264))//KLIK NA TLACITKO NAPOVEDA
		{
			send(0,"LOAD 0 0 screens/4.jpg\n");
			stav=4;
		}
		else if ((x>=641) && (x<=760) && (y>=305) && (y<=354))//KLIK NA TLACITKO INFORMACE
		{
			send(0,"LOAD 0 0 screens/5.jpg\n");
			stav=3;
		}
		else if ((x>=641) && (x<=760) && (y>=390) && (y<=439))//KLIK NA TLACITKO RESET
		{
			send(0,"LOAD 0 0 screens/1.jpg\n");
			stav=1;
		}
		else //KLIK JINAM NEZ NA AKTIVNI PRVKY SCREENU
		{
			send(0,"LOAD 0 0 screens/1.jpg\n");
			stav=1;
		}
	}
	//MERENI-NENI NASTAVENO
	else if (stav==2)
	{
		if ((x>=80) && (x<=199) && (y>=400) && (y<=449))//KLIK NA TLACITKO NASTAVENI
		{
			//obraz
			send(0,"LOAD 0 0 screens/9.jpg\n");
			//metoda
			if (metoda==1)
			{
				send(0,"CLR 254 144 297 187 2016\n");
				//send(0,"CLR 254 206 297 249 63488\n");
				//send(0,"CLR 254 268 297 311 63488\n");
			}
			else if (metoda==2)
			{
				send(0,"CLR 254 144 297 187 63488\n");
				send(0,"CLR 254 206 297 249 2016\n");
				send(0,"CLR 254 268 297 311 63488\n");
			}
			else if (metoda==3)
			{
				send(0,"CLR 254 144 297 187 63488\n");
				send(0,"CLR 254 206 297 249 63488\n");
				send(0,"CLR 254 268 297 311 2016\n");
			}
			//rozsah
			if (rozsah==1)
			{
				send(0,"CLR 254 360 297 403 2016\n");
				send(0,"CLR 254 422 297 465 63488\n");
			}
			else if (rozsah==2)
			{
				send(0,"CLR 254 360 297 403 63488\n");
				send(0,"CLR 254 422 297 465 2016\n");
			}
			//mezni hodnota
			send(0,hodnotaMez);
			//casova konstanta
			send(0,hodnotaCas);
			stav=7;
			MenuToNast=1;
		}
		else if ((x>=600) && (x<=719) && (y>=400) && (y<=449))//KLIK NA TLACITKO ZPET DO HLAVNIHO MENU
		{
			send(0,"LOAD 0 0 screens/1.jpg\n");
			stav=1;
		}
		else //KLIK JINAM NEZ NA AKTIVNI PRVKY SCREENU
		{
			send(0,"LOAD 0 0 screens/2.jpg\n");
			stav=2;
		}
	}
	//INFORMACE
	else if (stav==3)
	{
		if ((x>=600) && (x<=719) && (y>=400) && (y<=449))//KLIK NA TLACITKO ZPET DO HLAVNIHO MENU
		{
			send(0,"LOAD 0 0 screens/1.jpg\n");
			stav=1;
		}
		else //KLIK JINAM NEZ NA AKTIVNI PRVKY SCREENU
		{
			send(0,"LOAD 0 0 screens/5.jpg\n");
			stav=3;
		}
	}
	//NAPOVEDA
	else if (stav==4)
	{
		if ((x>=600) && (x<=719) && (y>=400) && (y<=449))//KLIK NA TLACITKO ZPET DO HLAVNIHO MENU
		{
			send(0,"LOAD 0 0 screens/1.jpg\n");
			stav=1;
		}
		else if ((x>=250) && (x<=369) && (y>=400) && (y<=449))//KLIK NA TLACITKO DALSI NAPOVEDA
		{
			send(0,"LOAD 0 0 screens/4.jpg\n");
			stav=4;
		}
		else //KLIK JINAM NEZ NA AKTIVNI PRVKY SCREENU
		{
			send(0,"LOAD 0 0 screens/4.jpg\n");

			stav=4;
		}
	}
	//MERENI STOP
	else if (stav==5)
	{
		if ((x>=641) && (x<=760) && (y>=45) && (y<=94))//KLIK NA TLACITKO START
		{
			//vycisteni posuvneho registru data
			for (int i=0;i<600;i++)
			{
				data[i]=0;
			}
			//povoleni preruseni od timeru 3, 0, 2 - spusteni mereni a PWM analogovzch vzstupu
			//ETIMSK |= (1<<TOIE0)|(1<<TOIE2);
			//OD piny na XTR11 do log 0 (output disable)
			PORTC |= 0b00001010;
			sbi(PORTA,PINA7);
			//obraz
			send(0,"LOAD 0 0 screens/8.jpg\n");
			//metoda
			if (metoda==1)
			{
				if (rozsah==1)
				{
					send(0,"STR 45 170 30 TURBIDIMETR 25 - 200 NTU\n");
				}
				else if (rozsah==2)
				{
					send(0,"STR 45 170 30 TURBIDIMETR 200 - 500 NTU\n");
				}
			}
			else if (metoda==2)
			{
				if (rozsah==1)
				{
					send(0,"STR 45 170 30 NEFELOMETR 25 - 200 NTU\n");
				}
				else if (rozsah==2)
				{
					send(0,"STR 45 170 30 NEFELOMETR 200 - 500 NTU\n");
				}
			}
			else if (metoda==3)
			{
				if (rozsah==1)
				{
					send(0,"STR 45 170 30 POMEROVA 25 - 200 NTU\n");
				}
				else if (rozsah==2)
				{
					send(0,"STR 45 170 30 POMEROVA 200 - 500 NTU\n");
				}
			}
			//aktualni hodnota
			char message[25] = "STR 290 170 30 \n\n\n\n\n\n\n\n\n\n";
			char st[5];
			itoa(prumer,st,10);
			message[15]=st[0];
			message[16]=st[1];
			message[17]=st[2];
			message[18]=st[3];
			message[19]=st[4];
			send(0,message);
			//stav
			if (DO1==1)
			{
				send(0,"CLR 243 258 286 301 2016\n");
			}
			if (DO2==1)
			{
				send(0,"CLR 243 305 286 348 2016\n");
			}
			if (AO1==1)
			{
				send(0,"CLR 243 352 286 395 2016\n");
			}
			if (AO2==1)
			{
				send(0,"CLR 243 399 286 442 2016\n");
			}
			// hodnoty vystupu
			send(0,hodnotaDO1M);
			send(0,hodnotaDO2M);
			send(0,hodnotaAO1M);
			send(0,hodnotaAO2M);
			stav=6;
		}
		else if ((x>=641) && (x<=760) && (y>=300) && (y<=349))//KLIK NA TLACITKO NASTAVENI
		{
			//obraz
			send(0,"LOAD 0 0 screens/9.jpg\n");
			//metoda
			if (metoda==1)
			{
				send(0,"CLR 254 144 297 187 2016\n");
				//send(0,"CLR 254 206 297 249 63488\n");
				//send(0,"CLR 254 268 297 311 63488\n");
			}
			else if (metoda==2)
			{
				send(0,"CLR 254 144 297 187 63488\n");
				send(0,"CLR 254 206 297 249 2016\n");
				send(0,"CLR 254 268 297 311 63488\n");
			}
			else if (metoda==3)
			{
				send(0,"CLR 254 144 297 187 63488\n");
				send(0,"CLR 254 206 297 249 63488\n");
				send(0,"CLR 254 268 297 311 2016\n");
			}
			//rozsah
			if (rozsah==1)
			{
				send(0,"CLR 254 360 297 403 2016\n");
				send(0,"CLR 254 422 297 465 63488\n");
			}
			else if (rozsah==2)
			{
				send(0,"CLR 254 360 297 403 63488\n");
				send(0,"CLR 254 422 297 465 2016\n");
			}
			//mezni hodnota
			send(0,hodnotaMez);
			//casova konstanta
			send(0,hodnotaCas);
			stav=7;
			MenuToNast=0;
		}
		else if ((x>=641) && (x<=760) && (y>=390) && (y<=439))//KLIK NA TLACITKO ZPET DO HLAVNIHO MENU
		{
			send(0,"LOAD 0 0 screens/1.jpg\n");
			stav=1;
		}
		else //KLIK JINAM NEZ NA AKTIVNI PRVKY SCREENU
		{
			//obraz
			send(0,"LOAD 0 0 screens/7.jpg\n");
			//metoda
			if (metoda==1)
			{
				if (rozsah==1)
				{
					send(0,"STR 45 170 30 TURBIDIMETR 25 - 200 NTU\n");
				}
				else if (rozsah==2)
				{
					send(0,"STR 45 170 30 TURBIDIMETR 200 - 500 NTU\n");
				}
			}
			else if (metoda==2)
			{
				if (rozsah==1)
				{
					send(0,"STR 45 170 30 NEFELOMETR 25 - 200 NTU\n");
				}
				else if (rozsah==2)
				{
					send(0,"STR 45 170 30 NEFELOMETR 200 - 500 NTU\n");
				}
			}
			else if (metoda==3)
			{
				if (rozsah==1)
				{
					send(0,"STR 45 170 30 POMEROVA 25 - 200 NTU\n");
				}
				else if (rozsah==2)
				{
					send(0,"STR 45 170 30 POMEROVA 200 - 500 NTU\n");
				}
			}
			//posledni hodnota
			char message[25] = "STR 290 170 30 \n\n\n\n\n\n\n\n\n\n";
			char st[5];
			itoa(prumer,st,10);
			message[15]=st[0];
			message[16]=st[1];
			message[17]=st[2];
			message[18]=st[3];
			message[19]=st[4];
			send(0,message);
			//stav
			if (DO1==1)
			{
				send(0,"CLR 243 258 286 301 2016\n");
			}
			if (DO2==1)
			{
				send(0,"CLR 243 305 286 348 2016\n");
			}
			if (AO1==1)
			{
				send(0,"CLR 243 352 286 395 2016\n");
			}
			if (AO2==1)
			{
				send(0,"CLR 243 399 286 442 2016\n");
			}
			// hodnoty vystupu
			send(0,hodnotaDO1M);
			send(0,hodnotaDO2M);
			send(0,hodnotaAO1M);
			send(0,hodnotaAO2M);
			stav=5;
		}
	}
	//MERENI RUN
	else if (stav==6)
	{
		if ((x>=641) && (x<=760) && (y>=135) && (y<=184))//KLIK NA TLACITKO STOP
		{
			//yhasnuti IR LED
			cbi(PORTA,PINA7);
			//zakazani preruseni od timeru 3, 0, 2
			ETIMSK &= (0<<TOIE0)&(0<<TOIE2);			
			//obraz
			send(0,"LOAD 0 0 screens/7.jpg\n");
			//metoda
			if (metoda==1)
			{
				if (rozsah==1)
				{
					send(0,"STR 45 170 30 TURBIDIMETR 25 - 200 NTU\n");
				}
				else if (rozsah==2)
				{
					send(0,"STR 45 170 30 TURBIDIMETR 200 - 500 NTU\n");
				}
			}
			else if (metoda==2)
			{
				if (rozsah==1)
				{
					send(0,"STR 45 170 30 NEFELOMETR 25 - 200 NTU\n");
				}
				else if (rozsah==2)
				{
					send(0,"STR 45 170 30 NEFELOMETR 200 - 500 NTU\n");
				}
			}
			else if (metoda==3)
			{
				if (rozsah==1)
				{
					send(0,"STR 45 170 30 POMEROVA 25 - 200 NTU\n");
				}
				else if (rozsah==2)
				{
					send(0,"STR 45 170 30 POMEROVA 200 - 500 NTU\n");
				}
			}
			//posledni hodnota
			char message[25] = "STR 290 170 30 \n\n\n\n\n\n\n\n\n\n";
			char st[5];
			itoa(prumer,st,10);
			message[15]=st[0];
			message[16]=st[1];
			message[17]=st[2];
			message[18]=st[3];
			message[19]=st[4];
			send(0,message);
			//stav
			if (DO1==1)
			{
				send(0,"CLR 243 258 286 301 2016\n");
			}
			if (DO2==1)
			{
				send(0,"CLR 243 305 286 348 2016\n");
			}
			if (AO1==1)
			{
				send(0,"CLR 243 352 286 395 2016\n");
			}
			if (AO2==1)
			{
				send(0,"CLR 243 399 286 442 2016\n");
			}
			// hodnoty vystupu
			send(0,hodnotaDO1M);
			send(0,hodnotaDO2M);
			send(0,hodnotaAO1M);
			send(0,hodnotaAO2M);
			stav=5;
		}
		else if (MR==1)
		{
			MR=0;
			//aktualni hodnota
			char message[25] = "STR 290 170 30 \n\n\n\n\n\n\n\n\n\n";
			char st[5];
			itoa(prumer,st,10);
			message[15]=st[0];
			message[16]=st[1];
			message[17]=st[2];
			message[18]=st[3];
			message[19]=st[4];
			send(0,"CLR 244 162 414 204 2016\n");
			send(0,message);
			// hodnoty vystupu
			if (DO1==1)
			{
				send(0,hodnotaDO1M);
			}
			if (DO2==1)
			{
				send(0,hodnotaDO2M);
			}
			if (AO1==1)
			{
				send(0,hodnotaAO1M);
			}
			if (AO2==1)
			{
				send(0,hodnotaAO2M);
			}
			stav=6;
		}
		else //KLIK JINAM NEZ NA AKTIVNI PRVKY SCREENU
		{
			//obraz
			send(0,"LOAD 0 0 screens/8.jpg\n");
			//metoda
			if (metoda==1)
			{
				if (rozsah==1)
				{
					send(0,"STR 45 170 30 TURBIDIMETR 25 - 200 NTU\n");
				}
				else if (rozsah==2)
				{
					send(0,"STR 45 170 30 TURBIDIMETR 200 - 500 NTU\n");
				}
			}
			else if (metoda==2)
			{
				if (rozsah==1)
				{
					send(0,"STR 45 170 30 NEFELOMETR 25 - 200 NTU\n");
				}
				else if (rozsah==2)
				{
					send(0,"STR 45 170 30 NEFELOMETR 200 - 500 NTU\n");
				}
			}
			else if (metoda==3)
			{
				if (rozsah==1)
				{
					send(0,"STR 45 170 30 POMEROVA 25 - 200 NTU\n");
				}
				else if (rozsah==2)
				{
					send(0,"STR 45 170 30 POMEROVA 200 - 500 NTU\n");
				}
			}
			//aktualni hodnota
			char message[25] = "STR 290 170 30 \n\n\n\n\n\n\n\n\n\n";
			char st[5];
			itoa(prumer,st,10);
			message[15]=st[0];
			message[16]=st[1];
			message[17]=st[2];
			message[18]=st[3];
			message[19]=st[4];
			send(0,message);
			//stav
			if (DO1==1)
			{
				send(0,"CLR 243 258 286 301 2016\n");
			}
			if (DO2==1)
			{
				send(0,"CLR 243 305 286 348 2016\n");
			}
			if (AO1==1)
			{
				send(0,"CLR 243 352 286 395 2016\n");
			}
			if (AO2==1)
			{
				send(0,"CLR 243 399 286 442 2016\n");
			}
			// hodnoty vystupu
			send(0,hodnotaDO1M);
			send(0,hodnotaDO2M);
			send(0,hodnotaAO1M);
			send(0,hodnotaAO2M);
			stav=6;
		}
	}
	//NASTAVENI 1/2
	else if (stav==7)
	{
		if ((x>=400) && (x<=519) && (y>=400) && (y<=449))//KLIK NA TLACITKO NASTAVENI 2/2
		{
			send(0,"LOAD 0 0 screens/10.jpg\n");
			//stav
			if (DO1==1)
			{
				send(0,"CLR 254 144 297 187 2016\n");
			}
			if (DO2==1)
			{
				send(0,"CLR 254 206 297 249 2016\n");
			}
			if (AO1==1)
			{
				send(0,"CLR 254 268 297 311 2016\n");
			}
			if (AO2==1)
			{
				send(0,"CLR 254 330 297 373 2016\n");
			}
			// hodnoty vystupu
			send(0,hodnotaDO1N);
			send(0,hodnotaDO2N);
			send(0,hodnotaAO1N);
			send(0,hodnotaAO2N);
			stav=8;
		}
		else if ((x>=600) && (x<=719) && (y>=400) && (y<=449))//KLIK NA TLACITKO ZPET
		{
			if (MenuToNast == 1)//DO HLAVNIHO MENU
			{
				send(0,"LOAD 0 0 screens/1.jpg\n");
				stav=1;
			}
			else if (MenuToNast == 0)//DO MERENI STOP
			{
				//obraz
				send(0,"LOAD 0 0 screens/7.jpg\n");
				//metoda
				if (metoda==1)
				{
					if (rozsah==1)
					{
						send(0,"STR 45 170 30 TURBIDIMETR 25 - 200 NTU\n");
					}
					else if (rozsah==2)
					{
						send(0,"STR 45 170 30 TURBIDIMETR 200 - 500 NTU\n");
					}
				}
				else if (metoda==2)
				{
					if (rozsah==1)
					{
						send(0,"STR 45 170 30 NEFELOMETR 25 - 200 NTU\n");
					}
					else if (rozsah==2)
					{
						send(0,"STR 45 170 30 NEFELOMETR 200 - 500 NTU\n");
					}
				}
				else if (metoda==3)
				{
					if (rozsah==1)
					{
						send(0,"STR 45 170 30 POMEROVA 25 - 200 NTU\n");
					}
					else if (rozsah==2)
					{
						send(0,"STR 45 170 30 POMEROVA 200 - 500 NTU\n");
					}
				}
				//posledni hodnota
				char message[21] = "STR 290 170 30 XXXXX\n";
				char st[5];
				itoa(prumer,st,10);
				message[15]=st[0];
				message[16]=st[1];
				message[17]=st[2];
				message[18]=st[3];
				message[19]=st[4];
				send(0,message);
				//stav
				if (DO1==1)
				{
					send(0,"CLR 243 258 286 301 2016\n");
				}
				if (DO2==1)
				{
					send(0,"CLR 243 305 286 348 2016\n");
				}
				if (AO1==1)
				{
					send(0,"CLR 243 352 286 395 2016\n");
				}
				if (AO2==1)
				{
					send(0,"CLR 243 399 286 442 2016\n");
				}
				// hodnoty vystupu
				send(0,hodnotaDO1M);
				send(0,hodnotaDO2M);
				send(0,hodnotaAO1M);
				send(0,hodnotaAO2M);
				stav=5;
			}
		}
		else if ((x>=254) && (x<=297) && (y>=145) && (y<=188))//KLIK DO POLE A/P TURBIDIMETR
		{
			if (metoda!=1)
			{
				send(0,"CLR 254 144 297 187 2016\n");
				//send(0,"CLR 254 206 297 249 63488\n");
				//send(0,"CLR 254 268 297 311 63488\n");
				cbi(PORTA,PINA0);
				sbi(PORTA,PINA1);
				metoda=1;
				ADMUX =0x01;
			}
			else if (metoda==1)
			{
				send(0,"CLR 254 144 297 187 2016\n");
			}
			stav=7;
		}
		/*else if ((x>=254) && (x<=297) && (y>=207) && (y<=250))//KLIK DO POLE A/P NEFELOMETR
		{
			
			if (metoda!=2)
			{
				send(0,"CLR 254 144 297 187 63488\n");
				send(0,"CLR 254 206 297 249 2016\n");
				send(0,"CLR 254 268 297 311 63488\n");
				sbi(PORTA,PINA0);
				cbi(PORTA,PINA1);
				metoda=2;
				ADMUX =0x00;
			}
			else if (metoda==2)
			{
				send(0,"CLR 254 206 297 249 2016\n");
			}
			stav=7;
		}
		else if ((x>=254) && (x<=297) && (y>=269) && (y<=312))//KLIK DO POLE A/P POMEROVY
		{
			if (metoda!=3)
			{
				send(0,"CLR 254 144 297 187 63488\n");
				send(0,"CLR 254 206 297 249 63488\n");
				send(0,"CLR 254 268 297 311 2016\n");
				cbi(PORTA,PINA0);
				cbi(PORTA,PINA1);
				metoda=3;
			}
			else if (metoda==3)
			{
				send(0,"CLR 254 268 297 311 2016\n");
			}
			stav=7;
		}*/
		else if ((x>=254) && (x<=297) && (y>=361) && (y<=404))//KLIK DO POLE 25-200
		{
			if (rozsah!=1)
			{
				send(0,"CLR 254 360 297 403 2016\n");
				send(0,"CLR 254 422 297 465 63488\n");
				rozsah=1;
			}
			else
			{
				send(0,"CLR 254 360 297 403 2016\n");
			}
			stav=7;
		}
		else if ((x>=254) && (x<=297) && (y>=423) && (y<=466))//KLIK DO POLE 200-500
		{
			if (rozsah!=2)
			{
				send(0,"CLR 254 360 297 403 63488\n");
				send(0,"CLR 254 422 297 465 2016\n");
				rozsah=2;
			}
			else
			{
				send(0,"CLR 254 422 297 465 2016\n");
			}
			stav=7;
		}
		else if ((x>=400) && (x<=569) && (y>=181) && (y<=223))//KLIK DO TEXTOVEHO POLE MEZNI HODNOTA
		{
			kdoChceHodnotu=1;
			clearHodnota();
			iHodnota =15;
			send(0,"LOAD 0 0 screens/11.jpg\n");
			stav=10;
		}
		else if ((x>=400) && (x<=569) && (y>=293) && (y<=334))//KLIK DO TEXTOVEHO POLE CASOVA KONSTANTA
		{
			kdoChceHodnotu=6;
			clearHodnota();
			iHodnota =15;
			send(0,"LOAD 0 0 screens/13.jpg\n");
			stav=10;
		}
		else //KLIK JINAM NEZ NA AKTIVNI PRVKY SCREENU NEBO REFRESH
		{
			//obraz
			send(0,"LOAD 0 0 screens/9.jpg\n");
			//metoda
			if (metoda==1)
			{
				send(0,"CLR 254 144 297 187 2016\n");
				//send(0,"CLR 254 206 297 249 63488\n");
				//send(0,"CLR 254 268 297 311 63488\n");
			}
			else if (metoda==2)
			{
				send(0,"CLR 254 144 297 187 63488\n");
				send(0,"CLR 254 206 297 249 2016\n");
				send(0,"CLR 254 268 297 311 63488\n");
			}
			else if (metoda==3)
			{
				send(0,"CLR 254 144 297 187 63488\n");
				send(0,"CLR 254 206 297 249 63488\n");
				send(0,"CLR 254 268 297 311 2016\n");
			}
			//rozsah
			if (rozsah==1)
			{
				send(0,"CLR 254 360 297 403 2016\n");
				send(0,"CLR 254 422 297 465 63488\n");
			}
			else if (rozsah==2)
			{
				send(0,"CLR 254 360 297 403 63488\n");
				send(0,"CLR 254 422 297 465 2016\n");
			}
			//mezni hodnota
			send(0,hodnotaMez);
			//casova konstanta
			send(0,hodnotaCas);
			stav=7;
		}
	}
	//NASTAVENI 2/2
	else if (stav==8)
	{
		if ((x>=400) && (x<=519) && (y>=400) && (y<=449))//KLIK NA TLACITKO NASTAVENI 1/2
		{
			//obraz
			send(0,"LOAD 0 0 screens/9.jpg\n");
			//metoda
			if (metoda==1)
			{
				send(0,"CLR 254 144 297 187 2016\n");
				//send(0,"CLR 254 206 297 249 63488\n");
				//send(0,"CLR 254 268 297 311 63488\n");
			}
			else if (metoda==2)
			{
				send(0,"CLR 254 144 297 187 63488\n");
				send(0,"CLR 254 206 297 249 2016\n");
				send(0,"CLR 254 268 297 311 63488\n");
			}
			else if (metoda==3)
			{
				send(0,"CLR 254 144 297 187 63488\n");
				send(0,"CLR 254 206 297 249 63488\n");
				send(0,"CLR 254 268 297 311 2016\n");
			}
			//rozsah
			if (rozsah==1)
			{
				send(0,"CLR 254 360 297 403 2016\n");
				send(0,"CLR 254 422 297 465 63488\n");
			}
			else if (rozsah==2)
			{
				send(0,"CLR 254 360 297 403 63488\n");
				send(0,"CLR 254 422 297 465 2016\n");
			}
			//mezni hodnota
			send(0,hodnotaMez);
			//casova konstanta
			send(0,hodnotaCas);
			stav=7;
			MenuToNast=1;
		}
		else if ((x>=600) && (x<=719) && (y>=400) && (y<=449))//KLIK NA TLACITKO ZPET
		{
			if (MenuToNast == 1)//DO HLAVNIHO MENU
			{
				send(0,"LOAD 0 0 screens/1.jpg\n");
				stav=1;
			}
			else if (MenuToNast == 0)//DO MERENI STOP
			{
				//obraz
				send(0,"LOAD 0 0 screens/7.jpg\n");
				//metoda
				if (metoda==1)
				{
					if (rozsah==1)
					{
						send(0,"STR 45 170 30 TURBIDIMETR 25 - 200 NTU\n");
					}
					else if (rozsah==2)
					{
						send(0,"STR 45 170 30 TURBIDIMETR 200 - 500 NTU\n");
					}
				}
				else if (metoda==2)
				{
					if (rozsah==1)
					{
						send(0,"STR 45 170 30 NEFELOMETR 25 - 200 NTU\n");
					}
					else if (rozsah==2)
					{
						send(0,"STR 45 170 30 NEFELOMETR 200 - 500 NTU\n");
					}
				}
				else if (metoda==3)
				{
					if (rozsah==1)
					{
						send(0,"STR 45 170 30 POMEROVA 25 - 200 NTU\n");
					}
					else if (rozsah==2)
					{
						send(0,"STR 45 170 30 POMEROVA 200 - 500 NTU\n");
					}
				}
				//posledni hodnota
				char message[21] = "STR 290 170 30 XXXXX\n";
				char st[5];
				itoa(prumer,st,10);
				message[15]=st[0];
				message[16]=st[1];
				message[17]=st[2];
				message[18]=st[3];
				message[19]=st[4];
				send(0,message);
				//stav
				if (DO1==1)
				{
					send(0,"CLR 243 258 286 301 2016\n");
				}
				if (DO2==1)
				{
					send(0,"CLR 243 305 286 348 2016\n");
				}
				if (AO1==1)
				{
					send(0,"CLR 243 352 286 395 2016\n");
				}
				if (AO2==1)
				{
					send(0,"CLR 243 399 286 442 2016\n");
				}
				// hodnoty vystupu
				send(0,hodnotaDO1M);
				send(0,hodnotaDO2M);
				send(0,hodnotaAO1M);
				send(0,hodnotaAO2M);
				stav=5;
			}
		}
		else if ((x>=254) && (x<=297) && (y>=144) && (y<=187))//KLIK DO POLE A/P DO1
		{
			if (DO1==1)
			{
				send(0,"CLR 254 144 297 187 63488\n");
				DO1=0;
				sbi(PORTA,PINA5);
			}
			else
			{
				send(0,"CLR 254 144 297 187 2016\n");
				DO1=1;
				cbi(PORTA,PINA5);
			}
			stav=8;
		}
		else if ((x>=254) && (x<=297) && (y>=206) && (y<=249))//KLIK DO POLE A/P DO2
		{
			if (DO2==1)
			{
				send(0,"CLR 254 206 297 249 63488\n");
				DO2=0;
				sbi(PORTA,PINA4);
			}
			else
			{
				send(0,"CLR 254 206 297 249 2016\n");
				DO2=1;
				cbi(PORTA,PINA4);
			}
			stav=8;
		}
		else if ((x>=254) && (x<=297) && (y>=268) && (y<=311))//KLIK DO POLE A/P AO1
		{
			if (AO1==1)
			{
				send(0,"CLR 254 268 297 311 63488\n");
				AO1=0;
				sbi(PORTA,PINA3);
			}
			else
			{
				send(0,"CLR 254 268 297 311 2016\n");
				AO1=1;
				cbi(PORTA,PINA3);
			}
			stav=8;
		}
		else if ((x>=254) && (x<=297) && (y>=330) && (y<=373))//KLIK DO POLE A/P AO2
		{
			if (AO2==1)
			{
				send(0,"CLR 254 330 297 373 63488\n");
				AO2=0;
				sbi(PORTA,PINA2);
			}
			else
			{
				send(0,"CLR 254 330 297 373 2016\n");
				AO2=1;
				cbi(PORTA,PINA2);
			}
			stav=8;
		}
		else if ((x>=361) && (x<=402) && (y>=145) && (y<=186))//KLIK DO TEXTOVEHO POLE HODNOTA DO1
		{
			kdoChceHodnotu=2;
			clearHodnota();
			iHodnota =15;
			send(0,"LOAD 0 0 screens/12.jpg\n");
			stav=11;
		}
		else if ((x>=361) && (x<=402) && (y>=207) && (y<=248))//KLIK DO TEXTOVEHO POLE HODNOTA DO2
		{
			kdoChceHodnotu=3;
			clearHodnota();
			iHodnota =15;
			send(0,"LOAD 0 0 screens/12.jpg\n");
			stav=11;
		}
		else if ((x>=361) && (x<=530) && (y>=269) && (y<=310))//KLIK DO TEXTOVEHO POLE HODNOTA AO1
		{
			kdoChceHodnotu=4;
			clearHodnota();
			iHodnota =15;
			send(0,"LOAD 0 0 screens/3.jpg\n");
			stav=9;
		}
		else if ((x>=361) && (x<=530) && (y>=331) && (y<=372))//KLIK DO TEXTOVEHO POLE HODNOTA AO2
		{
			kdoChceHodnotu=5;
			clearHodnota();
			iHodnota =15;
			send(0,"LOAD 0 0 screens/3.jpg\n");
			stav=9;
		}
		else //KLIK JINAM NEZ NA AKTIVNI PRVKY SCREENU
		{
			send(0,"LOAD 0 0 screens/10.jpg\n");
			//stav
			if (DO1==1)
			{
				send(0,"CLR 254 144 297 187 2016\n");
			}
			if (DO2==1)
			{
				send(0,"CLR 254 206 297 249 2016\n");
			}
			if (AO1==1)
			{
				send(0,"CLR 254 268 297 311 2016\n");
			}
			if (AO2==1)
			{
				send(0,"CLR 254 330 297 373 2016\n");
			}
			// hodnoty vystupu
			send(0,hodnotaDO1N);
			send(0,hodnotaDO2N);
			send(0,hodnotaAO1N);
			send(0,hodnotaAO2N);
			stav=8;
		}
	}
	//KLAVESNICE ANALOGOVYCH VYSTUPU
	else if (stav==9)
	{
		if ((x>=25) && (x<=174) && (y>=25) && (y<=124))//KLIK NA TLACISKO 1
		{
			if (iHodnota==25)
			{
				iHodnota--;
			}
			hodnota[iHodnota++]='1';
			send(0,"LOAD 0 0 screens/3.jpg\n");
			send(0,hodnota);
			stav=9;
		}
		else if ((x>=185) && (x<=334) && (y>=25) && (y<=124))//KLIK NA TLACISKO 2
		{
			if (iHodnota==25)
			{
				iHodnota--;
			}
			hodnota[iHodnota++]='2';
			send(0,"LOAD 0 0 screens/3.jpg\n");
			send(0,hodnota);
			stav=9;
		}
		else if ((x>=345) && (x<=494) && (y>=25) && (y<=124))//KLIK NA TLACISKO 3
		{
			if (iHodnota==25)
			{
				iHodnota--;
			}
			hodnota[iHodnota++]='3';
			send(0,"LOAD 0 0 screens/3.jpg\n");
			send(0,hodnota);
			stav=9;
		}
		else if ((x>=25) && (x<=174) && (y>=135) && (y<=234))//KLIK NA TLACISKO 4
		{
			if (iHodnota==25)
			{
				iHodnota--;
			}
			hodnota[iHodnota++]='4';
			send(0,"LOAD 0 0 screens/3.jpg\n");
			send(0,hodnota);
			stav=9;
		}
		else if ((x>=185) && (x<=334) && (y>=135) && (y<=234))//KLIK NA TLACISKO 5
		{
			if (iHodnota==25)
			{
				iHodnota--;
			}
			hodnota[iHodnota++]='5';
			send(0,"LOAD 0 0 screens/3.jpg\n");
			send(0,hodnota);
			stav=9;
		}
		else if ((x>=345) && (x<=494) && (y>=135) && (y<=234))//KLIK NA TLACISKO 6
		{
			if (iHodnota==25)
			{
				iHodnota--;
			}
			hodnota[iHodnota++]='6';
			send(0,"LOAD 0 0 screens/3.jpg\n");
			send(0,hodnota);
			stav=9;
		}
		else if ((x>=25) && (x<=174) && (y>=245) && (y<=344))//KLIK NA TLACISKO 7
		{
			if (iHodnota==25)
			{
				iHodnota--;
			}
			hodnota[iHodnota++]='7';
			send(0,"LOAD 0 0 screens/3.jpg\n");
			send(0,hodnota);
			stav=9;
		}
		else if ((x>=185) && (x<=334) && (y>=245) && (y<=344))//KLIK NA TLACISKO 8
		{
			if (iHodnota==25)
			{
				iHodnota--;
			}
			hodnota[iHodnota++]='8';
			send(0,"LOAD 0 0 screens/3.jpg\n");
			send(0,hodnota);
			stav=9;
		}
		else if ((x>=345) && (x<=494) && (y>=245) && (y<=344))//KLIK NA TLACISKO 9
		{
			if (iHodnota==25)
			{
				iHodnota--;
			}
			hodnota[iHodnota++]='9';
			send(0,"LOAD 0 0 screens/3.jpg\n");
			send(0,hodnota);
			stav=9;
		}
		else if ((x>=185) && (x<=334) && (y>=355) && (y<=454))//KLIK NA TLACISKO 0
		{
			if (iHodnota==25)
			{
				iHodnota--;
			}
			hodnota[iHodnota++]='0';
			send(0,"LOAD 0 0 screens/3.jpg\n");
			send(0,hodnota);
			stav=9;
		}
		else if ((x>=25) && (x<=174) && (y>=355) && (y<=454))//KLIK NA TLACISKO ,
		{
			if (iHodnota==25)
			{
				iHodnota--;
			}
			hodnota[iHodnota++]=',';
			send(0,"LOAD 0 0 screens/3.jpg\n");
			send(0,hodnota);
			stav=9;
		}
		else if ((x>=345) && (x<=494) && (y>=355) && (y<=454))//KLIK NA TLACISKO BACK SPACE
		{
			if (iHodnota==25)
			{
				iHodnota--;
			}
			hodnota[--iHodnota]=' ';
			send(0,"LOAD 0 0 screens/3.jpg\n");
			send(0,hodnota);
			stav=9;
		}
		else if ((x>=580) && (x<=729) && (y>=80) && (y<=179))//KLIK NA TLACISKO OK
		{
			if (kdoChceHodnotu==4)//AO1
			{
				//hodnotaAO1 = hodnota;
				send(0,"LOAD 0 0 screens/10.jpg\n");
				//stav
				if (DO1==1)
				{
					send(0,"CLR 254 144 297 187 2016\n");
				}
				if (DO2==1)
				{
					send(0,"CLR 254 206 297 249 2016\n");
				}
				if (AO1==1)
				{
					send(0,"CLR 254 268 297 311 2016\n");
				}
				if (AO2==1)
				{
					send(0,"CLR 254 330 297 373 2016\n");
				}
				// hodnoty vystupu
				send(0,hodnotaDO1N);
				send(0,hodnotaDO2N);
				for (int i=15;i<25;i++)
				{
					hodnotaAO1N[i]=hodnota[i];
					hodnotaAO1M[i]=hodnota[i];
				}
				send(0,hodnotaAO1N);
				send(0,hodnotaAO2N);
				//prevod z retezce na cislo pro nastaveni PWM
				char carka=0;
				rad=1;
				a=0;
				b=0;
				c=0;
				for (int i=0;i<25;i++)
				{
					if (hodnotaAO1N[i]==',')
					{
						carka=i;
					}
				}
				if (carka==0)
				{
					b=0;
					c=0;
				}
				else
				{
					if (hodnotaAO1N[carka+1]<='2')
					{
						b=0;
						c=0;
					}
					else if ((hodnotaAO1N[carka+1]>'2')&&(hodnotaAO1N[carka+1]<='7'))
					{
						b=5;
						c=0;
					}
					else if (hodnotaAO1N[carka+1]>'7')
					{
						b=0;
						c=1;
					}
				}
				
				for (int i = carka;i>=15;i--)
				{
					a+=decode(hodnotaAO1N[i],rad);
					rad*=10;
				}
				if (a>=20)
				{
					a=20;
					b=0;
					c=0;
				}
				HAO1=12*a+b*6/5+c*6+15;
				//konec prevodu
				stav=8;
			}
			else if (kdoChceHodnotu==5)//AO2
			{
				//hodnotaAO2 = hodnota;
				send(0,"LOAD 0 0 screens/10.jpg\n");
				//stav
				if (DO1==1)
				{
					send(0,"CLR 254 144 297 187 2016\n");
				}
				if (DO2==1)
				{
					send(0,"CLR 254 206 297 249 2016\n");
				}
				if (AO1==1)
				{
					send(0,"CLR 254 268 297 311 2016\n");
				}
				if (AO2==1)
				{
					send(0,"CLR 254 330 297 373 2016\n");
				}
				// hodnoty vystupu
				send(0,hodnotaDO1N);
				send(0,hodnotaDO2N);
				send(0,hodnotaAO1N);
				for (int i=15;i<25;i++)
				{
					hodnotaAO2N[i]=hodnota[i];
					hodnotaAO2M[i]=hodnota[i];
				}
				send(0,hodnotaAO2N);
				//prevod z retezce na cislo pro nastaveni PWM
				char carka=0;
				rad=1;
				a=0;
				b=0;
				c=0;
				for (int i=0;i<25;i++)
				{
					if (hodnotaAO2N[i]==',')
					{
						carka=i;
					}
				}
				if (carka==0)
				{
					b=0;
					c=0;
				}
				else
				{
					if (hodnotaAO2N[carka+1]<='2')
					{
						b=0;
						c=0;
					}
					else if ((hodnotaAO2N[carka+1]>'2')&&(hodnotaAO2N[carka+1]<='7'))
					{
						b=5;
						c=0;
					}
					else if (hodnotaAO2N[carka+1]>'7')
					{
						b=0;
						c=1;
					}
				}
				
				for (int i = carka;i>=15;i--)
				{
					a+=decode(hodnotaAO2N[i],rad);
					rad*=10;
				}
				if (a>=20)
				{
					a=20;
					b=0;
					c=0;
				}
				HAO2=12*a+b*6/5+c*6+15;
				//konec prevodu
				stav=8;
			}
		}
		else //KLIK JINAM NEZ NA AKTIVNI PRVKY SCREENU
		{
			send(0,"LOAD 0 0 screens/3.jpg\n");
			send(0,hodnota);
			stav=9;
		}
	}
	//KLAVESNICE DIGITALNICH VYSTUPU
	else if (stav==11)
	{
		if ((x>=25) && (x<=174) && (y>=25) && (y<=124))//KLIK NA TLACISKO 1
		{
			hodnota[iHodnota]='1';
			send(0,"LOAD 0 0 screens/12.jpg\n");
			send(0,hodnota);
			stav=11;
		}
		else if ((x>=185) && (x<=334) && (y>=355) && (y<=454))//KLIK NA TLACISKO 0
		{
			hodnota[iHodnota]='0';
			send(0,"LOAD 0 0 screens/12.jpg\n");
			send(0,hodnota);
			stav=11;
		}
		else if ((x>=345) && (x<=494) && (y>=355) && (y<=454))//KLIK NA TLACISKO BACK SPACE
		{
			if (iHodnota==25)
			{
				iHodnota--;
			}
			hodnota[--iHodnota]=' ';
			send(0,"LOAD 0 0 screens/12.jpg\n");
			send(0,hodnota);
			stav=11;
		}
		else if ((x>=580) && (x<=729) && (y>=80) && (y<=179))//KLIK NA TLACISKO OK
		{
			if (kdoChceHodnotu==2)//DO1
			{
				send(0,"LOAD 0 0 screens/10.jpg\n");
				//stav
				if (DO1==1)
				{
					send(0,"CLR 254 144 297 187 2016\n");
				}
				if (DO2==1)
				{
					send(0,"CLR 254 206 297 249 2016\n");
				}
				if (AO1==1)
				{
					send(0,"CLR 254 268 297 311 2016\n");
				}
				if (AO2==1)
				{
					send(0,"CLR 254 330 297 373 2016\n");
				}
				// hodnoty vystupu
				hodnotaDO1N[15]=hodnota[15];
				hodnotaDO1M[15]=hodnota[15];
				send(0,hodnotaDO1N);
				send(0,hodnotaDO2N);
				send(0,hodnotaAO1N);
				send(0,hodnotaAO2N);
				//prevod z retezce na cislo
				if (hodnotaDO1N[15]==0)
				{
					HDO1=0;
				}
				else
				{
					HDO1=1;
				}
				stav=8;
			}
			else if (kdoChceHodnotu==3)//DO2
			{
				//hodnotaDO2 = hodnota;
				send(0,"LOAD 0 0 screens/10.jpg\n");
				//stav
				if (DO1==1)
				{
					send(0,"CLR 254 144 297 187 2016\n");
				}
				if (DO2==1)
				{
					send(0,"CLR 254 206 297 249 2016\n");
				}
				if (AO1==1)
				{
					send(0,"CLR 254 268 297 311 2016\n");
				}
				if (AO2==1)
				{
					send(0,"CLR 254 330 297 373 2016\n");
				}
				// hodnoty vystupu
				send(0,hodnotaDO1N);
				hodnotaDO2N[15]=hodnota[15];
				hodnotaDO2M[15]=hodnota[15];
				send(0,hodnotaDO2N);
				send(0,hodnotaAO1N);
				send(0,hodnotaAO2N);
				//prevod z retezce na cislo
				if (hodnotaDO2N[15]==0)
				{
					HDO2=0;
				}
				else
				{
					HDO2=1;
				}
				stav=8;
			}			
		}
		else //KLIK JINAM NEZ NA AKTIVNI PRVKY SCREENU
		{
			send(0,"LOAD 0 0 screens/12.jpg\n");
			send(0,hodnota);
			stav=11;
		}
	}
	//KLAVESNICE NEZNI HODNOTY CASOVA KONSTANTA
	else if (stav==10)
	{
		if ((x>=25) && (x<=174) && (y>=25) && (y<=124))//KLIK NA TLACISKO 1
		{
			if (iHodnota==25)
			{
				iHodnota--;
			}
			hodnota[iHodnota++]='1';
			if (kdoChceHodnotu==1)
			{
				send(0,"LOAD 0 0 screens/11.jpg\n");
			}
			else if (kdoChceHodnotu==6)
			{
				send(0,"LOAD 0 0 screens/13.jpg\n");
			}
			send(0,hodnota);
			stav=10;
		}
		else if ((x>=185) && (x<=334) && (y>=25) && (y<=124))//KLIK NA TLACISKO 2
		{
			if (iHodnota==25)
			{
				iHodnota--;
			}
			hodnota[iHodnota++]='2';
			if (kdoChceHodnotu==1)
			{
				send(0,"LOAD 0 0 screens/11.jpg\n");
			}
			else if (kdoChceHodnotu==6)
			{
				send(0,"LOAD 0 0 screens/13.jpg\n");
			}
			send(0,hodnota);
			stav=10;
		}
		else if ((x>=345) && (x<=494) && (y>=25) && (y<=124))//KLIK NA TLACISKO 3
		{
			if (iHodnota==25)
			{
				iHodnota--;
			}
			hodnota[iHodnota++]='3';
			if (kdoChceHodnotu==1)
			{
				send(0,"LOAD 0 0 screens/11.jpg\n");
			}
			else if (kdoChceHodnotu==6)
			{
				send(0,"LOAD 0 0 screens/13.jpg\n");
			}
			send(0,hodnota);
			stav=10;
		}
		else if ((x>=25) && (x<=174) && (y>=135) && (y<=234))//KLIK NA TLACISKO 4
		{
			if (iHodnota==25)
			{
				iHodnota--;
			}
			hodnota[iHodnota++]='4';
			if (kdoChceHodnotu==1)
			{
				send(0,"LOAD 0 0 screens/11.jpg\n");
			}
			else if (kdoChceHodnotu==6)
			{
				send(0,"LOAD 0 0 screens/13.jpg\n");
			}
			send(0,hodnota);
			stav=10;
		}
		else if ((x>=185) && (x<=334) && (y>=135) && (y<=234))//KLIK NA TLACISKO 5
		{
			if (iHodnota==25)
			{
				iHodnota--;
			}
			hodnota[iHodnota++]='5';
			if (kdoChceHodnotu==1)
			{
				send(0,"LOAD 0 0 screens/11.jpg\n");
			}
			else if (kdoChceHodnotu==6)
			{
				send(0,"LOAD 0 0 screens/13.jpg\n");
			}
			send(0,hodnota);
			stav=10;
		}
		else if ((x>=345) && (x<=494) && (y>=135) && (y<=234))//KLIK NA TLACISKO 6
		{
			if (iHodnota==25)
			{
				iHodnota--;
			}
			hodnota[iHodnota++]='6';
			if (kdoChceHodnotu==1)
			{
				send(0,"LOAD 0 0 screens/11.jpg\n");
			}
			else if (kdoChceHodnotu==6)
			{
				send(0,"LOAD 0 0 screens/13.jpg\n");
			}
			send(0,hodnota);
			stav=10;
		}
		else if ((x>=25) && (x<=174) && (y>=245) && (y<=344))//KLIK NA TLACISKO 7
		{
			if (iHodnota==25)
			{
				iHodnota--;
			}
			hodnota[iHodnota++]='7';
			if (kdoChceHodnotu==1)
			{
				send(0,"LOAD 0 0 screens/11.jpg\n");
			}
			else if (kdoChceHodnotu==6)
			{
				send(0,"LOAD 0 0 screens/13.jpg\n");
			}
			send(0,hodnota);
			stav=10;
		}
		else if ((x>=185) && (x<=334) && (y>=245) && (y<=344))//KLIK NA TLACISKO 8
		{
			if (iHodnota==25)
			{
				iHodnota--;
			}
			hodnota[iHodnota++]='8';
			if (kdoChceHodnotu==1)
			{
				send(0,"LOAD 0 0 screens/11.jpg\n");
			}
			else if (kdoChceHodnotu==6)
			{
				send(0,"LOAD 0 0 screens/13.jpg\n");
			}
			send(0,hodnota);
			stav=10;
		}
		else if ((x>=345) && (x<=494) && (y>=245) && (y<=344))//KLIK NA TLACISKO 9
		{
			if (iHodnota==25)
			{
				iHodnota--;
			}
			hodnota[iHodnota++]='9';
			if (kdoChceHodnotu==1)
			{
				send(0,"LOAD 0 0 screens/11.jpg\n");
			}
			else if (kdoChceHodnotu==6)
			{
				send(0,"LOAD 0 0 screens/13.jpg\n");
			}
			send(0,hodnota);
			stav=10;
		}
		else if ((x>=185) && (x<=334) && (y>=355) && (y<=454))//KLIK NA TLACISKO 0
		{
			if (iHodnota==25)
			{
				iHodnota--;
			}
			hodnota[iHodnota++]='0';
			if (kdoChceHodnotu==1)
			{
				send(0,"LOAD 0 0 screens/11.jpg\n");
			}
			else if (kdoChceHodnotu==6)
			{
				send(0,"LOAD 0 0 screens/13.jpg\n");
			}
			send(0,hodnota);
			stav=10;
		}
		else if ((x>=345) && (x<=494) && (y>=355) && (y<=454))//KLIK NA TLACISKO BACK SPACE
		{
			if (iHodnota==25)
			{
				iHodnota--;
			}
			hodnota[--iHodnota]=' ';
			send(0,"LOAD 0 0 screens/13.jpg\n");
			send(0,hodnota);
			stav=10;
		}
		else if ((x>=580) && (x<=729) && (y>=80) && (y<=179))//KLIK NA TLACISKO OK
		{
			if (kdoChceHodnotu==1)//mezni hodnota
			{
				//obraz
				send(0,"LOAD 0 0 screens/9.jpg\n");
				//metoda
				if (metoda==1)
				{
					send(0,"CLR 254 144 297 187 2016\n");
					//send(0,"CLR 254 206 297 249 63488\n");
					//send(0,"CLR 254 268 297 311 63488\n");
				}
				else if (metoda==2)
				{
					send(0,"CLR 254 144 297 187 63488\n");
					send(0,"CLR 254 206 297 249 2016\n");
					send(0,"CLR 254 268 297 311 63488\n");
				}
				else if (metoda==3)
				{
					send(0,"CLR 254 144 297 187 63488\n");
					send(0,"CLR 254 206 297 249 63488\n");
					send(0,"CLR 254 268 297 311 2016\n");
				}
				//rozsah
				if (rozsah==1)
				{
					send(0,"CLR 254 360 297 403 2016\n");
					send(0,"CLR 254 422 297 465 63488\n");
				}
				else if (rozsah==2)
				{
					send(0,"CLR 254 360 297 403 63488\n");
					send(0,"CLR 254 422 297 465 2016\n");
				}
				//mezni hodnota
				for (int i=15;i<25;i++)
				{
					hodnotaMez[i]=hodnota[i];
				}
				send(0,hodnotaMez);
				//casova konstanta
				send(0,hodnotaCas);
				//prevod z retezce na cislo kvuli porovnavani hodnot
				rad=1;
				Hmez=0;
				for (int i=25;i>=15;i--)
				{
					if ((hodnotaMez[i] >= '0')&&(hodnotaMez[i]<='9'))
					{
						Hmez+=decode(hodnotaMez[i],rad);
						rad*=10;
					}
				}
				stav=7;
			}
			else if (kdoChceHodnotu==6)//casova konstanta
			{
				//obraz
				send(0,"LOAD 0 0 screens/9.jpg\n");
				//metoda
				if (metoda==1)
				{
					send(0,"CLR 254 144 297 187 2016\n");
					//send(0,"CLR 254 206 297 249 63488\n");
					//send(0,"CLR 254 268 297 311 63488\n");
				}
				else if (metoda==2)
				{
					send(0,"CLR 254 144 297 187 63488\n");
					send(0,"CLR 254 206 297 249 2016\n");
					send(0,"CLR 254 268 297 311 63488\n");
				}
				else if (metoda==3)
				{
					send(0,"CLR 254 144 297 187 63488\n");
					send(0,"CLR 254 206 297 249 63488\n");
					send(0,"CLR 254 268 297 311 2016\n");
				}
				//rozsah
				if (rozsah==1)
				{
					send(0,"CLR 254 360 297 403 2016\n");
					send(0,"CLR 254 422 297 465 63488\n");
				}
				else if (rozsah==2)
				{
					send(0,"CLR 254 360 297 403 63488\n");
					send(0,"CLR 254 422 297 465 2016\n");
				}
				//casova konstanta
				for (int i=15;i<25;i++)
				{
					hodnotaCas[i]=hodnota[i];
				}
				send(0,hodnotaMez);
				//casova konstanta
				send(0,hodnotaCas);
				//prevod z retezce na cislo kvuli porovnavani hodnot
				rad=1;
				Cas=0;
				for (int i=25;i>=15;i--)
				{
					if ((hodnotaCas[i] >= '0')&&(hodnotaCas[i]<='9'))
					{
						Cas+=decode(hodnotaCas[i],rad);
						rad*=10;
					}
				}
				stav=7;
			}
		}
		else //KLIK JINAM NEZ NA AKTIVNI PRVKY SCREENU
		{
			if (kdoChceHodnotu==1)
			{	
				send(0,"LOAD 0 0 screens/11.jpg\n");
				send(0,hodnota);
				stav=10;
			}
			if (kdoChceHodnotu==6)
			{
				send(0,"LOAD 0 0 screens/13.jpg\n");
				send(0,hodnota);
				stav=10;
			}
		}
	}
	//osetreni aby vzdy svitil spravny screen, protoze displej ma refresh po 30ti prijatych prikazech
	if (cCommand>=30)
	{
		cCommand=cCommand-30;
		automat();
	}
	//konec osetreni
	x=0;
	y=0;
}
//___________________________________________________________________________________________________________
void stavMereni() //funkce ktera slouzi k vyhodnoceni a prenastaveni vystupu na zaklade zmerene hodnoty
{
	if (prumer>Hmez)
	{
		sbi(PORTC,PINC0);
		cbi(PORTC,PINC1);
	}
	else if (prumer<=Hmez)
	{
		cbi(PORTC,PINC0);
		sbi(PORTC,PINC1);
	}
}
//___________________________________________________________________________________________________________
void automatEthernet()
{
	if (ethernet==128)//vzhodnoceni po pokusu o otevreni LISTEN TCP SOCKETU
	{
		if ((ethernetCommand[17]=='I')&&(ethernetCommand[21]=='0'))//byl otevren listen socket s cislem 10
		{
			listen_socket=10;
			ethernet=64;
		}
		else if ((ethernetCommand[17]=='I')&&(ethernetCommand[21]=='1'))//byl otevren listen socket s cislem 11
		{
			listen_socket=11;
			ethernet=64;
		}
		else if ((ethernetCommand[17]=='I')&&(ethernetCommand[19]=='E'))//nastala chyba a prikaz pro otevreni socketu se posle znova
		{
			send(1,"AT+iLTCP:20100,1\r\n");
		}
	}
	else if (ethernet==64)//overeni jestli je pripojen klient
	{
		if ((ethernetCommand[13]=='I')&&(ethernetCommand[3]=='E'))//chyba - neni otevreny LISTEN TCP SOCKET
		{
			ethernet=128;
			send(1,"AT+iLTCP:20100,1\r\n");
		}
		else if ((ethernetCommand[1]=='I')&&(ethernetCommand[4]=='-'))//k socketu neni propojen zadny klient
		{
			//zustavame v tomto stavu
		}
		else if ((ethernetCommand[1]=='I')&&(ethernetCommand[4]>='0')&&(ethernetCommand[4]<='9'))//je vytvoreno aktivni spojeni klient-server s cislem v active_socket
		{
			ethernet=32;		
			active_socket[0]=ethernetCommand[4];
			active_socket[1]=ethernetCommand[5];
			active_socket[2]=ethernetCommand[6];
			
			for (int i=0;i<3;i++)
			{
				NENI_NASTAVENO[i+10] = active_socket[i];
				MERENI_STOP[i+10] = active_socket[i];
				NASTAVENI1[i+10] = active_socket[i];
				LOAD_SCREEN[i+10] = active_socket[i];
				MERENI_RUN[i+10] = active_socket[i];
				TURBIDIMETR[i+10] = active_socket[i];
				NEFELOMETR[i+10] = active_socket[i];
				POMEROVY[i+10] = active_socket[i];
				HODNOTA_MEZ[i+10] = active_socket[i];
				NASTAVENI2[i+10] = active_socket[i];
				STAV_DO1_A[i+10] = active_socket[i];
				STAV_DO1_P[i+10] = active_socket[i];
				STAV_DO2_A[i+10] = active_socket[i];
				STAV_DO2_P[i+10] = active_socket[i];
				STAV_AO1_A[i+10] = active_socket[i];
				STAV_AO1_P[i+10] = active_socket[i];
				STAV_AO2_A[i+10] = active_socket[i];
				STAV_AO2_P[i+10] = active_socket[i];
				HODNOTA_DO1[i+10] = active_socket[i];
				HODNOTA_DO2[i+10] = active_socket[i];
				HODNOTA_AO1[i+10] = active_socket[i];
				HODNOTA_AO2[i+10] = active_socket[i];
				HODNOTY_ALL[i+10] = active_socket[i];
				NYNI_NELZE[i+10] = active_socket[i];
				HODNOTA_CAS[i+10] = active_socket[i];
				NEZNAMY_PRIKAZ[i+10] = active_socket[i];
				ROZSAH1[i+10] = active_socket[i];
				ROZSAH2[i+10] = active_socket[i];
			}
			EIMSK|=(1<<INT7);//povoleni ext preruseni na pinu INT7
		}
	}
	else if (ethernet==32)
	{
		//W8 FOR DATA
	}
	else if (ethernet==16)
	{
		if ((ethernetCommand[1]=='I')&&(ethernetCommand[3]=='0'))//buffer v NSL byl prazdny
		{
			ethernet=32;
			EIMSK|=(1<<INT7);//povoleni ext preruseni na pinu INT7
		}
		else if ((ethernetCommand[1]=='I')&&(ethernetCommand[3]>='0')&&(ethernetCommand[3]<='9'))//prisla nejaka data
		{
			//vyhodnoceni prijatych dat (prikazu z PC), obsluha a odpoved do NSL
			if ((ethernetCommand[11]=='M'))//I/11:KLIK_MERENI
			{
				ethernet=8;
				if (stav==1)
				{
					x=700;
					y=70;
					automat();
					if (nastaveno==0)
					{
						send(1,NENI_NASTAVENO);
					}
					else if (nastaveno!=0)
					{
						send(1,MERENI_STOP);
					}
				}
				else
				{
					ethernet=8;
					send(1,NYNI_NELZE);
				}
			}
			else if ((ethernetCommand[11]=='N')&&(ethernetCommand[20]=='1'))//I/15:KLIK_NASTAVENI1
			{
				ethernet=8;				
				if (stav==1)
				{
					x=700;
					y=150;
					automat();
					send(1,NASTAVENI1);
				}
				else if (stav==2)
				{
					x=100;
					y=400;
					automat();
					send(1,NASTAVENI1);
				}
				else if (stav==5)
				{
					x=700;
					y=300;
					automat();
					send(1,NASTAVENI1);
				}
				else if (stav==8)
				{
					x=500;
					y=400;
					automat();
					send(1,NASTAVENI1);
				}
				else
				{
					ethernet=8;
					send(1,NYNI_NELZE);
				}
			}
			else if (ethernetCommand[11]=='R')//I/10:KLIK_RESET
			{
				ethernet=8;				
				if (stav==1)
				{
					x=700;
					y=400;
					automat();
					send(1,LOAD_SCREEN);
				}
				else
				{
					ethernet=8;
					send(1,NYNI_NELZE);
				}
			}
			else if (ethernetCommand[10]=='Z')//I/9:KLIK_ZPET
			{
				ethernet=8;
				if (stav==2)//ZPET DO HLAVNIHO MENU
				{
					x=700;
					y=400;
					automat();
					send(1,LOAD_SCREEN);
				}
				else if (stav==5)//ZPET DO HLAVNIHO MENU
				{
					x=700;
					y=400;
					automat();
					send(1,LOAD_SCREEN);
				}
				else if ((stav==7) || (stav==8))
				{
					x=700;
					y=400;
					automat();
					if (MenuToNast==0)//ZPET DO MERENI STOP
					{
						send(1,MERENI_STOP);
					}
					else if (MenuToNast==1)//ZPET DO HLAVNIHO MENU
					{
						send(1,LOAD_SCREEN);
					}
				}
				else
				{
					pom=1;
					ethernet=8;
					send(1,NYNI_NELZE);
				}
			}
			else if ((ethernetCommand[11]=='S')&&(ethernetCommand[13]=='A'))//I/10:KLIK_START
			{
				ethernet=8;
				if (stav==5)
				{
					x=700;
					y=50;
					automat();
					send(1,MERENI_RUN);
				}
				else
				{
					ethernet=8;
					send(1,NYNI_NELZE);
				}
			}
			else if ((ethernetCommand[10]=='S')&&(ethernetCommand[12]=='O'))//I/9:KLIK_STOP
			{
				ethernet=8;
				if (stav==6)
				{
					x=700;
					y=150;
					automat();
					send(1,MERENI_STOP);
				}
				else
				{
					ethernet=8;
					send(1,NYNI_NELZE);
				}
			}
			else if (ethernetCommand[11]=='U')//I/9:KLIK_TURB
			{
				ethernet=8;
				if (stav==7)
				{
					x=270;
					y=150;
					automat();
					send(1,TURBIDIMETR);
				}
				else
				{
					ethernet=8;
					send(1,NYNI_NELZE);
				}
			}
			else if (ethernetCommand[12]=='F')//I/8:KLIK_NEF
			{
				ethernet=8;
				if (stav==7)
				{
					x=270;
					y=250;
					automat();
					send(1,NEFELOMETR);
				}
				else
				{
					ethernet=8;
					send(1,NYNI_NELZE);
				}
			}
			else if (ethernetCommand[10]=='P')//I/8:KLIK_POM
			{
				ethernet=8;
				if (stav==7)
				{
					x=270;
					y=300;
					automat();
					send(1,POMEROVY);
				}
				else
				{
					ethernet=8;
					send(1,NYNI_NELZE);
				}
			}
			else if (ethernetCommand[14]=='M')//I/22:HODNOTA_MEZ=XXXXXXXXXX
			{
				ethernet=8;
				if (stav==7)
				{
					//prevod retezce na cislo a na retezec pro vypis na displej
					rad=1;
					Hmez=0;
					for (int i=27;i>17;i--)
					{
						if ((ethernetCommand[i]>='0')&&(ethernetCommand[i]<='9'))
						{
							hodnotaMez[i-3]=ethernetCommand[i];
							Hmez+=decode(ethernetCommand[i],rad);
							rad*=10;
						}
					}
					//konec prevodu
					automat();
					for (int i=18;i<=27;i++)
					{
						if ((ethernetCommand[i]>='0')&&(ethernetCommand[i]<='9'))
						{
							HODNOTA_MEZ[11+i]=ethernetCommand[i];
						}
					}
					send(1,HODNOTA_MEZ);
				}
				else
				{
					ethernet=8;
					send(1,NYNI_NELZE);
				}
			}
			else if (ethernetCommand[14]=='C')//I/22:HODNOTA_CAS=XXXXXXXXXX
			{
				ethernet=8;
				if (stav==7)
				{
					//prevod retezce na cislo a na retezec pro vypis na displej
					rad=1;
					Cas=0;
					for (int i=27;i>17;i--)
					{
						if ((ethernetCommand[i]>='0')&&(ethernetCommand[i]<='9'))
						{
							hodnotaCas[i-3]=ethernetCommand[i];
							Cas+=decode(ethernetCommand[i],rad);
							rad*=10;
						}
					}
					//konec prevodu
					automat();
					for (int i=18;i<=27;i++)
					{
						if ((ethernetCommand[i]>='0')&&(ethernetCommand[i]<='9'))
						{
							HODNOTA_CAS[11+i]=ethernetCommand[i];
						}
					}
					send(1,HODNOTA_CAS);
				}
				else
				{
					ethernet=8;
					send(1,NYNI_NELZE);
				}
			}
			else if ((ethernetCommand[11]=='N')&&(ethernetCommand[20]=='2'))//I/15:KLIK_NASTAVENI2
			{
				ethernet=8;
				if (stav==7)
				{
					x=500;
					y=400;
					automat();
					send(1,NASTAVENI2);
				}
				else
				{
					ethernet=8;
					send(1,NYNI_NELZE);
				}
			}
			else if ((ethernetCommand[6]=='A')&&(ethernetCommand[13]=='D')&&(ethernetCommand[15]=='1'))//I/10:ACTIVE_DO1
			{
				ethernet=8;
				if (stav==8)
				{
					if (DO1!=1)
					{
						x=270;
						y=150;
						automat();
					}
					send(1,STAV_DO1_A);
				}
				else
				{
					ethernet=8;
					send(1,NYNI_NELZE);
				}
			}
			else if ((ethernetCommand[6]=='A')&&(ethernetCommand[13]=='D')&&(ethernetCommand[15]=='2'))//I/10:ACTIVE_DO2
			{
				ethernet=8;
				if (stav==8)
				{
					if (DO2!=1)
					{
						x=270;
						y=220;
						automat();
					}
					send(1,STAV_DO2_A);
				}
				else
				{
					ethernet=8;
					send(1,NYNI_NELZE);
				}
			}
			else if ((ethernetCommand[6]=='A')&&(ethernetCommand[13]=='A')&&(ethernetCommand[15]=='1'))//I/10:ACTIVE_AO1
			{
				ethernet=8;
				if (stav==8)
				{
					if (AO1!=1)
					{
						x=270;
						y=300;
						automat();
					}
					send(1,STAV_AO1_A);
				}
				else
				{
					ethernet=8;
					send(1,NYNI_NELZE);
				}
			}
			else if ((ethernetCommand[6]=='A')&&(ethernetCommand[13]=='A')&&(ethernetCommand[15]=='2'))//I/10:ACTIVE_AO2
			{
				ethernet=8;
				if (stav==8)
				{
					if (AO2!=1)
					{
						x=270;
						y=350;
						automat();
					}
					send(1,STAV_AO2_A);
				}
				else
				{
					ethernet=8;
					send(1,NYNI_NELZE);
				}
			}
			else if ((ethernetCommand[6]=='D')&&(ethernetCommand[15]=='D')&&(ethernetCommand[17]=='1'))//I/12:DEACTIVE_DO1
			{
				ethernet=8;
				if (stav==8)
				{
					if (DO1==1)
					{
						x=270;
						y=150;
						automat();
					}
					send(1,STAV_DO1_P);
				}
				else
				{
					ethernet=8;
					send(1,NYNI_NELZE);
				}
			}
			else if ((ethernetCommand[6]=='D')&&(ethernetCommand[15]=='D')&&(ethernetCommand[17]=='2'))//I/12:DEACTIVE_DO2
			{
				ethernet=8;
				if (stav==8)
				{
					if (DO2==1)
					{
						x=270;
						y=220;
						automat();
					}
					send(1,STAV_DO2_P);
				}
				else
				{
					ethernet=8;
					send(1,NYNI_NELZE);
				}
			}
			else if ((ethernetCommand[6]=='D')&&(ethernetCommand[15]=='A')&&(ethernetCommand[17]=='1'))//I/12:DEACTIVE_AO1
			{
				ethernet=8;
				if (stav==8)
				{
					if (AO1==1)
					{
						x=270;
						y=300;
						automat();
					}
					send(1,STAV_AO1_P);
				}
				else
				{
					ethernet=8;
					send(1,NYNI_NELZE);
				}
			}
			else if ((ethernetCommand[6]=='D')&&(ethernetCommand[15]=='A')&&(ethernetCommand[17]=='2'))//I/12:DEACTIVE_AO2
			{
				ethernet=8;
				if (stav==8)
				{
					if (AO2==1)
					{
						x=270;
						y=350;
						automat();
					}
					send(1,STAV_AO2_P);
				}
				else
				{
					ethernet=8;
					send(1,NYNI_NELZE);
				}
			}
			else if ((ethernetCommand[6]=='H')&&(ethernetCommand[14]=='D')&&(ethernetCommand[16]=='1'))//I/13:HODNOTA_DO1=X
			{
				ethernet=8;
				if (stav==8)
				{
					//prevod retezce na cislo a na retezec pro vypis na displej
					if (ethernetCommand[18]=='0')
					{
						HDO1=0;
						hodnotaDO1M[15]='0';
						hodnotaDO1N[15]='0';
					}
					else if (ethernetCommand[18]=='1')
					{
						HDO1=1;
						hodnotaDO1M[15]='1';
						hodnotaDO1N[15]='1';
					}
					//konec prevodu
					automat();
					if ((ethernetCommand[18]>='0')&&(ethernetCommand[18]<='1'))
					{
						HODNOTA_DO1[29]=ethernetCommand[18];
					}
					send(1,HODNOTA_DO1);
				}
				else
				{
					ethernet=8;
					send(1,NYNI_NELZE);
				}
			}
			else if ((ethernetCommand[6]=='H')&&(ethernetCommand[14]=='D')&&(ethernetCommand[16]=='2'))//I/13:HODNOTA_DO2=X
			{
				ethernet=8;
				if (stav==8)
				{
					//prevod retezce na cislo a na retezec pro vypis na displej
					if (ethernetCommand[18]=='0')
					{
						HDO2=0;
						hodnotaDO2M[15]='0';
						hodnotaDO2N[15]='0';
					}
					else if (ethernetCommand[18]=='1')
					{
						HDO2=1;
						hodnotaDO2M[15]='1';
						hodnotaDO2N[15]='1';
					}
					//konec prevodu
					automat();
					if ((ethernetCommand[18]>='0')&&(ethernetCommand[18]<='1'))
					{
						HODNOTA_DO2[29]=ethernetCommand[18];
					}
					send(1,HODNOTA_DO2);
				}
				else
				{
					ethernet=8;
					send(1,NYNI_NELZE);
				}
			}
			else if ((ethernetCommand[6]=='H')&&(ethernetCommand[14]=='A')&&(ethernetCommand[16]=='1'))//I/22:HODNOTA_AO1=XXXXXXXXXX
			{
				ethernet=8;
				if (stav==8)
				{
					//prevod retezce na cislo a na retezec pro vypis na displej
					char carka=0;
					rad=1;
					a=0;
					b=0;
					c=0;
					for (int i=18;i<=27;i++)
					{
						if (((ethernetCommand[i]>='0')&&(ethernetCommand[i]<='9'))||(ethernetCommand[i]==','))
						{
							hodnotaAO1M[i-3]=ethernetCommand[i];
							hodnotaAO1N[i-3]=ethernetCommand[i];
							if (ethernetCommand[i]==',')
							{
								carka=i;
							}
						}
					}
					if (carka==0)
					{
						b=0;
						c=0;
					}
					else
					{
						if (ethernetCommand[carka+1]<='2')
						{
							b=0;
							c=0;
						}
						else if ((ethernetCommand[carka+1]>'2')&&(ethernetCommand[carka+1]<='7'))
						{
							b=5;
							c=0;
						}
						else if (ethernetCommand[carka+1]>'7')
						{
							b=0;
							c=1;
						}
					}
					
					for (int i = carka-1;i>=18;i--)
					{
						a+=decode(ethernetCommand[i],rad);
						rad*=10;
					}
					if (a>=20)
					{
						a=20;
						b=0;
						c=0;
					}
					HAO1=12*a+b*6/5+c*6+15;
					//konec prevodu
					automat();
					for (int i=18;i<=27;i++)
					{
						if (((ethernetCommand[i]>='0')&&(ethernetCommand[i]<='9'))||(ethernetCommand[i]==','))
						{
							HODNOTA_AO1[11+i]=ethernetCommand[i];
						}
					}
					send(1,HODNOTA_AO1);
				}
				else
				{
					ethernet=8;
					send(1,NYNI_NELZE);
				}
			}
			else if ((ethernetCommand[6]=='H')&&(ethernetCommand[14]=='A')&&(ethernetCommand[16]=='2'))//I/22:HODNOTA_AO2=XXXXXXXXXX
			{
				ethernet=8;
				if (stav==8)
				{
					//prevod retezce na cislo a na retezec pro vypis na displej
					char carka=0;
					rad=1;
					a=0;
					b=0;
					c=0;
					for (int i=18;i<=27;i++)
					{
						if (((ethernetCommand[i]>='0')&&(ethernetCommand[i]<='9'))||(ethernetCommand[i]==','))
						{
							hodnotaAO2M[i-3]=ethernetCommand[i];
							hodnotaAO2N[i-3]=ethernetCommand[i];
							if (ethernetCommand[i]==',')
							{
								carka=i;
							}
						}
					}
					if (carka==0)
					{
						b=0;
						c=0;
					}
					else
					{
						if (ethernetCommand[carka+1]<='2')
						{
							b=0;
							c=0;
						}
						else if ((ethernetCommand[carka+1]>'2')&&(ethernetCommand[carka+1]<='7'))
						{
							b=5;
							c=0;
						}
						else if (ethernetCommand[carka+1]>'7')
						{
							b=0;
							c=1;
						}
					}
					
					for (int i = carka-1;i>=18;i--)
					{
						a+=decode(ethernetCommand[i],rad);
						rad*=10;
					}
					if (a>=20)
					{
						a=20;
						b=0;
						c=0;
					}
					HAO2=12*a+b*6/5+c*6+15;
					//konec prevodu
					automat();
					for (int i=18;i<=27;i++)
					{
						if (((ethernetCommand[i]>='0')&&(ethernetCommand[i]<='9'))||(ethernetCommand[i]==','))
						{
							HODNOTA_AO2[11+i]=ethernetCommand[i];
						}
					}
					send(1,HODNOTA_AO2);
				}
				else
				{
					ethernet=8;
					send(1,NYNI_NELZE);
				}
			}
			else if ((ethernetCommand[6]=='R')&&(ethernetCommand[15]=='-'))//I/13:ROZSAH=25-200
			{
				ethernet=8;
				if (stav==7)
				{
					x=270;
					y=400;
					automat();
					send(1,ROZSAH1);
				}
				else
				{
					ethernet=8;
					send(1,NYNI_NELZE);
				}
			}
			else if ((ethernetCommand[6]=='R')&&(ethernetCommand[16]=='-'))//I/14:ROZSAH=200-500
			{
				ethernet=8;
				if (stav==7)
				{
					x=270;
					y=450;
					automat();
					send(1,ROZSAH2);
				}
				else
				{
					ethernet=8;
					send(1,NYNI_NELZE);
				}
			}
			else if (ethernetCommand[8]=='?')//I/5:STAV?
			{
				send(1,HODNOTY_ALL);
			}
		}
		else if ((ethernetCommand[1]=='I')&&(ethernetCommand[3]=='E'))//chyba
		{
			ethernet=4;
			if (listen_socket==10)
			{
				send(1,"AT+iSCLS:010\r\n");
			}
			else if (listen_socket==11)
			{
				send(1,"AT+iSCLS:011\r\n");
			}
		}
		else
		{
			ethernet=8;
			send(1,NEZNAMY_PRIKAZ);
		}
	}
	else if (ethernet==8)//odeslala se odpoved?
	{
		ethernet=32;
		indE=0;
		UCSR1B |= (1<<RXEN1)|(1<<RXCIE1);
		UCSR0B |= (1<<RXEN0)|(1<<RXCIE0);
		EIMSK|=(1<<INT7);//povoleni ext preruseni na pinu INT7		
	}
	else if (ethernet==4)
	{
		if ((ethernetCommand[1]=='I')&&(ethernetCommand[3]=='O'))//socket se uspesne uzavrel
		{
			ethernet=128;
			send(1,"AT+iLTCP:20100,1\r\n");
		}
		else if ((ethernetCommand[1]=='I')&&(ethernetCommand[3]=='E'))//NE
		{
			if (listen_socket==10)
			{
				send(1,"AT+iSCLS:010\r\n");
			}
			else if (listen_socket==11)
			{
				send(1,"AT+iSCLS:011\r\n");
			}
		}
	}
// 	else if (ethernet==2)
// 	{
// 	}
// 	else if (ethernet==1)
// 	{
// 	}
	else//pokud jeste nejsem v zadnem ze stavu tzn.: jeste nbyl odeslan AT+i prikaz na otevreni LISTEN TCP SOCKETU
	{
		send(1,"AT+iLTCP:20100,1\r\n");
		ethernet=128;
	}
}
//__________________________________________________________________________________________________________________________
void prumernaHodnota()
{
	unsigned int soucet = 0;
	unsigned int cCisel =0;
	for (int i=0;i<600;i++)
	{
		if (data[i]!=0)
		{
			soucet+=data[i];
			cCisel++;
		}
	}
	prumer = soucet/cCisel;
}
void vyhodnoceniAD()
{
	if (rozsah==1)
	{
		if (prumer>841)	prumer=0;
		else if ((prumer<=841) && (prumer>=828))	prumer=25;
		else if ((prumer<=827) && (prumer>=817))	prumer=26;
		else if ((prumer<=816) && (prumer>=806))	prumer=27;
		else if ((prumer<=805) && (prumer>=796))	prumer=28;
		else if ((prumer<=795) && (prumer>=787))	prumer=29;
		else if ((prumer<=787) && (prumer>=779))	prumer=30;
		else if ((prumer<=778) && (prumer>=771))	prumer=31;
		else if ((prumer<=770) && (prumer>=763))	prumer=32;
		else if ((prumer<=762) && (prumer>=755))	prumer=33;
		else if ((prumer<=754) && (prumer>=748))	prumer=34;
		else if ((prumer<=747) && (prumer>=741))	prumer=35;
		else if ((prumer<=740) && (prumer>=735))	prumer=36;
		else if ((prumer<=734) && (prumer>=728))	prumer=37;
		else if ((prumer<=727) && (prumer>=722))	prumer=38;
		else if ((prumer<=721) && (prumer>=716))	prumer=39;
		else if ((prumer<=715) && (prumer>=710))	prumer=40;
		else if ((prumer<=709) && (prumer>=705))	prumer=41;
		else if ((prumer<=704) && (prumer>=699))	prumer=42;
		else if ((prumer<=698) && (prumer>=694))	prumer=43;
		else if ((prumer<=693) && (prumer>=688))	prumer=44;
		else if ((prumer<=687) && (prumer>=683))	prumer=45;
		else if ((prumer<=682) && (prumer>=678))	prumer=46;
		else if ((prumer<=677) && (prumer>=673))	prumer=47;
		else if ((prumer<=672) && (prumer>=668))	prumer=48;
		else if ((prumer<=667) && (prumer>=663))	prumer=49;
		else if ((prumer<=662) && (prumer>=658))	prumer=50;
		else if ((prumer<=657) && (prumer>=654))	prumer=51;
		else if ((prumer<=653) && (prumer>=649))	prumer=52;
		else if ((prumer<=648) && (prumer>=645))	prumer=53;
		else if ((prumer<=644) && (prumer>=640))	prumer=54;
		else if ((prumer<=639) && (prumer>=636))	prumer=55;
		else if ((prumer<=635) && (prumer>=631))	prumer=56;
		else if ((prumer<=630) && (prumer>=627))	prumer=57;
		else if ((prumer<=626) && (prumer>=623))	prumer=58;
		else if ((prumer<=622) && (prumer>=619))	prumer=59;
		else if ((prumer<=618) && (prumer>=615))	prumer=60;
		else if ((prumer<=614) && (prumer>=611))	prumer=61;
		else if ((prumer<=610) && (prumer>=607))	prumer=62;
		else if ((prumer<=606) && (prumer>=603))	prumer=63;
		else if ((prumer<=602) && (prumer>=599))	prumer=64;
		else if ((prumer<=598) && (prumer>=595))	prumer=65;
		else if ((prumer<=594) && (prumer>=591))	prumer=66;
		else if ((prumer<=590) && (prumer>=587))	prumer=67;
		else if ((prumer<=586) && (prumer>=584))	prumer=68;
		else if ((prumer<=583) && (prumer>=580))	prumer=69;
		else if ((prumer<=579) && (prumer>=576))	prumer=70;
		else if ((prumer<=575) && (prumer>=573))	prumer=71;
		else if ((prumer<=572) && (prumer>=569))	prumer=72;
		else if ((prumer<=568) && (prumer>=566))	prumer=73;
		else if ((prumer<=565) && (prumer>=562))	prumer=74;
		else if ((prumer<=561) && (prumer>=559))	prumer=75;
		else if ((prumer<=558) && (prumer>=555))	prumer=76;
		else if ((prumer<=554) && (prumer>=552))	prumer=77;
		else if ((prumer<=551) && (prumer>=548))	prumer=78;
		else if ((prumer<=547) && (prumer>=545))	prumer=79;
		else if ((prumer<=544) && (prumer>=542))	prumer=80;
		else if ((prumer<=541) && (prumer>=538))	prumer=81;
		else if ((prumer<=537) && (prumer>=535))	prumer=82;
		else if ((prumer<=534) && (prumer>=532))	prumer=83;
		else if ((prumer<=531) && (prumer>=529))	prumer=84;
		else if ((prumer<=528) && (prumer>=526))	prumer=85;
		else if ((prumer<=525) && (prumer>=522))	prumer=86;
		else if ((prumer<=521) && (prumer>=519))	prumer=87;
		else if ((prumer<=518) && (prumer>=516))	prumer=88;
		else if ((prumer<=515) && (prumer>=513))	prumer=89;
		else if ((prumer<=512) && (prumer>=510))	prumer=90;
		else if ((prumer<=509) && (prumer>=507))	prumer=91;
		else if ((prumer<=506) && (prumer>=504))	prumer=92;
		else if ((prumer<=503) && (prumer>=501))	prumer=93;
		else if ((prumer<=500) && (prumer>=498))	prumer=94;
		else if ((prumer<=497) && (prumer>=495))	prumer=95;
		else if ((prumer<=494) && (prumer>=492))	prumer=96;
		else if ((prumer<=491) && (prumer>=489))	prumer=97;
		else if ((prumer<=488) && (prumer>=486))	prumer=98;
		else if ((prumer<=485) && (prumer>=483))	prumer=99;
		else if ((prumer<=482) && (prumer>=480))	prumer=100;
		else if ((prumer<=479) && (prumer>=478))	prumer=101;
		else if ((prumer<=477) && (prumer>=475))	prumer=102;
		else if ((prumer<=474) && (prumer>=472))	prumer=103;
		else if ((prumer<=471) && (prumer>=469))	prumer=104;
		else if ((prumer<=468) && (prumer>=466))	prumer=105;
		else if ((prumer<=465) && (prumer>=464))	prumer=106;
		else if ((prumer<=463) && (prumer>=461))	prumer=107;
		else if ((prumer<=460) && (prumer>=458))	prumer=108;
		else if ((prumer<=457) && (prumer>=455))	prumer=109;
		else if ((prumer<=454) && (prumer>=453))	prumer=110;
		else if ((prumer<=452) && (prumer>=450))	prumer=111;
		else if ((prumer<=449) && (prumer>=447))	prumer=112;
		else if ((prumer<=446) && (prumer>=445))	prumer=113;
		else if ((prumer<=444) && (prumer>=442))	prumer=114;
		else if ((prumer<=441) && (prumer>=439))	prumer=115;
		else if ((prumer<=438) && (prumer>=437))	prumer=116;
		else if ((prumer<=436) && (prumer>=434))	prumer=117;
		else if ((prumer<=433) && (prumer>=432))	prumer=118;
		else if ((prumer<=431) && (prumer>=429))	prumer=119;
		else if ((prumer<=428) && (prumer>=426))	prumer=120;
		else if ((prumer<=425) && (prumer>=424))	prumer=121;
		else if ((prumer<=423) && (prumer>=421))	prumer=122;
		else if ((prumer<=420) && (prumer>=419))	prumer=123;
		else if ((prumer<=418) && (prumer>=416))	prumer=124;
		else if ((prumer<=415) && (prumer>=414))	prumer=125;
		else if ((prumer<=413) && (prumer>=411))	prumer=126;
		else if ((prumer<=410) && (prumer>=409))	prumer=127;
		else if ((prumer<=408) && (prumer>=406))	prumer=128;
		else if ((prumer<=405) && (prumer>=404))	prumer=129;
		else if ((prumer<=403) && (prumer>=401))	prumer=130;
		else if ((prumer<=400) && (prumer>=399))	prumer=131;
		else if ((prumer<=398) && (prumer>=397))	prumer=132;
		else if ((prumer<=396) && (prumer>=395))	prumer=133;
		else if ((prumer<=394) && (prumer>=392))	prumer=134;
		else if ((prumer<=391) && (prumer>=389))	prumer=135;
		else if ((prumer<=388) && (prumer>=387))	prumer=136;
		else if ((prumer<=387) && (prumer>=385))	prumer=137;
		else if ((prumer<=384) && (prumer>=382))	prumer=138;
		else if ((prumer<=381) && (prumer>=380))	prumer=139;
		else if ((prumer<=379) && (prumer>=378))	prumer=140;
		else if ((prumer<=377) && (prumer>=375))	prumer=141;
		else if ((prumer<=374) && (prumer>=373))	prumer=142;
		else if ((prumer<=372) && (prumer>=371))	prumer=143;
		else if ((prumer<=370) && (prumer>=368))	prumer=144;
		else if ((prumer<=367) && (prumer>=366))	prumer=145;
		else if ((prumer<=365) && (prumer>=364))	prumer=146;
		else if ((prumer<=363) && (prumer>=361))	prumer=147;
		else if ((prumer<=360) && (prumer>=359))	prumer=148;
		else if ((prumer<=358) && (prumer>=357))	prumer=149;
		else if ((prumer<=356) && (prumer>=355))	prumer=150;
		else if ((prumer<=354) && (prumer>=352))	prumer=151;
		else if ((prumer<=351) && (prumer>=350))	prumer=152;
		else if ((prumer<=349) && (prumer>=348))	prumer=153;
		else if ((prumer<=347) && (prumer>=346))	prumer=154;
		else if ((prumer<=345) && (prumer>=344))	prumer=155;
		else if ((prumer<=343) && (prumer>=341))	prumer=156;
		else if ((prumer<=340) && (prumer>=339))	prumer=157;
		else if ((prumer<=338) && (prumer>=337))	prumer=158;
		else if ((prumer<=336) && (prumer>=335))	prumer=159;
		else if ((prumer<=334) && (prumer>=333))	prumer=160;
		else if ((prumer<=332) && (prumer>=331))	prumer=161;
		else if ((prumer<=330) && (prumer>=328))	prumer=162;
		else if ((prumer<=327) && (prumer>=326))	prumer=163;
		else if ((prumer<=325) && (prumer>=324))	prumer=164;
		else if ((prumer<=323) && (prumer>=322))	prumer=165;
		else if ((prumer<=321) && (prumer>=320))	prumer=166;
		else if ((prumer<=319) && (prumer>=318))	prumer=167;
		else if ((prumer<=317) && (prumer>=316))	prumer=168;
		else if ((prumer<=315) && (prumer>=314))	prumer=169;
		else if ((prumer<=313) && (prumer>=312))	prumer=170;
		else if ((prumer<=311) && (prumer>=309))	prumer=171;
		else if ((prumer<=308) && (prumer>=307))	prumer=172;
		else if ((prumer<=306) && (prumer>=305))	prumer=173;
		else if ((prumer<=304) && (prumer>=303))	prumer=174;
		else if ((prumer<=302) && (prumer>=301))	prumer=175;
		else if ((prumer<=300) && (prumer>=299))	prumer=176;
		else if ((prumer<=298) && (prumer>=297))	prumer=177;
		else if ((prumer<=296) && (prumer>=295))	prumer=178;
		else if ((prumer<=294) && (prumer>=293))	prumer=179;
		else if ((prumer<=292) && (prumer>=291))	prumer=180;
		else if ((prumer<=290) && (prumer>=289))	prumer=181;
		else if ((prumer<=288) && (prumer>=287))	prumer=182;
		else if ((prumer<=286) && (prumer>=285))	prumer=183;
		else if ((prumer<=284) && (prumer>=283))	prumer=184;
		else if ((prumer<=282) && (prumer>=281))	prumer=185;
		else if ((prumer<=280) && (prumer>=279))	prumer=186;
		else if ((prumer<=278) && (prumer>=277))	prumer=187;
		else if ((prumer<=276) && (prumer>=275))	prumer=188;
		else if ((prumer<=274) && (prumer>=273))	prumer=189;
		else if ((prumer<=272) && (prumer>=271))	prumer=190;
		else if ((prumer<=270) && (prumer>=269))	prumer=191;
		else if ((prumer<=268) && (prumer>=267))	prumer=192;
		else if ((prumer<=266) && (prumer>=265))	prumer=193;
		else if ((prumer<=264) && (prumer>=263))	prumer=194;
		else if ((prumer<=262) && (prumer>=261))	prumer=195;
		else if ((prumer<=260) && (prumer>=259))	prumer=196;
		else if ((prumer<=258) && (prumer>=258))	prumer=197;
		else if ((prumer<=257) && (prumer>=256))	prumer=198;
		else if ((prumer<=255) && (prumer>=254))	prumer=199;
		else if ((prumer<=253) && (prumer>=252))	prumer=200;
		else if ((prumer<=251) && (prumer>=250))	prumer=201;
		else if ((prumer<=249) && (prumer>=248))	prumer=202;
		else if ((prumer<=247) && (prumer>=246))	prumer=203;
		else if ((prumer<=245) && (prumer>=244))	prumer=204;
		else if ((prumer<=243) && (prumer>=242))	prumer=205;
		else if ((prumer<=241) && (prumer>=240))	prumer=206;
		else if ((prumer<=239) && (prumer>=239))	prumer=207;
		else if ((prumer<=238) && (prumer>=237))	prumer=208;
		else if ((prumer<=236) && (prumer>=235))	prumer=209;
		else if ((prumer<=234) && (prumer>=233))	prumer=210;
		else if ((prumer<=232) && (prumer>=231))	prumer=211;
		else if ((prumer<=230) && (prumer>=229))	prumer=212;
		else if ((prumer<=228) && (prumer>=228))	prumer=213;
		else if ((prumer<=227) && (prumer>=226))	prumer=214;
		else if ((prumer<=225) && (prumer>=224))	prumer=215;
		else if ((prumer<=223) && (prumer>=222))	prumer=216;
		else if ((prumer<=221) && (prumer>=220))	prumer=217;
		else if ((prumer<=219) && (prumer>=218))	prumer=218;
		else if ((prumer<=217) && (prumer>=217))	prumer=219;
		else if ((prumer<=216) && (prumer>=215))	prumer=220;
		else if (prumer<215)	prumer=999;
	}
	else if (rozsah==2)
	{
		if (prumer>550)	prumer=0;
		else if ((prumer<=550) && (prumer>=523))	prumer=210;
		else if ((prumer<=522) && (prumer>=504))	prumer=212;
		else if ((prumer<=503) && (prumer>=490))	prumer=214;
		else if ((prumer<=489) && (prumer>=479))	prumer=216;
		else if ((prumer<=478) && (prumer>=469))	prumer=218;
		else if ((prumer<=468) && (prumer>=461))	prumer=220;
		else if ((prumer<=460) && (prumer>=453))	prumer=222;
		else if ((prumer<=452) && (prumer>=445))	prumer=224;
		else if ((prumer<=444) && (prumer>=439))	prumer=226;
		else if ((prumer<=438) && (prumer>=432))	prumer=228;
		else if ((prumer<=431) && (prumer>=426))	prumer=230;
		else if ((prumer<=425) && (prumer>=420))	prumer=232;
		else if ((prumer<=419) && (prumer>=415))	prumer=234;
		else if ((prumer<=414) && (prumer>=409))	prumer=236;
		else if ((prumer<=408) && (prumer>=404))	prumer=238;
		else if ((prumer<=403) && (prumer>=399))	prumer=240;
		else if ((prumer<=398) && (prumer>=394))	prumer=242;
		else if ((prumer<=393) && (prumer>=390))	prumer=244;
		else if ((prumer<=389) && (prumer>=385))	prumer=246;
		else if ((prumer<=384) && (prumer>=381))	prumer=248;
		else if ((prumer<=380) && (prumer>=376))	prumer=250;
		else if ((prumer<=375) && (prumer>=372))	prumer=252;
		else if ((prumer<=371) && (prumer>=368))	prumer=254;
		else if ((prumer<=367) && (prumer>=364))	prumer=256;
		else if ((prumer<=363) && (prumer>=360))	prumer=258;
		else if ((prumer<=359) && (prumer>=356))	prumer=260;
		else if ((prumer<=355) && (prumer>=352))	prumer=262;
		else if ((prumer<=351) && (prumer>=349))	prumer=264;
		else if ((prumer<=348) && (prumer>=345))	prumer=266;
		else if ((prumer<=344) && (prumer>=341))	prumer=268;
		else if ((prumer<=340) && (prumer>=338))	prumer=270;
		else if ((prumer<=337) && (prumer>=334))	prumer=272;
		else if ((prumer<=333) && (prumer>=331))	prumer=274;
		else if ((prumer<=330) && (prumer>=328))	prumer=276;
		else if ((prumer<=327) && (prumer>=324))	prumer=278;
		else if ((prumer<=323) && (prumer>=321))	prumer=280;
		else if ((prumer<=320) && (prumer>=318))	prumer=282;
		else if ((prumer<=317) && (prumer>=315))	prumer=284;
		else if ((prumer<=314) && (prumer>=311))	prumer=286;
		else if ((prumer<=310) && (prumer>=308))	prumer=288;
		else if ((prumer<=307) && (prumer>=305))	prumer=290;
		else if ((prumer<=304) && (prumer>=302))	prumer=292;
		else if ((prumer<=301) && (prumer>=299))	prumer=294;
		else if ((prumer<=298) && (prumer>=296))	prumer=296;
		else if ((prumer<=395) && (prumer>=293))	prumer=298;
		else if ((prumer<=292) && (prumer>=291))	prumer=300;
		else if ((prumer<=290) && (prumer>=288))	prumer=302;
		else if ((prumer<=287) && (prumer>=285))	prumer=304;
		else if ((prumer<=284) && (prumer>=282))	prumer=306;
		else if ((prumer<=281) && (prumer>=279))   prumer=308;
		else if ((prumer<=278) && (prumer>=277))	prumer=310;
		else if ((prumer<=276) && (prumer>=274))	prumer=312;
		else if ((prumer<=273) && (prumer>=271))	prumer=314;
		else if ((prumer<=270) && (prumer>=269))	prumer=316;
		else if ((prumer<=268) && (prumer>=266))	prumer=318;
		else if ((prumer<=265) && (prumer>=263))	prumer=320;
		else if ((prumer<=262) && (prumer>=261))	prumer=322;
		else if ((prumer<=260) && (prumer>=258))	prumer=324;
		else if ((prumer<=257) && (prumer>=256))	prumer=326;
		else if ((prumer<=255) && (prumer>=253))	prumer=328;
		else if ((prumer<=252) && (prumer>=251))	prumer=330;
		else if ((prumer<=250) && (prumer>=248))	prumer=332;
		else if ((prumer<=247) && (prumer>=246))	prumer=334;
		else if ((prumer<=245) && (prumer>=243))	prumer=336;
		else if ((prumer<=242) && (prumer>=241))	prumer=338;
		else if ((prumer<=240) && (prumer>=238))	prumer=340;
		else if ((prumer<=237) && (prumer>=236))	prumer=342;
		else if ((prumer<=235) && (prumer>=234))	prumer=344;
		else if ((prumer<=233) && (prumer>=231))	prumer=346;
		else if ((prumer<=230) && (prumer>=229))	prumer=348;
		else if ((prumer<=228) && (prumer>=227))	prumer=350;
		else if ((prumer<=226) && (prumer>=224))	prumer=352;
		else if ((prumer<=223) && (prumer>=222))	prumer=354;
		else if ((prumer<=221) && (prumer>=220))	prumer=356;
		else if ((prumer<=219) && (prumer>=218))	prumer=358;
		else if ((prumer<=217) && (prumer>=215))	prumer=360;
		else if ((prumer<=214) && (prumer>=213))	prumer=362;
		else if ((prumer<=212) && (prumer>=211))	prumer=364;
		else if ((prumer<=210) && (prumer>=209))	prumer=366;
		else if ((prumer<=208) && (prumer>=207))	prumer=368;
		else if ((prumer<=206) && (prumer>=204))	prumer=370;
		else if ((prumer<=203) && (prumer>=202))	prumer=372;
		else if ((prumer<=201) && (prumer>=200))	prumer=374;
		else if ((prumer<=199) && (prumer>=198))	prumer=376;
		else if ((prumer<=197) && (prumer>=196))	prumer=378;
		else if ((prumer<=195) && (prumer>=194))	prumer=380;
		else if ((prumer<=193) && (prumer>=192))	prumer=382;
		else if ((prumer<=191) && (prumer>=190))	prumer=384;
		else if ((prumer<=189) && (prumer>=188))	prumer=386;
		else if ((prumer<=187) && (prumer>=185))	prumer=388;
		else if ((prumer<=184) && (prumer>=183))	prumer=390;
		else if ((prumer<=182) && (prumer>=181))	prumer=392;
		else if ((prumer<=180) && (prumer>=179))	prumer=394;
		else if ((prumer<=178) && (prumer>=177))	prumer=396;
		else if ((prumer<=176) && (prumer>=175))	prumer=398;
		else if ((prumer<=174) && (prumer>=173))	prumer=400;
		else if ((prumer<=172) && (prumer>=171))	prumer=402;
		else if ((prumer<=170) && (prumer>=169))	prumer=404;
		else if ((prumer<=168) && (prumer>=168))	prumer=406;
		else if ((prumer<=167) && (prumer>=166))	prumer=408;
		else if ((prumer<=165) && (prumer>=164))	prumer=410;
		else if ((prumer<=163) && (prumer>=162))	prumer=412;
		else if ((prumer<=161) && (prumer>=160))	prumer=414;
		else if ((prumer<=159) && (prumer>=158))	prumer=416;
		else if ((prumer<=157) && (prumer>=156))	prumer=418;
		else if ((prumer<=155) && (prumer>=154))	prumer=420;
		else if ((prumer<=153) && (prumer>=152))	prumer=422;
		else if ((prumer<=151) && (prumer>=150))	prumer=424;
		else if ((prumer<=149) && (prumer>=149))	prumer=426;
		else if ((prumer<=148) && (prumer>=147))	prumer=428;
		else if ((prumer<=146) && (prumer>=145))	prumer=430;
		else if ((prumer<=144) && (prumer>=143))	prumer=432;
		else if ((prumer<=142) && (prumer>=141))	prumer=434;
		else if ((prumer<=140) && (prumer>=139))	prumer=436;
		else if ((prumer<=138) && (prumer>=138))	prumer=438;
		else if ((prumer<=137) && (prumer>=136))	prumer=440;
		else if ((prumer<=135) && (prumer>=134))	prumer=442;
		else if ((prumer<=133) && (prumer>=132))	prumer=444;
		else if ((prumer<=131) && (prumer>=130))	prumer=446;
		else if ((prumer<=129) && (prumer>=129))	prumer=448;
		else if ((prumer<=128) && (prumer>=127))	prumer=450;
		else if ((prumer<=126) && (prumer>=125))	prumer=452;
		else if ((prumer<=124) && (prumer>=123))	prumer=454;
		else if ((prumer<=122) && (prumer>=122))	prumer=456;
		else if ((prumer<=119) && (prumer>=120))	prumer=458;
		else if ((prumer<=119) && (prumer>=118))	prumer=460;
		else if ((prumer<=117) && (prumer>=116))	prumer=462;
		else if ((prumer<=115) && (prumer>=115))	prumer=464;
		else if ((prumer<=114) && (prumer>=113))	prumer=466;
		else if ((prumer<=112) && (prumer>=111))	prumer=468;
		else if ((prumer<=110) && (prumer>=110))	prumer=460;
		else if ((prumer<=109) && (prumer>=108))	prumer=472;
		else if ((prumer<=107) && (prumer>=106))	prumer=474;
		else if ((prumer<=105) && (prumer>=104))	prumer=476;
		else if ((prumer<=104) && (prumer>=103))	prumer=478;
		else if ((prumer<=102) && (prumer>=101))	prumer=480;
		else if ((prumer<=100) && (prumer>=99))	prumer=482;
		else if ((prumer<=98) && (prumer>=98))	prumer=484;
		else if ((prumer<=97) && (prumer>=96))	prumer=486;
		else if ((prumer<=95) && (prumer>=95))	prumer=488;
		else if ((prumer<=94) && (prumer>=93))	prumer=490;
		else if ((prumer<=92) && (prumer>=91))	prumer=492;
		else if ((prumer<=90) && (prumer>=90))	prumer=494;
		else if ((prumer<=89) && (prumer>=88))	prumer=496;
		else if ((prumer<=87) && (prumer>=86))	prumer=498;
		else if ((prumer<=85) && (prumer>=85))	prumer=500;
		else if ((prumer<=84) && (prumer>=83))	prumer=502;
		else if ((prumer<=82) && (prumer>=82))	prumer=504;
		else if ((prumer<=81) && (prumer>=80))	prumer=506;
		else if ((prumer<=79) && (prumer>=78))	prumer=508;
		else if ((prumer<=77) && (prumer>=77))	prumer=510;
		else if ((prumer<=76) && (prumer>=75))	prumer=512;
		else if ((prumer<=74) && (prumer>=74))	prumer=514;
		else if ((prumer<=73) && (prumer>=72))	prumer=516;
		else if ((prumer<=71) && (prumer>=71))	prumer=518;
		else if ((prumer<=70) && (prumer>=69))	prumer=520;
		else if ((prumer<=68) && (prumer>=67))	prumer=522;
		else if (prumer<67)	prumer=999;
	}
}
//______________________________________MAIN_LOOP_______________________________________________________________
//______________________________________MAIN_LOOP_______________________________________________________________
int main(void)
{
   DDRA = 0b10111111; //nastaveni pinu na portu A
   PORTA |= 0b00111111; //pocatecni stav LED (zhasnute)
   DDRB = 0b10100000; //nastaveni pinu na portu B
   DDRC = 0b10100011; //nastaveni pinu na portu C
   DDRE &= 0b01111011; //nastaveni vstupniho pinu PINE2 (busy flag od displeje) a PINE7 (INT7)
   DDRD &= 0b11011101;
   DDRF &= 0b11111100;
   //nastaveni ADC
   ADMUX |= 0x01; //nastaveni reference
   ADCSRA = 0x8f; //nastaveni ADC
   
   //nastaveni casovace 3 na 1s - v obsluze preruseni spousteni ADC
   TCCR3B |= 0b00000100; //nastaveni preddelicky na 256
   TCNT3H = 0x57;
   TCNT3L = 0x40;
   ETIMSK |= (1<<TOIE3);
   
   //nastaveni casovace 0 pro PWM AO1
   TCCR0 = 0x6e;
   TCNT0 = 15;
   TIMSK |= 0b00000001; 
   
   //nastaveni casovace 2 pro PWM AO2
   TCCR2 = 0x6c;
   TCNT2 = 15;
   TIMSK |= 0b01000000;

   //nastaveni UART0 pro komunikaci s displejem
   UCSR0B |= (1<<RXCIE0);
   UCSR0C |= (1 << UCSZ00 ) | (1 << UCSZ01 ); //mode
   UBRR0L = 71; //baudrate
   UCSR0B |= (1<<RXEN0);
  
   //predefinovani klasickyho string vystupu
   stdout = &uart_output;
  
   //nastaveni UART1 pro komunikaci s NanoSocket LAN
   UCSR1B |= (1<<RXCIE1);
   UCSR1C |= (1 << UCSZ10 ) | (1 << UCSZ11 ); //mode
   UBRR1L = 71; //baudrate
   UCSR1B |= (1<<RXEN1);
   sbi(PORTD,PIND5);
   
   //nastaveni externiho preruseni INT7 pro DATA_READY
   EICRB |= (1<<ISC71)|(1<<ISC70);
   
   //globalni povoleni preruseni
   sei();
   //cekani pred startem
   for (int i=0;i<250;i++)
   {
	   _delay_ms(20);
   }
   //send(1,"AT+iLSST:010\r\n");  
   automat();
   for (int i=0;i<50;i++)
   {
	   _delay_ms(20);
   }
   automatEthernet();
   
   //nekonecna smycka
   while(1)
   {
   }
}

//PRERUSENI__________________________________________________________________________________________________________________________________________________________
//obsluha preruseni od Timer/Counter3
 ISR(TIMER3_OVF_vect)	{
   TCNT3H = 0x57;
   TCNT3L = 0x40;
   if ((ethernet==64)/*&&((t%5)==0)*/)
   {
	   if (listen_socket==10)
	   {
		   send(1,"AT+iLSST:010\r\n");
	   }
	   else if (listen_socket==11)
	   {
		   send(1,"AT+iLSST:011\r\n");
	   }
   }
   else if ((ethernet==32)&&(t==10))
   {
	   ethernet=16;
	   EIMSK &= (0<<INT7);
	   char commandNSL[16]="AT+iSRCV:000\r\n";
	   commandNSL[9]=active_socket[0];
	   commandNSL[10]=active_socket[1];
	   commandNSL[11]=active_socket[2];
	   send(1,commandNSL);
	   t=0;
   }
   else if ((ethernet==32)&&(t!=10))
   {
	   t++;
   }
   if (stav==6)
   {
	   ADCSRA |= (1<<ADSC); //start prevodu 
   }
}

//obsluha preruseni prijem dat z displeje
ISR(USART0_RX_vect)
{
	input[ind++]= UDR0;
	if ((input[ind-1]=='\n') && (input[0])=='T')
	{
		UCSR0B &= (0<<RXCIE0)&(0<<RXEN0);
		touchPosition();
		automat();
		
		ind=0;
		//clear input
		for (int cZnak=0;cZnak<15;cZnak++)
		{
			input[cZnak]='-';
		}
		UCSR0B |= (1<<RXCIE0)|(1<<RXEN0);
	}
	else if ((input[ind-1]=='\n') && (input[0])!='T')
	{
		ind=0;
	}
}
//obsluha preruseni prijem dat z Nano SocketLAN

ISR(USART1_RX_vect)
{
	
	ethernetCommand[indE++]= UDR1;
	if ((ethernetCommand[indE-1]=='\r')||(ethernetCommand[indE-1]==';'))
	{
		automatEthernet();
		indE=0;
		for (int cZnak=0;cZnak<50;cZnak++)
		{
			ethernetCommand[cZnak]='-';
		}
	}
}
//obsluha externiho preruseni od pinu INT7 - v bufferu v Nano SocketLAN jsou data cekajici na precteni
ISR(INT7_vect)
{
	ethernet=16;
	EIMSK &= (0<<INT7);
	t=0;
	char commandNSL[16]="AT+iSRCV:000\r\n";
	commandNSL[9]=active_socket[0];
	commandNSL[10]=active_socket[1];
	commandNSL[11]=active_socket[2];
	send(1,commandNSL);	
}

//obsluha preruseni od ADC
ISR(ADC_vect){
	if (metoda==1)
	{
		data[idata] = ADCW;
	}
	else if (metoda==2)
	{
		data[idata] = ADCW;
	}
	else if (metoda==3)
	{
		if (MUX0==0)
		{
			ADMUX=0x00;
			pom=ADCW;
			idata--;
		}
		else if (MUX0==1)
		{
			ADMUX=0x01;
			data[idata]=pom/ADCW;
		}
	}
	
	prumernaHodnota();
	
	//int i = idata;
		
		
	idata++;
	if (idata==Cas/**5*/)
	{
		vyhodnoceniAD();
		//stavMereni();//porovnani zakalu s mezi a pripadne prenastavi vystupy
		MR=1;
		automat();
		idata=0;
	}
}

//obsluha prerusenoi od PWM AO1
ISR(TIMER0_OVF_vect)
{
	TCNT0=15;
}

//obsluha prerusenoi od PWM AO2
ISR(TIMER2_OVF_vect)
{
	TCNT2=15;
}
