Created
January 19, 2017 08:15
-
-
Save Siunami/ccbe0beddc3cd64b76f30358ff8ac616 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Created by Matthew Siu on 2017-1-17. | |
http://www.matthewsiu.com/ | |
https://github.com/Siunami | |
https://www.linkedin.com/in/matthewwilsonsiu | |
*/ | |
// BEGIN WITH PIN 5 | |
// This can be changed to any starting values desired | |
int pinState[10] = {LOW, LOW, LOW, LOW, HIGH, LOW, LOW, LOW, LOW, LOW}; | |
int pinUpdated[10] = {LOW, LOW, LOW, LOW, HIGH, LOW, LOW, LOW, LOW, LOW}; | |
void setup() { | |
} | |
void loop() { | |
delay(500); | |
for (int i = 0; i < 10; i++){ | |
//Update Pins | |
// First pin is in OUTPUT 2 so need to add 2 to each i value | |
pinMode(i+2, OUTPUT); | |
digitalWrite(i+2, pinState[i]); | |
// RULES | |
// Rules are checked on pinState array. pinUpdated array stores the updated values. | |
int left = i - 1; //pin to the left of current | |
int right = i + 1; // pint to the right of current | |
if (i == 0){ // First pin is only checked for the one next to it. | |
if (pinState[right] == LOW){ | |
pinUpdated[i] = LOW; | |
} else { | |
pinUpdated[i] = HIGH; | |
} | |
} else if (i == 9){ // Last pin is only checked for the one next to it. | |
if (pinState[left] == LOW){ | |
pinUpdated[i] = LOW; | |
} else { | |
pinUpdated[i] = HIGH; | |
} | |
} else if (pinState[left] == LOW && pinState[right] == LOW){ // If both adjacent pins are off, current pin is off | |
pinUpdated[i] = LOW; | |
} else if ((pinState[left] == HIGH && pinState[right] == LOW) | |
|| (pinState[left] == LOW && pinState[right] == HIGH)){ // If one of the adjacent pins is on, current pin is now on | |
pinUpdated[i] = HIGH; | |
} else { // Both adjacent pins are on. Current pin suffocates and turns off(dies). | |
pinUpdated[i] = LOW; | |
} | |
} | |
// Set the pinState values to pinUpdated values for next iteration. | |
for (int y = 0; y < 10; y++){ | |
pinState[y] = pinUpdated[y]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment