#include <avr/io.h>
#include <avr/interrupt.h>

#define MUX_OFFSET 0x40 // MUX value for REFS0 to be enabled for AREV <---> VCC tie

SIGNAL(SIG_ADC);
int main (void);

/* Globals, I know... I know... */
unsigned int adc_data;  //variable for ADC results


SIGNAL(SIG_ADC)                 //ADC ISR
{

        adc_data = ADCW;        //read all 10 bits into variable


        if ( adc_data > 900 && adc_data <= 1024 )
        {
                PORTC = 0xFF;   // LED status: 1111-1111
                PORTB = 0x01;   // turn on solenoid
        }

        else if (adc_data > 800 && adc_data <= 900 )
        {
                PORTC = 0x7F;   // LED status: 0111-1111
                PORTB = 0x01;   // turn on solenoid
        }

        else if (adc_data > 700 && adc_data <= 800 )
        {
                PORTC = 0x3F;   // LED status: 0011-1111
                PORTB = 0x01;   // turn on solenoid
        }

        else if (adc_data > 600 && adc_data <= 700)
        {
                PORTC = 0x1F;   // 0001-1111
                PORTB = 0x01;   // turn on solenoid
        }

        else if (adc_data > 500 && adc_data <= 600 )
        {
                PORTC = 0x0F;   // 0000-1111
                PORTB = 0x01;   // turn on solenoid
        }

        else if (adc_data > 400 && adc_data <= 500)
        {
                PORTC = 0x07;   // 0000-0111
                PORTB = 0x01;   // turn on solenoid
        }

        else if (adc_data > 300 && adc_data <= 400 )
        {
                PORTC = 0x03;   // 0000-0011
                PORTB = 0x01;   // turn on solenoid
        }

        else if (adc_data > 200 && adc_data <= 300 )
        {
                PORTC = 0x00;   // all lights off 0000-0000
                PORTB = 0x00;   // turn off solenoid
        }

        else if (adc_data > 100 && adc_data <= 200 )
        {
                PORTC = 0x00;   // all lights off 0000-0000
                PORTB = 0x00;   // turn off solenoid
        }

        else if (adc_data > 0 && adc_data <= 100 )
        {
                PORTC = 0x00;   // all lights off 0000-0000
                PORTB = 0x00;   // turn off solenoid
        }

        else
        {
                PORTC = 0x00;   // all lights off 0000-0000
                PORTB = 0x00;   // solenoid off
        }

        ADCSRA = ADCSRA | 0x40; //start the next conversion
}


int main(void)
{
        DDRB=0xFF;      //LED output pins enabled
        PORTC= 0xFF;    //All LEDs off

        DDRC=0x01;      //Reed Relay output pin enabled
        PORTB= 0x00;    //Reed Relay off

        ADMUX=MUX_OFFSET; //MUX value for REFS0 to be enabled for AREV <---> VCC tie
        ADCSRA=0xCE;      //ADC on, /64, interrupt unmasked, and started

        sei();          //global interrupt enable bit


        while(1) {
        }
}