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(){
}