Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*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.
- To change which random sequence initiates first, just move the sequence to a higher or lower position in the respective function.
- 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.
- Complete information about this code and the Nano circuit board to use are at https://steamtraininfo.com/for-modelers/arduino-projects/randomized-lighting.
- */
- //random floor lights
- const int LED_1_PIN = 2;
- const int LED_2_PIN = 3;
- const int LED_3_PIN = 4;
- const int LED_4_PIN = 5;
- const int LED_5_PIN = 6;
- const int LED_6_PIN = 7;
- //random security lights
- const int LED_7_PIN = 8;
- const int LED_8_PIN = 9;
- const int LED_9_PIN = 10;
- //constantly lit
- const int LED_10_PIN = 12; // yellow office light
- #define SWITCH_PIN A5
- // Define the minimum and maximum delay times in milliseconds
- const int MIN_DELAY_MS_1 = 1000;
- const int MAX_DELAY_MS_1 = 5000;
- const int MIN_DELAY_MS_2 = 1000;
- const int MAX_DELAY_MS_2 = 2000;
- const int LEDS_2_DELAY_MS = 5000;
- void setup()
- {
- // Set all LED pins as output
- pinMode(LED_1_PIN, OUTPUT);
- pinMode(LED_2_PIN, OUTPUT);
- pinMode(LED_3_PIN, OUTPUT);
- pinMode(LED_4_PIN, OUTPUT);
- pinMode(LED_5_PIN, OUTPUT);
- pinMode(LED_6_PIN, OUTPUT);
- //constantly lit
- pinMode(LED_10_PIN, OUTPUT);
- digitalWrite(LED_10_PIN, HIGH);
- //security
- pinMode(LED_7_PIN, OUTPUT);
- digitalWrite(LED_7_PIN, HIGH);
- pinMode(LED_8_PIN, OUTPUT);
- digitalWrite(LED_8_PIN, HIGH);
- pinMode(LED_9_PIN, OUTPUT);
- digitalWrite(LED_9_PIN, HIGH);
- pinMode(SWITCH_PIN, INPUT);
- // Seed the random number generator
- randomSeed(analogRead(A7));
- }
- void loop()
- {
- digitalWrite(LED_10_PIN, HIGH);
- // Check state of switch
- bool switchState = digitalRead(SWITCH_PIN);
- if (switchState == HIGH)
- {
- turnOnLights();
- delay(LEDS_2_DELAY_MS);
- }
- else
- {
- turnOffLights();
- }
- }
- void turnOnLights() //turns room lights on, security lights off
- {
- // Create two arrays to hold the LED pins
- int leds_1[] = {LED_1_PIN, LED_2_PIN, LED_3_PIN, LED_4_PIN, LED_5_PIN, LED_6_PIN};
- int leds_2[] = {LED_7_PIN, LED_8_PIN, LED_9_PIN};
- // Turn on each room LED in random order for the LEDs in array leds_1[]
- for (int i = 0; i < 6; i++)
- {
- int index = random(i, 6);
- int temp = leds_1[index];
- leds_1[index] = leds_1[i];
- leds_1[i] = temp;
- digitalWrite(leds_1[i], HIGH);
- delay(random(MIN_DELAY_MS_1, MAX_DELAY_MS_1));
- }
- // Turn off each security LED in random order for LEDs in array leds_2[]
- for (int i = 0; i < 3; i++)
- {
- int index = random(i, 3);
- int temp = leds_2[index];
- leds_2[index] = leds_2[i];
- leds_2[i] = temp;
- digitalWrite(leds_2[i], LOW);
- delay(random(MIN_DELAY_MS_2, MAX_DELAY_MS_2));
- }
- // Leave all room LEDs on and security lights off
- digitalWrite(LED_1_PIN, HIGH);
- digitalWrite(LED_2_PIN, HIGH);
- digitalWrite(LED_3_PIN, HIGH);
- digitalWrite(LED_4_PIN, HIGH);
- digitalWrite(LED_5_PIN, HIGH);
- digitalWrite(LED_6_PIN, HIGH);
- digitalWrite(LED_7_PIN, LOW);
- digitalWrite(LED_8_PIN, LOW);
- digitalWrite(LED_9_PIN, LOW);
- }
- void turnOffLights() // turns room LEDs off and security lights on
- {
- // Create two arrays to hold the LED pins
- int leds_1[] = {LED_1_PIN, LED_2_PIN, LED_3_PIN, LED_4_PIN, LED_5_PIN, LED_6_PIN};
- int leds_2[] = {LED_7_PIN, LED_8_PIN, LED_9_PIN};
- // Turn on each security LED in random
- for (int i = 0; i < 3; i++)
- {
- int index = random(i, 3);
- int temp = leds_2[index];
- leds_2[index] = leds_2[i];
- leds_2[i] = temp;
- digitalWrite(leds_2[i], HIGH);
- delay(random(MIN_DELAY_MS_2, MAX_DELAY_MS_2));
- }
- // Turn off each room LED in random order
- for (int i = 0; i < 6; i++)
- {
- int index = random(i, 6);
- int temp = leds_1[index];
- leds_1[index] = leds_1[i];
- leds_1[i] = temp;
- digitalWrite(leds_1[i], LOW);
- delay(random(MIN_DELAY_MS_1, MAX_DELAY_MS_1));
- }
- // Leave all room LEDs off and security LEDs on
- digitalWrite(LED_1_PIN, LOW);
- digitalWrite(LED_2_PIN, LOW);
- digitalWrite(LED_3_PIN, LOW);
- digitalWrite(LED_4_PIN, LOW);
- digitalWrite(LED_5_PIN, LOW);
- digitalWrite(LED_6_PIN, LOW);
- digitalWrite(LED_7_PIN, HIGH);
- digitalWrite(LED_8_PIN, HIGH);
- digitalWrite(LED_9_PIN, HIGH);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement