Advertisement
safwan092

Untitled

Feb 11th, 2024
7
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3. #define LED_PIN 5 // Pin number connected to the LED strip
  4. #define NUM_LEDS 60 // Number of LEDs in the strip
  5. #define LED_TYPE WS2812B // Type of the LED strip
  6. #define COLOR_ORDER GRB // Color order of the LED strip
  7.  
  8. CRGB leds[NUM_LEDS]; // Define an array of CRGB objects representing each LED
  9.  
  10. // Function to convert color temperature to RGB values
  11. void colorTemperatureToRGB(uint16_t kelvin, uint8_t& r, uint8_t& g, uint8_t& b) {
  12. // Calculate the red component
  13. if (kelvin <= 66) {
  14. r = 255;
  15. } else {
  16. double tempRed = 329.698727446 * pow((kelvin - 60), -0.1332047592);
  17. r = constrain(tempRed, 0, 255);
  18. }
  19.  
  20. // Calculate the green component
  21. if (kelvin <= 66) {
  22. double tempGreen = 99.4708025861 * log(kelvin) - 161.1195681661;
  23. g = constrain(tempGreen, 0, 255);
  24. } else {
  25. double tempGreen = 288.1221695283 * pow((kelvin - 60), -0.0755148492);
  26. g = constrain(tempGreen, 0, 255);
  27. }
  28.  
  29. // Calculate the blue component
  30. if (kelvin >= 66) {
  31. b = 255;
  32. } else if (kelvin <= 19) {
  33. b = 0;
  34. } else {
  35. double tempBlue = 138.5177312231 * log(kelvin - 10) - 305.0447927307;
  36. b = constrain(tempBlue, 0, 255);
  37. }
  38. }
  39.  
  40. void setup() {
  41. FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS); // Initialize the LED strip
  42. }
  43.  
  44. void loop() {
  45. // Set color temperature to daylight (5500K to 6500K)
  46. uint16_t kelvin = 6000; // You can adjust this value to your desired color temperature
  47.  
  48. // Convert color temperature to RGB values
  49. uint8_t r, g, b;
  50. colorTemperatureToRGB(kelvin, r, g, b);
  51.  
  52. // Set all LEDs to the calculated daylight color
  53. fill_solid(leds, NUM_LEDS, CRGB(r, g, b));
  54.  
  55. // Show the updated LEDs
  56. FastLED.show();
  57.  
  58. // Add a delay to control the speed of color changes
  59. delay(1000); // 1000 milliseconds = 1 second
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement