/* adc2.c
 * This program demonstrates the use of ADMUX for selecting the ADC input pin
 */

#include<avr/io.h>

void main(void)
{
	DDRB = 0xFF; // port B output
	unsigned char compare_value = 0x7f;
	unsigned char x[4]; //Array
	ADCSRA = _BV(ADEN) | _BV(ADPS2); //Enable ADC with prescaler=16
	int i=0;
	while (1==1)
	{  
		for(i=0;i<4;i++)
		{
			// Select pin ADC[0..3] using MUX
			ADMUX = _BV(ADLAR) + i;
			
			//Start conversion
			ADCSRA |= _BV(ADSC);

			// wait until converstion completed
			while (ADCSRA & _BV(ADSC) ) {}

			if(ADCH > compare_value) x[i]=1; //if value at the pin is greater than compare value
			else x[i]=0;
		}
		if(x[0] == 1 || x[1] == 1 || x[2] == 1 || x[3] == 1) PORTB=0xFF; //If voltage at all the four pins is greater than the set voltage
		else PORTB=0x00;
	}
}

