donwazonesko
donwazonesko

Reputation: 135

Sending data via USB by pressing button only once

I'm new to STM32, however i wanted to create simple project that allows me to send data via USB by using 2 buttons - only once when the button is pressed. At the moment if i hold the button it will continue to send data. Wanted to "break" out of the if, but it did not help.

while (1){
  if (HAL_GPIO_ReadPin(Button_GPIO_Port,Button_Pin)== GPIO_PIN_RESET){
      HAL_Delay(150);
      if (HAL_GPIO_ReadPin(Button_GPIO_Port,Button_Pin) == GPIO_PIN_RESET){
          ++MessageCounter1;
          MessageLength1 = sprintf(DataToSend, "Wiadomosc BUTTON 1 nr %d\n\r", MessageCounter1);
          CDC_Transmit_HS(DataToSend, MessageLength1);
      }
  }
  if (HAL_GPIO_ReadPin(Button2_GPIO_Port,Button2_Pin)== GPIO_PIN_RESET){
      HAL_Delay(150);
      if (HAL_GPIO_ReadPin(Button2_GPIO_Port,Button2_Pin) == GPIO_PIN_RESET{
          ++MessageCounter2;
          MessageLength2 = sprintf(DataToSend, "Wiadomosc BUTTON 2 nr %d\n\r", MessageCounter2);
          CDC_Transmit_HS(DataToSend, MessageLength2);
      }
  }

Upvotes: 1

Views: 103

Answers (1)

scopchanov
scopchanov

Reputation: 8419

Now you are waiting for the falling edge of the pulse, indicating the press of a button and debounce it using the delay. That is OK.

However, you also have to check for the rising edge of the pulse, generated on that pin when the button is released. Add a boolean variable for that purpose.

Upvotes: 1

Related Questions