Skip to content

Instantly share code, notes, and snippets.

@macklinu
Last active December 16, 2015 03:09
Show Gist options
  • Select an option

  • Save macklinu/5367889 to your computer and use it in GitHub Desktop.

Select an option

Save macklinu/5367889 to your computer and use it in GitHub Desktop.
an array of LEDs that turns on and off at random times (within a range)
const int numLeds = 4;
const int pins[] = {
2, 3, 4, 5};
long t_random[numLeds];
long t_reset[numLeds];
long top_range = 2000;
long bottom_range = 100;
int previous_led_state[numLeds];
int led_state[numLeds];
void setup() {
randomSeed(analogRead(0));
for (int i = 0; i < numLeds; i++) {
pinMode(pins[i], OUTPUT);
t_random[i] = random(bottom_range, top_range);
t_reset[i] = millis();
previous_led_state[i] = LOW;
led_state[i] = HIGH;
}
}
void loop() {
for (int i = 0; i < numLeds; i++) {
if (millis() - t_random[i] > t_reset[i]) {
if (previous_led_state[i] == LOW) {
led_state[i] = LOW;
previous_led_state[i] = HIGH;
}
else {
led_state[i] = HIGH;
previous_led_state[i] = LOW;
}
t_random[i] = random(bottom_range, top_range);
t_reset[i] = millis();
}
digitalWrite(pins[i], led_state[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment