STM32-SERVO-MOTOR-Main-Function&Button-Checks

by Marwen Maghrebi
void CheckButtons(void) {
    if (HAL_GPIO_ReadPin(BTN_PORT, BTN_45_PIN) == GPIO_PIN_SET) {
        SetServoAngle(45); // Set servo to 45 degrees
    } else if (HAL_GPIO_ReadPin(BTN_PORT, BTN_90_PIN) == GPIO_PIN_SET) {
        SetServoAngle(90); // Set servo to 90 degrees
    } else if (HAL_GPIO_ReadPin(BTN_PORT, BTN_N45_PIN) == GPIO_PIN_SET) {
        SetServoAngle(135); // Set servo to 135 degrees
    } else if (HAL_GPIO_ReadPin(BTN_PORT, BTN_N90_PIN) == GPIO_PIN_SET) {
        SetServoAngle(180); // Set servo to 180 degrees
    } else {
        SetServoAngle(currentAngle); // Maintain the current angle
    }
}

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void) {
    HAL_Init(); // Initialize the HAL Library
    SystemClock_Config(); // Configure the system clock
    MX_GPIO_Init(); // Initialize GPIO
    MX_TIM2_Init(); // Initialize Timer 2 for PWM

    HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1); // Start PWM on Timer Channel 1

    /* Infinite loop */
    while (1) {
        CheckButtons(); // Continuously check button states
        HAL_Delay(50); // Delay to debounce button presses
    }
}
Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
-
00:00
00:00
Update Required Flash plugin
-
00:00
00:00