/**@brief Function for performing battery measurement and updating the Battery Level characteristic
 *        in Battery Service.
 */
static void battery_level_update(void)
{
    uint32_t err_code;
    uint8_t  battery_level;
bat_adc_init();
    battery_level = measure_adc();

    err_code = ble_bas_battery_level_update(&m_bas, battery_level);
    if ((err_code != NRF_SUCCESS) &&
        (err_code != NRF_ERROR_INVALID_STATE) &&
        (err_code != BLE_ERROR_NO_TX_PACKETS) &&
        (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
        )
    {
        APP_ERROR_HANDLER(err_code);
    }
}
void main(void)
	{
	//Stops Watchdog
	WDTCTL = WDTPW | WDTHOLD;

	//Calibrate timer
	if(CALBC1_1MHZ==0xFF)
	{
		while(1);
	}
	DCOCTL = 0;
	BCSCTL1 = CALBC1_1MHZ;
	DCOCTL = CALDCO_1MHZ;

	//Timer 0
	TA0CCTL0 = CCIE;
	TA0CCR0 = 32768;
	TA0CTL = TASSEL_1 + MC_1;

	//Enable pushbutton S2
    P1DIR &= ~S2; // pushbutton is configured as input
    P1IE |= S2;   // port interrupt enabled
    P1OUT |= S2;
    P1IES &= ~S2;  // interrupt happens when button is released
    P1REN |= S2;  // to avoid some weird toggling (and LED blinking) when touching the P1.3
    P1IFG &= ~S2;

    //Enable red and green LED
    LED_DIR = LED_1; //LEDs are set as output
    LED_OUT &= ~(LED_1); //LEDs are turned off

    //Enable ports for UART connection
	P1SEL  |= BIT1 + BIT2;	 	//Enable module function on pin
	P1SEL2 |= BIT1 + BIT2;  	//Select second module; P1.1 = RXD, P1.2=TXD

	//Enable clock
	UCA0CTL1 |= UCSSEL_2;    	// Set clock source SMCLK
	UCA0BR0 = 104;           	// 1MHz 9600 (N=BRCLK/baudrate)
	UCA0BR1 = 0;             	// 1MHz 9600
	UCA0MCTL = UCBRS0;       	// Modulation UCBRSx = 1
	UCA0CTL1 &= ~UCSWRST;    	// **Initialize USCI state machine**

	//Enable motor
    P2DIR = 0x3C; // Allow P2 pins 2 to 5 to output for motor and
    P2OUT = 0x3C; // Disable everything.

	IE2 |= UCA0RXIE;         	//enable RX interrupt

	__enable_interrupt();

	while(1)
	{
		__bis_SR_register(GIE);


		if(clean_tank == 1)
		{
			//Filter reminders
			P1OUT |= LED_1;			//Kuidas initializeda ja kinni panna led ilma toggleita
			clean = 0;
			clean_tank = 0;
		}

		if(feed_fishes)
		{
			//Motor operations
			enable_motor(feed);
			feed_fishes = 0;
		}

		if(send_data)
		{
			//start bit (255)
			model_output = 0xFF;
			send_systemdata((int) (model_output));

			//light (0/1)
			ADC10CTL0 = ADC10SHT_3 + ADC10ON; //64 clocks for sample
			ADC10CTL1 = INCH_0 + ADC10DIV_3;	// Sensor is at channel X and CLK/4
			__delay_cycles(3000);	// settle time 30ms
			adc_value = measure_adc();
			model_output = calculate_lightlevel(adc_value);
			send_systemdata((int) (model_output));

			//water (0/1)
			ADC10CTL0 = ADC10SHT_3 + ADC10ON; //64 clocks for sample
			ADC10CTL1 = INCH_7 + ADC10DIV_3;	// Sensor is at channel X and CLK/4
			__delay_cycles(3000);	// settle time 30ms
			adc_value = measure_adc();
			model_output = calculate_waterlevel(adc_value);
			send_systemdata((int) (model_output));

			//feed (0/1)
			ADC10CTL0 = ADC10SHT_3 + ADC10ON; //64 clocks for sample
			ADC10CTL1 = INCH_5 + ADC10DIV_3;	// Sensor is at channel X and CLK/4
			__delay_cycles(3000);	// settle time 30ms
			adc_value = measure_adc();
			model_output = calculate_feedlevel(adc_value);
			send_systemdata((int) (model_output));

			//temperature value (0..40)
			ADC10CTL0 = ADC10SHT_3 + ADC10ON; //64 clocks for sample
			ADC10CTL1 = INCH_4 + ADC10DIV_3;	// Temp Sensor is at channel X and CLK/4
			__delay_cycles(3000);	// settle time 30ms
			adc_value = measure_adc();
			__delay_cycles(3000);	// settle time 30ms
			model_output = calculate_temperature(adc_value);
			send_systemdata((int) (model_output));

			//temperature state (0/1/2)
			temp_state = temperature_state(model_output);
			send_systemdata((int) (temp_state));

			//Filter cleaning (0/1)
			send_systemdata((int) (clean));

			//exit send_data
			send_data = 0;
		}
	}
}