Hello,
I’m working with a custom made 16*16 white led matrix connected to an arduino. I’ve written a sketch that samples 256 pixels using the ‘get()’ function, converts the data, and sends to the arduino over osc. It works but is extremely slow. Note the code below is just for 16 leds.
I understand that the loadPixels function is probably more suitable for this, but I’m struggling to understand how to grab specific pixels from it.
Also, how might I go about writing a loop to convert all of the color data, instead of rewriting the same 4 lines of code.
Thanks
import oscP5.*;
import processing.serial.*;
Serial serial;
OscSerial osc;
String serialName = "/dev/tty.usbmodem40915201";
int led[];
int y1 = 15;
int y2 = 45;
int y3 = 75;
int y4 = 105;
int y5 = 135;
int y6 = 165;
int y7 = 195;
int y8 = 225;
int y9 = 255;
int y10 = 285;
int y11 = 315;
int y12 = 345;
int y13 = 375;
int y14 = 405;
int y15 = 435;
int y16 = 465;
int x1 = 15;
int x2 = 45;
int x3 = 75;
int x4 = 105;
int x5 = 135;
int x6 = 165;
int x7 = 195;
int x8 = 225;
int x9 = 255;
int x10 = 285;
int x11 = 315;
int x12 = 345;
int x13 = 375;
int x14 = 405;
int x15 = 435;
int x16 = 465;
// 2d arrays to hold current and next generations
int[][] current, next;
// prob of life, 0 < p < 1
float p;
// pause toggle
boolean pause;
// block and border vars: bx cell width in pixels, by cell height in
// pixels, bw border width in pixels
int bx, by, bw;
// grid size: dimensions of the 2d cell arrays
int gn, gm;
void setup() {
serial = new Serial(this, serialName, 9600);
osc = new OscSerial(this, serial);
osc.plug(this, "myFunction", "/helloFromArduino");
size(500, 500);
frameRate(10);
bx = 30;
by = 30;
bw = 1;
gn = 16;
gm = 16;
current = new int[gn][gm];
next = new int[gn][gm];
for (int i=0;i<gn;i++) {
for (int j=0;j<gm;j++) {
current[i][j] = 0;
next[i][j] = 0;
}
}
// life prob
p=0.2;
RandomCells(p);
pause = false;
}
void draw() {
background(0);
DrawCells();
if (!pause) ConwayStep();
int c0 = get(x1, y1); //get color data from x y point
float p0 = brightness(c0); //converts random color data to 0-2555
float f0 = map(p0,0,255,0,703); // converts rgb value to 703 led value
int led0 = int(f0); //converts float to int
if (led0 < 5000){
OscMessage msg = new OscMessage("/led/0");
msg.add(led0);
osc.send(msg);
}
int c1 = get(x2, y1); //get color data from x y point
float p1 = brightness(c1); //converts random color data to 0-2555
float f1 = map(p1,0,255,0,703); // converts rgb value to 703 led value
int led1 = int(f1); //converts float to int
if (led0 < 5000){
OscMessage msg = new OscMessage("/led/1");
msg.add(led1);
osc.send(msg);
}
int c2 = get(x3, y1); //get color data from x y point
float p2 = brightness(c2); //converts random color data to 0-2555
float f2 = map(p2,0,255,0,703); // converts rgb value to 703 led value
int led2 = int(f2); //converts float to int
if (led0 < 5000){
OscMessage msg = new OscMessage("/led/2");
msg.add(led2);
osc.send(msg);
}
int c3 = get(x4, y1); //get color data from x y point
float p3 = brightness(c3); //converts random color data to 0-2555
float f3 = map(p3,0,255,0,703); // converts rgb value to 703 led value
int led3 = int(f3); //converts float to int
if (led0 < 5000){
OscMessage msg = new OscMessage("/led/3");
msg.add(led3);
osc.send(msg);
}
int c4 = get(x5, y1); //get color data from x y point
float p4 = brightness(c4); //converts random color data to 0-2555
float f4 = map(p4,0,255,0,703); // converts rgb value to 703 led value
int led4 = int(f4); //converts float to int
if (led0 < 5000){
OscMessage msg = new OscMessage("/led/4");
msg.add(led4);
osc.send(msg);
}
int c5 = get(x6, y1); //get color data from x y point
float p5 = brightness(c5); //converts random color data to 0-2555
float f5 = map(p5,0,255,0,703); // converts rgb value to 703 led value
int led5 = int(f5); //converts float to int
if (led0 < 5000){
OscMessage msg = new OscMessage("/led/5");
msg.add(led5);
osc.send(msg);
}
int c6 = get(x7, y1); //get color data from x y point
float p6 = brightness(c6); //converts random color data to 0-2555
float f6 = map(p6,0,255,0,703); // converts rgb value to 703 led value
int led6 = int(f6); //converts float to int
// int led6 = 703;
if (led0 < 5000){
OscMessage msg = new OscMessage("/led/6");
msg.add(led6);
osc.send(msg);
}
int c7 = get(x8, y1); //get color data from x y point
float p7 = brightness(c7); //converts random color data to 0-2555
float f7 = map(p7,0,255,0,703); // converts rgb value to 703 led value
int led7= int(f7); //converts float to int
if (led0 < 5000){
OscMessage msg = new OscMessage("/led/7");
msg.add(led7);
osc.send(msg);
}
int c8 = get(x9, y1); //get color data from x y point
float p8 = brightness(c8); //converts random color data to 0-2555
float f8 = map(p8,0,255,0,703); // converts rgb value to 703 led value
int led8 = int(f8); //converts float to int
if (led0 < 5000){
OscMessage msg = new OscMessage("/led/8");
msg.add(led8);
osc.send(msg);
}
int c9 = get(x10, y1); //get color data from x y point
float p9 = brightness(c9); //converts random color data to 0-2555
float f9 = map(p9,0,255,0,703); // converts rgb value to 703 led value
int led9 = int(f9); //converts float to int
if (led0 < 5000){
OscMessage msg = new OscMessage("/led/9");
msg.add(led9);
osc.send(msg);
}
int c10 = get(x11, y1); //get color data from x y point
float p10 = brightness(c10); //converts random color data to 0-2555
float f10 = map(p10,0,255,0,703); // converts rgb value to 703 led value
int led10 = int(f10); //converts float to int
if (led0 < 5000){
OscMessage msg = new OscMessage("/led2/0");
msg.add(led10);
osc.send(msg);
}
int c11 = get(x12, y1); //get color data from x y point
float p11 = brightness(c11); //converts random color data to 0-2555
float f11 = map(p11,0,255,0,703); // converts rgb value to 703 led value
int led11 = int(f11); //converts float to int
if (led0 < 5000){
OscMessage msg = new OscMessage("/led2/1");
msg.add(led11);
osc.send(msg);
}
int c12 = get(x13, y1); //get color data from x y point
float p12 = brightness(c12); //converts random color data to 0-2555
float f12 = map(p12,0,255,0,703); // converts rgb value to 703 led value
int led12 = int(f12); //converts float to int
if (led0 < 5000){
OscMessage msg = new OscMessage("/led2/2");
msg.add(led12);
osc.send(msg);
}
int c13 = get(x14, y1); //get color data from x y point
float p13 = brightness(c13); //converts random color data to 0-2555
float f13 = map(p13,0,255,0,703); // converts rgb value to 703 led value
int led13 = int(f13); //converts float to int
if (led0 < 5000){
OscMessage msg = new OscMessage("/led2/3");
msg.add(led13);
osc.send(msg);
}
int c14 = get(x15, y1); //get color data from x y point
float p14 = brightness(c14); //converts random color data to 0-2555
float f14 = map(p14,0,255,0,703); // converts rgb value to 703 led value
int led14 = int(f14); //converts float to int
if (led0 < 5000){
OscMessage msg = new OscMessage("/led2/4");
msg.add(led14);
osc.send(msg);
}
int c15 = get(x16, y1); //get color data from x y point
float p15 = brightness(c15); //converts random color data to 0-2555
float f15 = map(p15,0,255,0,703); // converts rgb value to 703 led value
int led15 = int(f15); //converts float to int
if (led0 < 5000){
OscMessage msg = new OscMessage("/led2/5");
msg.add(led15);
osc.send(msg);
}
}
void keyPressed() {
if (key=='c') {
for (int i=0;i<gn;i++) {
for (int j=0;j<gm;j++) {
current[i][j] = 0;
}
}
}
if (key=='p' || key==' ') {
pause = !pause;
}
if (key=='q') {
exit();
}
if (key=='r') {
RandomCells(p);
}
}
void mouseDragged() {
// block size
int blockSize = 2;
for (int i=int(mouseX/(bx+bw)-blockSize); i<=int(mouseX/(bx+bw)+blockSize); i++) {
for (int j=int(mouseY/(by+bw)-blockSize); j<=int(mouseY/(by+bw)+blockSize); j++) {
if (i>=0 && i<gn && j>=0 && j<gm) current[i][j] = 1;
}
}
}
void mouseClicked() {
// block size
int i=int(mouseX/(bx+bw));
int j=int(mouseY/(by+bw));
if (i>=0 && i<gn && j>=0 && j<gm) current[i][j] = 1;
}
void RandomCells(float p) {
for (int i=0;i<gn;i++) {
for (int j=0;j<gm;j++) {
if (random(1)<p) {
current[i][j] = 1;
}
}
}
}
void DrawCells() {
noStroke();
fill(255);
for (int i=0;i<gn;i++) {
for (int j=0;j<gm;j++) {
if (current[i][j]==1) {
rect(i*(bx+bw), j*(by+bw), bx, by);
}
}
}
}
void ConwayStep() {
int nbhdSum;
for (int i=0;i<gn;i++) {
for (int j=0;j<gm;j++) {
nbhdSum=NeighborhoodSum(i, j);
if (nbhdSum<2 || nbhdSum>3) next[i][j]=0;
if (nbhdSum==3) next[i][j] = 1;
if (nbhdSum==2) next[i][j] = current[i][j];
}
}
for (int i=0;i<gn;i++) {
for (int j=0;j<gm;j++) {
current[i][j] = next[i][j];
}
}
}
// compute the neighborhood sum with bounds checking
int NeighborhoodSum(int i, int j) {
int sum = 0;
int k, l;
for (k=i-1;k<=i+1;k++) {
for (l=j-1;l<=j+1;l++) {
if (k>=0 && k<gn && l>=0 && l<gm && !(k==i && l==j)) {
sum+=current[k][l];
}
}
}
return sum;
}