LED Matrix simulation
in
Share your Work
•
2 months ago
This is a WIP of my first serious Processing project. I am planning to use MIDI input later for activating the individual LEDs. All suggestions welcome!
(Edit: updated code)
- import java.util.Iterator;
color[] pallete = {- #E65350,
- #FAE400,
- #A6E800,
- #7C9CFF,
- };
- color defaultColor = #010101;
- class LED {
- PVector position;
- int w;
- int h;
- color initialColor;
- color defaultColor = #010101;
- color c;
- float amt = 0;
- LED(PVector _position, int _w, int _h) {
- position = _position;
- w = _w;
- h = _h;
- initialColor = pallete[int(random(pallete.length))];
- c = defaultColor;
- }
- void go() {
- update();
- display();
- }
- void pulse() {
- amt = 1.0;
- }
- void update() {
- if (amt > 0) {
- c = lerpColor(defaultColor, initialColor, amt);
- amt -= 0.01;
- }
- }
- boolean within(int x, int y) {
- return x >= position.x && y >= position.y &&
- x <= (position.x + w) && (y <= position.y + h);
- }
- void display() {
- fill(c);
- stroke(0);
- strokeWeight(2);
- rect(position.x, position.y, w, h);
- }
- }
- class LEDPanel {
- ArrayList<LED> leds;
- int rows = 10;
- int cols = 10;
- LEDPanel() {
- int w = width / rows;
- int h = height / cols;
- leds = new ArrayList<LED>();
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- PVector position = new PVector(i * w, j * h);
- leds.add(new LED(position, w, h));
- }
- }
- }
- LED randomLED() {
- return leds.get(int(random(rows * cols)));
- }
- LED within(int x, int y) {
- for (LED led : leds) {
- if (led.within(x, y)) {
- return led;
- }
- }
- return null;
- }
- void go() {
- for (LED led : leds) {
- led.go();
- }
- }
- }
- LEDPanel panel;
- void setup() {
- size(500, 500);
- background(255);
- panel = new LEDPanel();
- }
- int t = 0;
- void draw() {
- if (t % 5 == 0) {
- panel.randomLED().pulse();
- }
- t += 1;
- panel.go();
- }
- void mouseMoved() {
- panel.within(mouseX, mouseY).pulse();
- }