Last active
April 20, 2017 19:19
-
-
Save mapledyne/9198bb4c9d6ee5454b20812852207000 to your computer and use it in GitHub Desktop.
untested multi-led loop
This file contains 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
unsigned long previous_time = 0; | |
int velocity_max = 15; | |
int rate_of_change = 25; | |
int led_max_brightness = 255; | |
int led_count = 2; | |
// This set could be improved with a struct, but not doing that to make it | |
// easier to read for someone who hasn't used structs: | |
int led_pins[2] = {5, 6}; | |
int current_led_value[2]; | |
int current_led_direction[2]; | |
void setup() | |
{ | |
previous_time = millis(); | |
for (int one_pin = 0; one_pin < led_count; one_pin++) | |
{ | |
current_led_value[one_pin] = random(0,led_max_brightness); | |
current_led_direction[one_pin] = 1; | |
if (random(0, 2) == 0) current_led_direction[one_pin] = -current_led_direction[one_pin]; | |
} | |
} | |
void loop() | |
{ | |
unsigned long current_time = millis(); | |
// only actually do stuff if it's been long enough: | |
if (previous_time + rate_of_change > current_time) return; | |
// Do stuff! | |
previous_time = current_time; | |
for (int one_pin = 0; one_pin < led_count; one_pin++) | |
{ | |
// act on one LED: | |
int velocity = random(0, velocity_max) * current_led_direction[one_pin]; | |
// turn our direction around if we've reached an end: | |
if (current_led_value[one_pin] + velocity > led_max_brightness | |
|| current_led_value[one_pin] + velocity < 0) | |
{ | |
current_led_direction[one_pin] = -current_led_direction[one_pin]; | |
continue; | |
} | |
current_led_value[one_pin] = current_led_value[one_pin] + velocity; | |
analogWrite(led_pins[one_pin], current_led_value[one_pin]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment