#define SET0(r, b) r &= ~(1<<b)
#define SET1(r, b) r |= (1<<b)
#define TEST(r, b) r & (1<<b)




void setup() {
Serial.begin(9600);


SET0(DDRC,1); // nastavenie input na REG DDRC na 1 bite (A1)

SET0(ADMUX,REFS1); //ref na aref
SET0(ADMUX,REFS0);

SET0(ADMUX,ADLAR); //zarovna nalavo

SET0(ADMUX,MUX3); //nozivka na A1 cize ADC1
SET0(ADMUX,MUX2);
SET0(ADMUX,MUX1);
SET1(ADMUX,MUX0);

SET1(ADCSRA,ADEN); //zapne prevodnik

SET0(ADCSRA,ADATE); //zapnem cez ADSC bit

SET0(ADCSRA,ADIE); //NEzachytavam prerusenie(IRS)

SET1(ADCSRA,ADPS1); // predelicka 128 cize to bude v range 50-200K
SET1(ADCSRA,ADPS2);
SET1(ADCSRA,ADPS0);

}

void loop() {
SET1(ADCSRA,ADSC);//zapnutie prevodu


while(TEST(ADCSRA, ADIF) == 0); // pockaj na koniec prevodu
delay(200);
int prevod = ADCL; // vypys prevodu 1. cast
prevod = prevod + (ADCH<<8); // vypys prevodu 2. cast(pripocitavame 2 bity na zaciatok posunutim o 8 do lava)

int randomNumber = random(0, 1001);



if(prevod < 204){ //basic logika kodu
 randomNumber = map(randomNumber,0 , 1001, 0 , 4);
Serial.println(randomNumber);
}else if(prevod > 204 && prevod < 512){
    randomNumber = map(randomNumber,0 , 1001, 10 , 21);
Serial.println(randomNumber);
}else if(prevod > 512 && prevod < 816){
    randomNumber = map(randomNumber,0 , 1001, 100 , 201);
Serial.println(randomNumber);
}else{
    randomNumber = map(randomNumber,0 , 1001, 500 , 1001);
Serial.println(randomNumber);
}



}