
// FAB_ADC02.c  This code is edited by Yrjo L. for Attiny44 use 2.5.2017
// ====================================
//                ATtiny44						ISP
//           VCC--+ o      +--GND				MISO---+5V
//GREEN LED PB0 --+        +--PA0 M2			SCK ---MOSI
//  RED LED PB1 --+        +--PA1 M1			RST ---GND
//	   	  RESET --+        +--PA2 ADC2 
// 	   INT0 PB2 --+        +--PA3 ADC3 TEMP
//          PA7 --+        +--SCK ADC4 V-MEA
//         MOSI --+--------+--MISO
// ====================================
#define F_CPU 1000000UL // 1 MHz (UL Unsigned Long)
#include <avr/io.h> 
#include <util/delay.h>
#define LED_RED PB1			// Define the I/O port to be used for the RED LED.
#define LED_GREEN PB0		// Define the I/O port to be used for the GREEN LED.
// #define MOTOR1 PA1		// Define the I/O port to be used for the MOTOR 1.
// #define MOTOR2 PA0		// Define the I/O port to be used for the MOTOR 2.
void initADC()				// 8-bit resolution, set ADLAR to 1 to enable the Left-shift result (only bits ADC9..ADC2)
{							// then, only reading ADCH is sufficient for 8-bit results (256 values)   
  ADCSRB =	(1 << ADLAR);									// left shift result
  ADCSRA =  (1 << ADEN)  |     // Enable ADC 
            (1 << ADPS2);    // Set prescaler to 16. Eg. 1 MHz/16 = 62.5 Hz sample rate.
}
int main(void) 
{
	int Mtemp;
	int Mvolt;
	DDRB |= (1 << LED_GREEN);	// LED ports as output
	DDRB |= (1 << LED_RED);		
		 initADC();
  while(1) {
 	ADMUX =  (1 << MUX1)  | (1 << MUX0);     // use ADC3 (PA3), MUX =000011b = TEMP   
	ADMUX &= ~(1 << MUX2);					 // not use ADC4 (PA4) MUX =000100b = VOLTAGE
    ADCSRA |= (1 << ADSC);         // start ADC measurement =TEMP
		  while (ADCSRA & (1 << ADSC) ); // wait till conversion complete 
	Mtemp = ADCH;
	   _delay_ms(2000);
      
   ADMUX &= ~(1 << MUX1)  & ~(1 << MUX0);  // not use ADC3 (PA3), MUX =000011b = TEMP
   ADMUX = (1 << MUX2);						// use ADC4 (PA4) MUX =000100b = VOLTAGE
   ADCSRA |= (1 << ADSC);         // start ADC measurement  =VOLTAGE
		while (ADCSRA & (1 << ADSC) ); // wait till conversion complete
  Mvolt = ADCH;
   if (Mtemp < 128 || Mvolt < 200) // if temperature > 20C or voltage < 12V then pump and blower stop
   {
	   PORTB |= (1 << LED_GREEN);  // pump & blower stop
	   PORTB &= ~(1 << LED_RED);
	   _delay_ms(2000);
	   } else {   // if temperature < 20C and voltage > 12V then pump and blower work
	   PORTB |= (1 << LED_RED);		// pump & blower start
	   PORTB &= ~(1 << LED_GREEN);
	   _delay_ms(2000);
	   }
  }  return(0);
}



