Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to make a program where two different LEDs are blinking every 1000ms but I also want to be able to exit the loop anytime I want. Once I get in the while loop, the button on the STM32 is not responding.

I am working with STM32CubeIDE and TouchGFX, C language.

C
void Screen1View::relay_1()
{
if(toggleButton1.getState()){
 while(1){
   HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_SET);
   HAL_Delay(1000);
   HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_RESET);
   HAL_Delay(1000);
   HAL_GPIO_WritePin(GPIOJ, GPIO_PIN_2, GPIO_PIN_SET);
   HAL_Delay(1000);
   HAL_GPIO_WritePin(GPIOJ, GPIO_PIN_2, GPIO_PIN_RESET);
   HAL_Delay(1000);
if(toggleButton1.getState() == 0){
   break;
       }
   }
}
else HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_RESET);
}


What I have tried:

I have tried to set toggleButton1.getState() != 0 and just play with the code around but nothing helps.

If I use that part of the code without while, I can interact with the button.
Posted
Updated 18-Aug-23 7:21am
v2
Comments
jeron1 18-Aug-23 10:09am    
Possibly setup a timer that triggers an interrupt at the proper time or have no interrupts and simply poll the timer status bit in your while loop and alter your LED outputs when the timer status flag is set, all the while still being able to poll for a button press?
Member 16074047 21-Aug-23 1:59am    
So for example I could (sum) +1 everytime the while goes trough and once it adds up to 10 the while loop stops?

You have 5 Delay statements in there: each of them probably waits around 1 second - so unless you hold the button down for at least 5 seconds, probably longer, there is no way that the code will see the button being pressed since it reads the current state of an IO pin - which is almost certainly not connected to a latch since there is no sign of a latch reset in your code ...
 
Share this answer
 
Comments
Member 16074047 18-Aug-23 7:11am    
Would adding an additional function inside the loop that checks if the button is 1 or 0 help?
OriginalGriff 18-Aug-23 7:32am    
Probably not: the check itself takes an unnoticeable amount of time in human terms, and each delay is a second - think about how long it takes you to "press a button" and you'd see what I mean.

What you probably need to do is change the whole way you do things: I have no idea if you have interrupts on that board or how you tie C code to them, but if you do and can, then I'd set up a state machine to decide what to do next and let timer and button interrupts change the state.
Member 16074047 18-Aug-23 7:36am    
Ill do some more research about your suggestion. Thank you for your help :)

edit:I can interupt the code anytime by pressing a physical button on my board. But is that the "legit way" to say that the program is working?
OriginalGriff 18-Aug-23 8:22am    
No, the interrupts I means are hardware signals that cause code to run when they change: Interrupt - Wikipedia[^]
Member 16074047 18-Aug-23 9:23am    
alright, Ill do something else.
You need to add a pause within your loop because your current loop is continuously running and not giving the system a chance to process other events, including button presses -
C
void Screen1View::relay_1()
{
    if (toggleButton1.getState()) {
        while (1) {
            //Turn LED1 on...
            HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_SET);
            HAL_Delay(1000);

            //Turn LED1 off...
            HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_RESET);
            HAL_Delay(1000);

            //Turn LED2 on...
            HAL_GPIO_WritePin(GPIOJ, GPIO_PIN_2, GPIO_PIN_SET);
            HAL_Delay(1000);

            //Turn LED2 off...
            HAL_GPIO_WritePin(GPIOJ, GPIO_PIN_2, GPIO_PIN_RESET);
            HAL_Delay(1000);

            //Check if the toggle button state has changed...
            if (!toggleButton1.getState()) {
                break; //If so, exit the loop if button is not pressed...
            }
        }
    } else {
        //If the button is not pressed, turn off LED1...
        HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_RESET);
    }
}
 
Share this answer
 
Comments
Member 16074047 18-Aug-23 9:08am    
how would I add a pause?
Andre Oosthuizen 18-Aug-23 12:44pm    
It's already added - 'if (!toggleButton1.getState()) {break;}'
Member 16074047 21-Aug-23 1:45am    
is this supposed to be inside the while loop? If yes, I have already tried that and it doesn't work :/

edit: I am guessing that the button is not responsive inside the while loop since the program doesn't have time to check the state of the button?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900