Untitled

 avatar
unknown
plain_text
20 days ago
663 B
2
Indexable
uint8_t current_state;
uint8_t last_state = 1;

uint32_t last_debounce_time = 0;
uint32_t debounce_delay = 50;

while (1)
{
    current_state = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0);

    // Detect state change
    if (current_state != last_state)
    {
        last_debounce_time = HAL_GetTick();
    }

    // Check debounce timeout
    if ((HAL_GetTick() - last_debounce_time) > debounce_delay)
    {
        // Button pressed (active LOW)
        if ((current_state == 0) && (last_state == 1))
        {
            uint8_t message[3] = {0x90, 60, 127};

            HAL_UART_Transmit(&huart2, message, 3, 10);
        }
    }

    last_state = current_state;
}
Editor is loading...
Leave a Comment