Can't seems to make it work

Hi i need help with my personal Project, the code is that it is a traffic light that run on a cycle of green yellow then red.
By pressing a push button1 it will blink the red light at the end of green-yellow-red cycles.
button2 is for elderly.

The buzzer is used to create a beeping sound during the pedestrian crossing.
I am using FreeRTOS function. the problem i am facing now is that when i press button1, it snap straight into the pedestrian crossing without first completing the cycle. and when it is finish, i does not restart the traffic light cycle

#define RED 6
#define YELLOW 7
#define GREEN 8

#define Button1 2 //normal
#define Button2 3 //elderly

#define BUZZER 10
#include <Arduino_FreeRTOS.h>
#include "semphr.h" 

long debouncing_time = 150; 
volatile unsigned long last_micros;

 
SemaphoreHandle_t interruptSemaphore,interruptSemaphore1;

const TickType_t _2s = pdMS_TO_TICKS(2000);  
const TickType_t _4s = pdMS_TO_TICKS(4000);  
const TickType_t _6s = pdMS_TO_TICKS(6000);  
const TickType_t _3s = pdMS_TO_TICKS(3000);  
const TickType_t _5s = pdMS_TO_TICKS(5000);  

TaskHandle_t led_handle = NULL, normal_handle = NULL, elderly_handle = NULL;



void setup() {
   pinMode(2, INPUT_PULLUP);
   pinMode(3, INPUT_PULLUP);
  
   Serial.begin(9600);
   

   xTaskCreate(LedControllerTask, "redled", 128,NULL,  0, &led_handle);
   xTaskCreate(TaskLed,  "Led", 128, NULL, 1, &normal_handle);
   xTaskCreate(TaskLed1,  "Led1", 128, NULL, 1, &elderly_handle);
   interruptSemaphore = xSemaphoreCreateBinary();
   interruptSemaphore1 = xSemaphoreCreateBinary();
  if (interruptSemaphore != NULL) {
    attachInterrupt(digitalPinToInterrupt(2), debounceInterrupt, LOW);
  }
   if (interruptSemaphore1 != NULL) { 
    attachInterrupt(digitalPinToInterrupt(3), debounceInterrupt1, LOW);
  }

}

void interruptHandler() {
  xSemaphoreGiveFromISR(interruptSemaphore, NULL);
}
void interruptHandler1() {
  xSemaphoreGiveFromISR(interruptSemaphore1, NULL);
}
void TaskLed(void *pvParameters)
{
  (void) pvParameters;
  pinMode(10, OUTPUT);
  for (;;) {
  if (xSemaphoreTake(interruptSemaphore, portMAX_DELAY) == pdPASS) {
   
      digitalWrite(10, HIGH);
      vTaskSuspend(led_handle);
      digitalWrite(YELLOW, LOW);
      digitalWrite(GREEN, LOW);
      Serial.println("Pedestrian: 4 second");
      for( int i=0; i <= 2; i++ ){
        digitalWrite(RED,digitalRead(RED)^1);
        delay(1000);
        digitalWrite(RED,digitalRead(RED)^1);
        delay(1000);
      }
      digitalWrite(10, LOW);
      vTaskResume(led_handle);
    }
  
  }
}
void TaskLed1(void *pvParameters)
{
  (void) pvParameters;
  pinMode(10, OUTPUT);
  for (;;) {
    if (xSemaphoreTake(interruptSemaphore1, portMAX_DELAY) == pdPASS) {
   
      digitalWrite(10, HIGH);
      vTaskSuspend(led_handle);
      digitalWrite(YELLOW, LOW);
      digitalWrite(GREEN, LOW);
      Serial.println("Elderly Pedestrian: 8 second");
      for( int i=0; i <= 4; i++ ){
        digitalWrite(RED,digitalRead(RED)^1);
        delay(1000);
        digitalWrite(RED,digitalRead(RED)^1);
        delay(1000);
      }
      digitalWrite(10, LOW);
      vTaskResume(led_handle);
    }
  
  }
}

void debounceInterrupt() {
  if((long)(micros() - last_micros) >= debouncing_time * 1000) {
    interruptHandler();
    last_micros = micros();
  }
}
void debounceInterrupt1() {
  if((long)(micros() - last_micros) >= debouncing_time * 1000) {
    interruptHandler1();
    last_micros = micros();
  }
}
void LedControllerTask(void *pvParameters) 
{ 
  pinMode(RED,OUTPUT); 
  pinMode(YELLOW,OUTPUT);
  pinMode(GREEN,OUTPUT); 
 
   digitalWrite(RED,LOW);
   digitalWrite(YELLOW, LOW);
   digitalWrite(GREEN,HIGH);
   Serial.println("GREEN: 4 Second");
   vTaskDelay(_4s);
   
   digitalWrite(RED,LOW);
   digitalWrite(YELLOW, HIGH);
   digitalWrite(GREEN,LOW);
   Serial.println("YELLOW: 2 Second");
   vTaskDelay(_2s);
   
   digitalWrite(RED,HIGH);
   digitalWrite(YELLOW,LOW);
   digitalWrite(GREEN,LOW);
   Serial.println("RED: 6 Second");
   vTaskDelay(_6s);

   
   
} 


void loop(){
  
}

While this may be part of a larger, more complex project, or a learning experience- you certainly decided to do it the hard way.

A simple state machine would be more than enough to perform this functionality.

Hello
Why do you use an OS and not pure C++?

Hi, @krypton232
Welcome to the forum..

You could do this a lot easier using switch.. case..

Tom... :smiley: :+1: :coffee: :australia:

it seems having 2 tasks is inappropriate (much less the use of an OS). there's really just one task that manages the stop light sequence with two stimuli that determine which sequence is "requested".

for this application, a request should only be started after a previous request is completed (unless an input needs to take priority)

shouldn't a light sequencer run continuously? why the need for 2 buttons? wouldn't just one that extends the period of one light be sufficient?

Can you elaborate on that?

assume it would make it longer

Maybe button2 is larger, well placed and easy to press, with a good tactile feel so you know you've done, and an LED that lights up after you do and stays on until whatever the button does is being done or has finished.

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.