/************************MAIN*********************************************/
int main(void)
{
	TIM_Config();
	PWM_Config();
	UART1_DMA_CONFIG(txbuffer,txsize,rxbuffer,rxsize,57600);
	USART_config ();
	Zero_state ();
	if (SysTick_Config(SystemCoreClock / SysTick_interval)) // systick_interrupt occur every 0.1 ms (1000=1ms)
  {/* Capture error */ while (1);}
	while (1)
	{
		//control signal
		X360_button ();
		X360_Y1_Analog();
	}
}
int main(void) 
{
	/* Init */
	TIM_Config();
	PWM_Config();
	ENC_Config();
	UART_DMA_CONFIG(txbuffer,txsize,rxbuffer,rxsize,57600);
	USART_config ();
	/* MPU 1 */
	MPU6050_Status = TM_MPU6050_Init(&MPU6050_Data0, TM_MPU6050_Device_0, TM_MPU6050_Accelerometer_2G, TM_MPU6050_Gyroscope_250s);
	Delay (200);
	/* MPU2 */
	//I2C_Config();
	//MPU_Config(MPU_Adress);
	//MPU_Wake(MPU_Adress);
	
	if (SysTick_Config(SystemCoreClock / 1000))
  {/* Capture error */ while (1);}
	
	while (1);	
}
int main(void)
{
	//Calibração do sensor
	/*configPWM();
	setServoAngle(0);
	while(1){
	}*/
	double dt = 0.0;
	double curr_err = 0;
	float PID_Input = 0;
	float PID_Output = 0;
	int i_PID_Input = 0;
	int i_Output = 0;
	char c_PID_Input[4];
	char c_Output[4];
	char c_adc_value[4];
	float gain = -20.0;
	
	
	ADC_config();
	ADC_Start();
	USART_config();
	
	configPWM();
	setServoAngle(0);
	ConfigSerie_BT();
	pid_start();
	pid_timer_config();
	configMotorPWM(500);
	setMotorDirection(1);
	duty_cycle = 10;
	while(1)
	{
		
		if(mode == 1)
		{
			
			if(all_read == 1)
			{
				
				PID_Input = Centroid_Algorithm();
				curr_err = (3.5 - PID_Input);
				dt = TCNT5;
				dt = (dt*16)/1000000;
				
				pid_update(ptr, curr_err,dt);
				PID_Output = PID.control;
				itoa(i_Output, c_Output, 10);
				SendString((char*)"\n-------------------\n");
				SendString((char*)"\nSaída_PID 1: ");
				SendString(c_Output);
				
			
				PID_Output = PID_Output*gain;
				if(PID_Output > 75)
					PID_Output = 75;
				if(PID_Output < -75)
					PID_Output = -75;
			
				setServoAngle(PID_Output);
			
				i_PID_Input = (int) PID_Input;
				i_Output = (int) PID_Output;
				itoa(i_PID_Input, c_PID_Input ,10);
				itoa(i_Output, c_Output, 10);
				SendString((char*)"\nLeituras: "); 
				for(int i = 0; i < 8; i++)
					{
						itoa(adc_value[i], c_adc_value, 10); 
						SendString(c_adc_value);
						UsartSendByte(' ');
					}
				SendString((char*)"\nEntrada PID: "); 
				SendString(c_PID_Input); 
				SendString((char*)"\nSaída_PID 2: "); 
				SendString(c_Output); 
				all_read = 0; 
				ADMUX &= 0XE0; //Limpa ADMUX 
				ADMUX |= channel; //Selecciona o canal 0 
				ADCSRA |= (1<<ADSC); //Inicia conversão
			}
		}
		if(mode == 0)
		{
			//
		}

	}
}