cancel
Showing results for 
Search instead for 
Did you mean: 

I need help for a traffic light, letting 3 LEDs blink at different times for a specific time.

HLa.1
Associate II

Hello, I´m pretty much a newbie, please bear with me.

I´ve seen a lot of posts here stating how to code a traffic light using HAL_Delay and HAL_GetTick() but unfortunately none of those I´ve found stated how I can implement different times when a LED blinks and different times for how long it blinks.

I need a green LED that blinks from 0ms to 3000ms,

a yellow LED that blinks from 2000ms to 3000ms and 5000ms to 6000ms and

a green LED that blinks from 3000ms to 5000ms.

I´ve made a working code using HAL_Delay but I need to use HAL_GetTick() so that I can implement an out-of-order state whenever a push-button is pressed.

Down below is a code but the loop is messed up after it successfully runs the loop once using HAL_GetTick().

/* USER CODE BEGIN PV */
unsigned long previous_time = 0;
int interval_time = 0;
unsigned long buffer = 0;
int interval[] = {0, 2000, 3000, 5000, 6000};
int time;
/* USER CODE END PV */
 
while (1) {
 
  time = HAL_GetTick() - buffer;
 
 
  if (HAL_GetTick() - previous_time >= interval[interval_time])  {
 
    if(HAL_GetTick() >= 6000 ) {
 
      previous_time = HAL_GetTick();
    }
 
    if(time == 0000) {
 
      HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_4);
      if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_5) == 1) {
 
        HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_5);
      }
      interval_time = 1;
    }
 
    if(time == 2000) {
 
      HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_5);
      interval_time = 2;
    }
 
    if(time == 3000)  {
 
      HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_4);
      HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_5);
      HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_11);
      interval_time = 3;
    }
 
    if(time == 5000) {
 
      HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_11);
      HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_5);
      interval_time = 4;
    }
 
   if(time >= 6000){
      interval_time = 0;
      if(HAL_GetTick() >= buffer)  {
      buffer += 6000;
      }
    }
  }
}

Using the STM32CubeIDE debug function I noticed that starting with the 2nd loop either the variable "time" is buggy or the if(time == ***) loops get ignored. In the example below "interval_time" should be 2 after "time" hits 2000.


_legacyfs_online_stmicro_images_0693W000008zsNN.png I`m using the STM32-L432KC and the pin setup is as follows.

2 REPLIES 2
HLa.1
Associate II

0693W000008zsOLQAY.png

HLa.1
Associate II

Fix:

  while (1) {
 
    time = HAL_GetTick() - buffer;
 
 
    if (HAL_GetTick() - previous_time == interval[interval_time])  {
 
     // if(HAL_GetTick() >= 6000 ) {
 
        previous_time = HAL_GetTick();
    //  }
 
      if(time >= 0000 && time <= 0050 ) {
 
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_SET);
        if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_5) == 1) {
 
          HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);
        }
        interval_time = 1;
      }
 
      if(time >= 1975 && time <= 2025 ) {
 
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_SET);
        interval_time = 2;
      }
 
      if(time >= 2975 && time <= 3025)  {
 
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_RESET);
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);
        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, GPIO_PIN_SET);
        interval_time = 3;
      }
 
      if(time >= 4975 && time <= 5025 ) {
 
        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, GPIO_PIN_RESET);
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_SET);
        interval_time = 4;
      }
 
     if(time >= 5975 && time <= 6025 ){
        interval_time = 0;
        if(HAL_GetTick() >= buffer)  {
        buffer += 6000;
        }
      }
    }
  }