-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbouncing_leds.ino
86 lines (65 loc) · 1.42 KB
/
bouncing_leds.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 2
#define PIXEL_COUNT 288
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(PIXEL_COUNT, PIN, NEO_GRB + NEO_KHZ800);
#define TD 0.075
#define BALL_COUNT 2
#define MAX_H (PIXEL_COUNT - 1)
#define G 9.8
#define A -G * TD
#define MAX_V sqrt(MAX_H * 2 * G)
struct Ball {
float v;
float h;
int red;
int green;
int blue;
};
Ball balls[BALL_COUNT];
void setup() {
pixels.begin();
randomSeed(analogRead(1));
for (int i = 0; i < BALL_COUNT; i++) {
Ball ball;
ball.v = MAX_V * (random(51) + 50) / 100;
ball.h = 0;
ball.red = random(256);
ball.green = random(256);
ball.blue = random(256);
balls[i] = ball;
}
}
void loop() {
for (int i = 0; i < BALL_COUNT; i++) {
float v = balls[i].v;
float h = balls[i].h;
h = min(
double(MAX_H),
max(
0.0,
(h + (v * TD) + (0.5 * A * TD * TD))
)
);
if (h == 0) {
v *= -0.75;
}
v += A;
if ((int) v == 0 && (int) h == 0) {
v = MAX_V * (random(6) + 95) / 100;
balls[i].red = random(128);
balls[i].green = random(128);
balls[i].blue = random(128);
}
balls[i].v = v;
balls[i].h = h;
}
pixels.clear();
for (int b = 0; b < BALL_COUNT; b++) {
pixels.setPixelColor((int) balls[b].h, balls[b].red, balls[b].green, balls[b].blue);
}
pixels.show();
delay(5);
}