Interrupt Problem

Hello,

I am newbie in embedded world, but until now I could solve my problems just with searching on internet. Now I cannot solve this problem and this is my first question in this forum. I can write here something very absurd, please ignore them. I will try to do my best to explain my problem.

For this project I am using stm32F407vgt6 MCU, Cubemx (v 5.6.1) , Keilµvision (v 5.29.0.0). (Cubemx pinout will be uploaded as Picture)

I have builded a 4 leg spider with 12 servos with timer 2,3 and timer 4. I am commanding my robot via Bluetooth module(hc-05). Now it can walk and turn right left.

I want to open a interrupt channel (PC13) which is connected to a sensor .

What I want to do; 

If I insert a metall dedector to Interrupt channel and robot meets with an metal, I want that robot stops and turn another direction and continue to walk. (Or we can think that when it comes against a wall, infrared sensor will be activated and robot stops and walk another direction). I did not use a analog sensor, instead I have connected with a digital sensor and read the sensor as it is button. I am not working on ADC mode.

I have applied all what I want and start the test.

Attempt 1:

Firstly I wrote just the stand position code in Interrupt function without a delay code as it has seen followingly.

/* USER CODE BEGIN 4 */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(GPIO_Pin);
/* NOTE: This function Should not be modified, when the callback is needed,
the HAL_GPIO_EXTI_Callback could be implemented in the user file
*/

htim2.Instance->CCR1 = 185;
htim2.Instance->CCR2 = 115;
htim2.Instance->CCR3 = 110;
htim2.Instance->CCR4 = 180;

htim3.Instance->CCR1 = 125;
htim3.Instance->CCR2 = 185;
htim3.Instance->CCR3 = 180;
htim3.Instance->CCR4 = 115;

htim4.Instance->CCR1 = 125;
htim4.Instance->CCR2 = 180;
htim4.Instance->CCR3 = 190;
htim4.Instance->CCR4 = 125;
}
/* USER CODE END 4 */ 

Result: The robot stops just a second in stand position and continue to walk suddenly.

Attemp 2: I have thought without a delay function interrupt does not take much time and continue to walk, and wrote turn right code which has too much delay in it.  

/* USER CODE BEGIN 4 */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(GPIO_Pin);
/* NOTE: This function Should not be modified, when the callback is needed,
the HAL_GPIO_EXTI_Callback could be implemented in the user file
*/
//2. Leg

htim3.Instance->CCR2 = 190;
HAL_Delay(40);
htim3.Instance->CCR2 = 210;
HAL_Delay(100);

htim2.Instance->CCR2 = 110;
HAL_Delay(40);
htim2.Instance->CCR2 = 166;
HAL_Delay(100);


htim3.Instance->CCR2 = 205;
HAL_Delay(40);
htim3.Instance->CCR2 = 185;
HAL_Delay(100);

etc...
}
/* USER CODE END 4 */

Result: If there is a delay code in user code 4, it makes always this result. When interrupt chn is activated, the robot stops moving and take no more command until I reset the MCU. 

and later I  tried so many other possibilities, but it did not work. (writing the all codes in interrupt function | in while section creating a  interrupt button and "if(interruptState){}" etc.)

I kindly need your helps. 

What should I do to overcome this problem?

My codes are very very long so I can not write down here but I will give short and important examples of my code at the end of this post.

/* USER CODE BEGIN PV */

char rxMessage[1];

/* USER CODE END PV */

-----------------

/* USER CODE BEGIN 2 */
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2)

etc...

HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_4);
/* USER CODE END 2 */

-----------------

while (1)
{
/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */
if(HAL_UART_Receive(&huart1,(uint8_t *)rxMessage,sizeof(rxMessage),HAL_MAX_DELAY)!=HAL_OK){
Error_Handler();
}
if(strncmp(rxMessage,"n",1)==0){

htim2.Instance->CCR1 = 185;
htim2.Instance->CCR2 = 115;
htim2.Instance->CCR3 = 110;
htim2.Instance->CCR4 = 180;

htim3.Instance->CCR1 = 125;
htim3.Instance->CCR2 = 185;
htim3.Instance->CCR3 = 180;
htim3.Instance->CCR4 = 115;

htim4.Instance->CCR1 = 125;
htim4.Instance->CCR2 = 180;
htim4.Instance->CCR3 = 190;
htim4.Instance->CCR4 = 125;

} (This stand and start position of the robot)

Walking

if(strncmp(rxMessage,"i",1)==0){

//Left-Front leg

htim3.Instance->CCR2 = 190;
HAL_Delay(40);
htim3.Instance->CCR2 = 205;
HAL_Delay(100);

htim2.Instance->CCR2 = 113; htim4.Instance->CCR2 = 176;
HAL_Delay(40);
htim2.Instance->CCR2 = 85; htim4.Instance->CCR2 = 150;
HAL_Delay(100);

htim3.Instance->CCR2 = 200;
HAL_Delay(40);
htim3.Instance->CCR2 = 178;
HAL_Delay(100);

etc...}

/* USER CODE BEGIN 4 */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(GPIO_Pin);
/* NOTE: This function Should not be modified, when the callback is needed,
the HAL_GPIO_EXTI_Callback could be implemented in the user file
*/

}
/* USER CODE END 4 */

More questions in this forum