Advertisement
kartonman

sochaczew workshop

Jun 26th, 2023
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. /*Code to light buildings. There are two random sequences. One is for security lights and the other for room lights. There is also one light that is constantly lit. You can change the random delays and delay between each of the random sequences. The code is designed to be run on an Arduino Nano. Assuming that you use pin A5 for a switch, you can add 17 lights total. When lights are added or removed, change the arrays and numbers in the index below the arrays.
  2. To change which random sequence initiates first, just move the sequence to a higher or lower position in the respective function.
  3. This code is copywrite Gary C Granai. You may use it for personal use. Use for any commercial purpose is prohibited. You may change it to suit your personal needs.
  4. Complete information about this code and the Nano circuit board to use are at https://steamtraininfo.com/for-modelers/arduino-projects/randomized-lighting.
  5. */
  6.  
  7.  
  8. //random floor lights
  9. const int LED_1_PIN = 2;
  10. const int LED_2_PIN = 3;
  11. const int LED_3_PIN = 4;
  12. const int LED_4_PIN = 5;
  13. const int LED_5_PIN = 6;
  14. const int LED_6_PIN = 7;
  15.  
  16. //random security lights
  17. const int LED_7_PIN = 8;
  18. const int LED_8_PIN = 9;
  19. const int LED_9_PIN = 10;
  20.  
  21. //constantly lit
  22. const int LED_10_PIN = 12; // yellow office light
  23.  
  24. #define SWITCH_PIN A5
  25.  
  26. // Define the minimum and maximum delay times in milliseconds
  27. const int MIN_DELAY_MS_1 = 1000;
  28. const int MAX_DELAY_MS_1 = 5000;
  29. const int MIN_DELAY_MS_2 = 1000;
  30. const int MAX_DELAY_MS_2 = 2000;
  31. const int LEDS_2_DELAY_MS = 5000;
  32.  
  33. void setup()
  34. {
  35. // Set all LED pins as output
  36. pinMode(LED_1_PIN, OUTPUT);
  37. pinMode(LED_2_PIN, OUTPUT);
  38. pinMode(LED_3_PIN, OUTPUT);
  39. pinMode(LED_4_PIN, OUTPUT);
  40. pinMode(LED_5_PIN, OUTPUT);
  41. pinMode(LED_6_PIN, OUTPUT);
  42.  
  43. //constantly lit
  44. pinMode(LED_10_PIN, OUTPUT);
  45. digitalWrite(LED_10_PIN, HIGH);
  46.  
  47. //security
  48. pinMode(LED_7_PIN, OUTPUT);
  49. digitalWrite(LED_7_PIN, HIGH);
  50. pinMode(LED_8_PIN, OUTPUT);
  51. digitalWrite(LED_8_PIN, HIGH);
  52. pinMode(LED_9_PIN, OUTPUT);
  53. digitalWrite(LED_9_PIN, HIGH);
  54.  
  55.  
  56.  
  57.  
  58. pinMode(SWITCH_PIN, INPUT);
  59.  
  60. // Seed the random number generator
  61. randomSeed(analogRead(A7));
  62. }
  63.  
  64. void loop()
  65. {
  66. digitalWrite(LED_10_PIN, HIGH);
  67. // Check state of switch
  68. bool switchState = digitalRead(SWITCH_PIN);
  69. if (switchState == HIGH)
  70. {
  71. turnOnLights();
  72. delay(LEDS_2_DELAY_MS);
  73. }
  74. else
  75. {
  76. turnOffLights();
  77. }
  78. }
  79.  
  80. void turnOnLights() //turns room lights on, security lights off
  81. {
  82. // Create two arrays to hold the LED pins
  83. int leds_1[] = {LED_1_PIN, LED_2_PIN, LED_3_PIN, LED_4_PIN, LED_5_PIN, LED_6_PIN};
  84. int leds_2[] = {LED_7_PIN, LED_8_PIN, LED_9_PIN};
  85.  
  86. // Turn on each room LED in random order for the LEDs in array leds_1[]
  87. for (int i = 0; i < 6; i++)
  88. {
  89. int index = random(i, 6);
  90. int temp = leds_1[index];
  91. leds_1[index] = leds_1[i];
  92. leds_1[i] = temp;
  93. digitalWrite(leds_1[i], HIGH);
  94. delay(random(MIN_DELAY_MS_1, MAX_DELAY_MS_1));
  95. }
  96.  
  97. // Turn off each security LED in random order for LEDs in array leds_2[]
  98. for (int i = 0; i < 3; i++)
  99. {
  100. int index = random(i, 3);
  101. int temp = leds_2[index];
  102. leds_2[index] = leds_2[i];
  103. leds_2[i] = temp;
  104. digitalWrite(leds_2[i], LOW);
  105. delay(random(MIN_DELAY_MS_2, MAX_DELAY_MS_2));
  106. }
  107.  
  108. // Leave all room LEDs on and security lights off
  109. digitalWrite(LED_1_PIN, HIGH);
  110. digitalWrite(LED_2_PIN, HIGH);
  111. digitalWrite(LED_3_PIN, HIGH);
  112. digitalWrite(LED_4_PIN, HIGH);
  113. digitalWrite(LED_5_PIN, HIGH);
  114. digitalWrite(LED_6_PIN, HIGH);
  115. digitalWrite(LED_7_PIN, LOW);
  116. digitalWrite(LED_8_PIN, LOW);
  117. digitalWrite(LED_9_PIN, LOW);
  118.  
  119. }
  120.  
  121. void turnOffLights() // turns room LEDs off and security lights on
  122. {
  123. // Create two arrays to hold the LED pins
  124.  
  125. int leds_1[] = {LED_1_PIN, LED_2_PIN, LED_3_PIN, LED_4_PIN, LED_5_PIN, LED_6_PIN};
  126. int leds_2[] = {LED_7_PIN, LED_8_PIN, LED_9_PIN};
  127.  
  128. // Turn on each security LED in random
  129. for (int i = 0; i < 3; i++)
  130. {
  131. int index = random(i, 3);
  132. int temp = leds_2[index];
  133. leds_2[index] = leds_2[i];
  134. leds_2[i] = temp;
  135. digitalWrite(leds_2[i], HIGH);
  136. delay(random(MIN_DELAY_MS_2, MAX_DELAY_MS_2));
  137. }
  138.  
  139. // Turn off each room LED in random order
  140. for (int i = 0; i < 6; i++)
  141. {
  142. int index = random(i, 6);
  143. int temp = leds_1[index];
  144. leds_1[index] = leds_1[i];
  145. leds_1[i] = temp;
  146. digitalWrite(leds_1[i], LOW);
  147. delay(random(MIN_DELAY_MS_1, MAX_DELAY_MS_1));
  148. }
  149.  
  150.  
  151.  
  152. // Leave all room LEDs off and security LEDs on
  153. digitalWrite(LED_1_PIN, LOW);
  154. digitalWrite(LED_2_PIN, LOW);
  155. digitalWrite(LED_3_PIN, LOW);
  156. digitalWrite(LED_4_PIN, LOW);
  157. digitalWrite(LED_5_PIN, LOW);
  158. digitalWrite(LED_6_PIN, LOW);
  159. digitalWrite(LED_7_PIN, HIGH);
  160. digitalWrite(LED_8_PIN, HIGH);
  161. digitalWrite(LED_9_PIN, HIGH);
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement