#include // Bool to check if light is already active bool lightActive = false; // Colors in Array: purple, blue, green, yellow, orange, red, white int colorValueArray[][3] = {{125,0,125}, {0,0,255}, {0,255,0}, {255,255,0}, {255,140,0}, {255,0,0}, {255,255,255}}; // Pointer for color array int colorCounter = 6; // Setting PWM properties const int freq = 5000; const int ledChannelRed = 0; const int ledChannelGreen = 1; const int ledChannelBlue= 2; const int resolution = 8; // Setup method for PWM configurations void setupPWM() { // Set pins as output pinMode(LIGHT_LED_PIN_R, OUTPUT); pinMode(LIGHT_LED_PIN_G, OUTPUT); pinMode(LIGHT_LED_PIN_B, OUTPUT); // Configure LED PWM functionalitites ledcSetup(ledChannelRed, freq, resolution); ledcSetup(ledChannelGreen, freq, resolution); ledcSetup(ledChannelBlue, freq, resolution); // Attach the channel to the GPIO2 to be controlled ledcAttachPin(LIGHT_LED_PIN_R, ledChannelRed); ledcAttachPin(LIGHT_LED_PIN_G, ledChannelGreen); ledcAttachPin(LIGHT_LED_PIN_B, ledChannelBlue); } // Takes a int value representing nanometers and sets color array pointer to appropriate color void setValueNM(int NM) { getColorBasedOnValueNM(NM); } // Logic to set color array pointer void getColorBasedOnValueNM(int valueNM) { if (valueNM <= 420) { //Purple colorCounter = 0; } else if (valueNM <= 490) { //Blue colorCounter = 1; } else if (valueNM <= 575) { //Green colorCounter = 2; } else if (valueNM <= 585) { //Yellow colorCounter = 3; } else if (valueNM <= 650) { //Orange colorCounter = 4; } else if (valueNM > 650) { // 650 to 750 is red colorCounter = 5; } Serial.println("New color set based on: "); Serial.print(valueNM); } // Shuts down the light bool shutdownLight() { ledcWrite(ledChannelRed, 0); ledcWrite(ledChannelGreen, 0); ledcWrite(ledChannelBlue, 0); return false; } // Activates the light based on colorValueArray bool activateLight() { ledcWrite(ledChannelRed, colorValueArray[colorCounter][0]); ledcWrite(ledChannelGreen, colorValueArray[colorCounter][1]); ledcWrite(ledChannelBlue, colorValueArray[colorCounter][2]); return true; } // Activate light for given amount of time if not already activated void lightTask(void *parameter) { takeSemaphore(); unsigned long lightTimeoutTimer = millis(); if (lightActive == false) { lightActive = activateLight(); // If current time is below or equal to light timeout do nothing while (millis() - lightTimeoutTimer <= MAX_LIGHT_TIMEOUT) { } lightActive = shutdownLight(); } releaseSemaphore(); vTaskDelete(NULL); } // Trigger for lightTask void triggerLight() { xTaskCreate( lightTask, /* Task function. */ "lightTask", /* String with name of task. */ 2048, /* Stack size in bytes. */ NULL, /* Parameter passed as input of the task */ 1, /* Priority of the task. */ NULL); /* Task handle. */ }