cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F407VG Led Blink Button Problem

Zabranjeni
Associate III

Hello, i'm a new about this embedded systems, i am working with stm32f4 discovery card with HAL Library.

İ looked the GPİO topic and i want to create a program like :

When i clikt the button 3 times red led will open and when i clikc the button 5 times green led will open only.

i create "int count" and i used conditions but couldnt be success about it.

Here is the my code , can anyone help me to show right algorithm?

0693W000003RUfeQAG.png

edit :

Finally it worked :

0693W000003RUtMQAW.png

1 ACCEPTED SOLUTION

Accepted Solutions

Ok, it done finally 🙂

it was all about conditions (==)

0693W000003RUtWQAW.png

View solution in original post

13 REPLIES 13

> if (Hal_GPIO_ReadPin(blahblah) == 1) {

This checks if the button is down, not if the button was "clicked". For "clicked", you need to test for transition of its state from 0 to 1, ie.

_Bool previousState;
 
if ( (Hal_GPIO_ReadPin(blahblah) != previousState) {  // transition
  previousState = !previousState;
  if (previousState == 1) { //transition from 0 to 1, i.e. pin just pressed
    --- do whatever you want for "just pressed"
  } else {
    --- do whatever you want for "just released"  
  }
}

You may find that there are many press/release events upon one press of the button - this is because of bouncing.

JW

MM..1
Chief II

Your code is ok for clicks with absolute lower time as 300ms. Normal human press and release took more time then your code have issue.

Inside if key down you need add while key release, but this is blocking type check, your code stops while release. When you need nonblocking you have hard job.

Zabranjeni
Associate III

Sorry, guyz. i tried what u said ; however i couldn't do what i want.

when count being 3 then it becomes zero.

i just wanna see red blink when i clickt 3 times and see green when i did 5 times.

zero from where? in code example not exist count=0, but i understand you add, but your idea is bad when you wait 5 clicks first arives 3 always...

You need if to check how is led state and when is red wait for 5, otherwise wait for 3....

TDK
Guru

0693W000003RUoWQAW.png

If you feel a post has answered your question, please click "Accept as Solution".

yeah i defined it like global before. Yes, i didnt make good algorithm for it. because when i reach 3 it becomes red with if condition and i cant reach other conditions. So i have problem about it. Maybe it s not important but i wanna solve this.

Now i will try with do while conditons .

Yeah, but i couldnt reach green blink( 5 times) again. Because when i click 3 times it red led lighting and there is no change after this.

0693W000003RUq8QAG.png

TDK
Guru

You want "count == 3" and "count == 5" for a comparison.

"count = 3" is an assignment and is always true (3).

If you feel a post has answered your question, please click "Accept as Solution".

Yeah, what should i do for a comparison?