Advertisement
kartonman

Goods Shed Daily Cycle Controller

Feb 23rd, 2022
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. /*Goods Shed Daily cycle. Set it to 3000 for the 1/2 hour cycle.
  2.  
  3. This sketch created and copywrite by Gary C. Granai https://www.facebook.com/gary.granai and is included in the Arduino For Model Railing Library at https://steamtraininfo.com/arduino-projects
  4.  
  5. You are free to use this sketch and amend it for your own personal use as long these credits remain intact.
  6. Using it in any manner or form for commercial purposes is prohibed.*/
  7.  
  8. #define FASTLED_INTERNAL
  9. #include <FastLED.h>
  10.  
  11. int num = 1; //an interval of 3000 lasts about 24.5 minutes real time
  12. int32_t interval = (int32_t) num * 500; /*num is an int, the calculation when done as an int and overflow.
  13. The (int32_t) converts it to the larger type, so the result of the calculation fits and doesn't overflow.*/
  14.  
  15. #define NUM_LEDS 27 // NUMBER OF PIXELS IN THE USED STRIP
  16. #define DATA_PIN 6
  17. byte maxBrightness = 100; // VALUES FROM 0 - 255 -> THE HIGHER, THE MORE AMPS NEEDED FOR THE LED STRIP
  18. CRGB leds[NUM_LEDS];
  19.  
  20. #define streetLights 2
  21. #define buildingLights 3
  22. #define securityLights 4
  23. int32_t ledTimer = 0;
  24. int32_t ledTimerUpdate = 0;
  25.  
  26.  
  27. //the colors and timing of the colors of the LEDs on the strip.
  28. DEFINE_GRADIENT_PALETTE (AM_gp) { // Midnight To Noon (amCycle)
  29. 0, 0, 0, 0, //black
  30. 5, 0, 0, 0, // Black
  31. 15, 0, 0, 0, //black
  32. 50, 0, 0, 0, //black
  33. 105, 97, 97, 79, //
  34. 127, 189, 143, 16, //
  35. 148, 239, 172, 0, //orange
  36. 169, 212, 202, 0, //dark yellow
  37. 200, 255, 253, 213, //bright yellow
  38. 232, 255, 255, 255, //white
  39. 255, 255, 255, 255, //white
  40. };
  41. DEFINE_GRADIENT_PALETTE (PM_gp) { // Noon to Midnight (pmCycle)
  42. 0, 255, 255, 255, //white
  43. 5, 255, 255, 255, //white
  44. 21, 255, 253, 213, //bright yellow
  45. 50, 212, 202, 0, //dark yellow
  46. 84, 239, 172, 0, //orange
  47. 105, 189, 143, 16, //
  48. 127, 97, 97, 79, //
  49. 148, 0, 0, 0, //black
  50. 255, 0, 0, 0, //black
  51. };
  52.  
  53.  
  54.  
  55. void setup() {
  56. Serial.begin(9600);
  57. FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  58. pinMode(streetLights, OUTPUT);
  59. pinMode(buildingLights, OUTPUT);
  60. pinMode(securityLights, OUTPUT);
  61. }
  62.  
  63. uint8_t paletteIndex = 0;
  64. CRGBPalette16 currentPalette = AM_gp;
  65. uint8_t state = 0; // 0 for AM, 1 for PM
  66.  
  67. //Routine
  68. //************************************************************************
  69. void loop() {
  70. EVERY_N_MILLISECONDS(interval) {
  71. paletteIndex++;
  72. if (paletteIndex == 240)/*Because a linear blend is being used, the index has to be set less than 255.
  73. If it is set at 255 there will be a discontinuity in the display of colors. Even, as in this case with 240
  74. selected, the linear blend take the index up to 255 and the full cycle displays on the LED strip.*/
  75. {
  76. paletteIndex = 0;
  77. state = 1 - state;
  78. currentPalette = state ? PM_gp : AM_gp;
  79. }
  80. CRGB color = ColorFromPalette(currentPalette, paletteIndex, maxBrightness, LINEARBLEND);
  81. fill_solid(leds, NUM_LEDS, color);
  82. FastLED.show();
  83. streetLightsCycle();
  84. buildingLightsCycle();
  85. securityLightsCycle();
  86. }
  87.  
  88. }
  89.  
  90. //Cycles
  91. //*************************************
  92. //street and track lighs
  93. void streetLightsCycle() {
  94. int32_t localInterval = 2 * 240 * interval;
  95. int32_t localTimer = millis() % localInterval;
  96. if ((localTimer >= .25 * localInterval) && (localTimer <= .75 * localInterval)) {
  97. digitalWrite(streetLights, LOW);
  98. } else {
  99. digitalWrite(streetLights, HIGH);
  100. }
  101. }
  102. //*************************************
  103. //building lights
  104. void buildingLightsCycle() {
  105. int32_t localInterval = 2 * 240 * interval;
  106. int32_t localTimer = millis() % localInterval;
  107. if ((localTimer >= .27 * localInterval) && (localTimer <= .7 * localInterval)) {
  108. digitalWrite(buildingLights, HIGH);
  109. } else {
  110. digitalWrite(buildingLights, LOW);
  111. }
  112. }
  113. //****************************************************
  114.  
  115. //security lights
  116. void securityLightsCycle() {
  117. int32_t localInterval = 2 * 240 * interval;
  118. int32_t localTimer = millis() % localInterval;
  119. if ((localTimer <= .31 * localInterval) || (localTimer >= .81 * localInterval)) {
  120. digitalWrite(securityLights, HIGH);
  121. } else {
  122. digitalWrite(securityLights, LOW);
  123. }
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement