Advertisement
kartonman

dialy cycle with street and builing light control

Oct 3rd, 2021
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. #define FASTLED_INTERNAL
  2. #include <FastLED.h>
  3.  
  4. int num = 1;
  5. int32_t interval = (int32_t) num * 100; /*num is an int, the calculation will done as an int and overflow.
  6. The (int32_t) casts (converts) to the larger type, so the result of the calculation
  7. fits and doesn't overflow.*/
  8.  
  9. #define NUM_LEDS 15 // NUMBER OF PIXELS IN THE USED STRIP
  10. #define DATA_PIN 5
  11. byte maxBrightness = 155; // VALUES FROM 0 - 255 -> THE HIGHER, THE MORE Amps NEEDED - FOR THE LED STRIPS
  12. CRGB leds[NUM_LEDS];
  13.  
  14. #define streetLights 2
  15. #define buildingLights 3
  16. int32_t ledTimer = 0;
  17. int32_t ledTimerUpdate = 0;
  18.  
  19.  
  20.  
  21. DEFINE_GRADIENT_PALETTE (AM_gp) { // Midnight To Noon (amCycle)
  22. 0, 0, 0, 0, //black
  23. 18, 0, 0, 0, //dark red
  24. 56, 161, 83, 0, //orange
  25. 115, 212, 202, 0, //dark yellow
  26. 179, 255, 253, 213, //bright yellow
  27. 255, 255, 255, 255, //white
  28. };
  29. DEFINE_GRADIENT_PALETTE (PM_gp) { // Noon to Midnight (pmCycle)
  30. 0, 255, 255, 255, //white
  31. 18, 255, 253, 213, //bright yellow
  32. 56, 212, 202, 0, //dark yellow
  33. 115, 161, 83, 0, //orange
  34. 179, 98, 0, 0, //dark red
  35. 255, 0, 0, 0 //black
  36. };
  37.  
  38.  
  39. void setup() {
  40. Serial.begin(9600);
  41. FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  42. pinMode(streetLights, OUTPUT);
  43. pinMode(buildingLights, OUTPUT);
  44. }
  45.  
  46.  
  47. uint8_t paletteIndex = 0;
  48. CRGBPalette16 currentPalette = AM_gp;
  49. uint8_t state = 0; // 0 for AM, 1 for PM
  50.  
  51. void loop() {
  52.  
  53. EVERY_N_MILLISECONDS(interval) {
  54. paletteIndex++;
  55. if (paletteIndex == 240) {
  56. paletteIndex = 0;
  57. state = 1 - state;
  58. currentPalette = state ? PM_gp : AM_gp;
  59. }
  60. CRGB color = ColorFromPalette(currentPalette, paletteIndex, maxBrightness, LINEARBLEND);
  61. fill_solid(leds, NUM_LEDS, color);
  62. FastLED.show();
  63. streetLightsRoutine();
  64. buildingLightsRoutine();
  65. }
  66.  
  67. }
  68.  
  69. //*************************************
  70.  
  71. void streetLightsRoutine() {
  72. int32_t localInterval = 2 * 240 * interval;
  73. int32_t localTimer = millis() % localInterval;
  74. if ((localTimer >= .25 * localInterval) && (localTimer <= .75 * localInterval)) {
  75. digitalWrite(streetLights, LOW);
  76. } else {
  77. digitalWrite(streetLights, HIGH);
  78. }
  79. }
  80. void buildingLightsRoutine() {
  81. int32_t localInterval = 2 * 240 * interval;
  82. int32_t localTimer = millis() % localInterval;
  83. if ((localTimer >= .5 * localInterval) && (localTimer <= .75 * localInterval)) {
  84. digitalWrite(buildingLights, HIGH);
  85. } else {
  86. digitalWrite(buildingLights, LOW);
  87. }
  88. }
  89.  
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement