This is my code so far:
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
int[][] fire;
// Flame colors
color[] palette;
float angle;
int[] calc1,calc2,calc3,calc4,calc5;
PGraphics pg;
int timer=0;
int elapsed=0;
int del=2000;
int numCircles = 500;
Circle[] circles = new Circle[numCircles];
void setup(){
size(1439, 800, P2D);
smooth();
for (int i=0; i<numCircles; i++){
circles[i] = new Circle(random(width),random(height));
}
arduino = new Arduino(this, Arduino.list()[0], 57600);
arduino.pinMode(2, Arduino.OUTPUT);
arduino.pinMode(3, Arduino.OUTPUT);
arduino.pinMode(4, Arduino.OUTPUT);
arduino.pinMode(5, Arduino.OUTPUT);
arduino.pinMode(6, Arduino.OUTPUT);
// Create buffered image for 3d cube
pg = createGraphics(width, height, P3D);
calc1 = new int[width];
calc3 = new int[width];
calc4 = new int[width];
calc2 = new int[height];
calc5 = new int[height];
colorMode(HSB);
fire = new int[width][height];
palette = new color[250];
// Generate the palette
for(int x = 0; x < palette.length; x++) {
//Hue 0 to 85: red to yellow
//Saturation is maximum: 255
//Lightness is 0..255 for x=0..128, and 255 for x=128..255
palette[x] = color(x/3, 255, constrain(x*3, 5, 255));
}
// this speeds up the effect by 10fps
for (int x = 0; x < width; x++) {
calc1[x] = x % width;
calc3[x] = (x - 1 + width) % width;
calc4[x] = (x + 1) % width;
}
for(int y = 0; y < height; y++) {
calc2[y] = (y + 1) % height;
calc5[y] = (y + 2) % height;
}
timer=millis();
}
void draw() {
if(keyPressed) {
if (key == 'a' || key == 'A') {
arduino.digitalWrite(2, Arduino.HIGH);
}
if (key == 'b' || key == 'B') {
arduino.digitalWrite(3, Arduino.HIGH);
}
if (key == 'c' || key == 'C') {
arduino.digitalWrite(4, Arduino.HIGH);
}
if (key == 'd' || key == 'D') {
arduino.digitalWrite(5, Arduino.HIGH);
}
if (key == 'e' || key == 'E') {
arduino.digitalWrite(6, Arduino.HIGH);
}
}
else
del=width-mouseX;
elapsed=millis()-timer; // measure elapsed time
if (elapsed>del*1){
arduino.digitalWrite(2, Arduino.HIGH);
}
if(elapsed>del*2){
arduino.digitalWrite(3, Arduino.HIGH);
}
if(elapsed>del*3){
arduino.digitalWrite(4, Arduino.HIGH);
arduino.digitalWrite(2, Arduino.LOW);
}
if(elapsed>del*4){
arduino.digitalWrite(5, Arduino.HIGH);
arduino.digitalWrite(3, Arduino.LOW);
}
if(elapsed>del*5){
arduino.digitalWrite(6, Arduino.HIGH);
arduino.digitalWrite(4, Arduino.LOW);
timer=millis();
}
if(pmouseY!=mouseY){
// if the present mouseY value is different than the previous mouse value
// output this value to pin 3
arduino.analogWrite(2,mouseY/4);
arduino.analogWrite(3,mouseY/4);
arduino.analogWrite(4,mouseY/4);
arduino.analogWrite(5,mouseY/4);
arduino.analogWrite(6,mouseY/4);
println(mouseY);
}
{
angle = angle + 0.05;
// draw cube
pg.beginDraw();
pg.translate(width >> 1, height >> 1);
pg.rotateX(sin(angle/2));
pg.rotateY(cos(angle/2));
pg.background(0);
pg.stroke(128);
pg.scale(mouseY/4);
pg.noFill();
pg.box(4);
pg.endDraw();
loadPixels();
int counter = 0;
// Do the fire calculations for every pixel, from top to bottom
for (int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
fire[x][y] =
((fire[calc3[x]][calc2[y]]
+ fire[calc1[x]][calc2[y]]
+ fire[calc4[x]][calc2[y]]
+ fire[calc1[x]][calc5[y]]) << 5) / 129;
// Output everything to screen using our palette colors
pixels[counter] = palette[fire[x][y]];
// Extract the red value using right shift and bit mask
// equivalent of red(pg.pixels[x+y*w])
if ((pg.pixels[counter++] >> 16 & 0xFF) == 128) {
// Only map 3D cube 'lit' pixels onto fire array needed for next frame
fire[x][y] = 128;
}
}
}
updatePixels();
} for (int i=0; i<numCircles; i++) {
circles[i].display(); // display all the circles
}
}
class Circle {
float x,y; // location
float dim; // dimension
color c; // color
Circle(float x, float y) {
this.x = x;
this.y = y;
dim = random(5);
c = color(random(55));
}
void display() {
float distance = dist(x,y,mouseX,mouseY); // distance between circle and mouse
if (distance < 355) { // if distance is smaller than 255
fill(355-distance);
} else { // if distance is bigger than 255
fill(c);
}
ellipse(x,y,dim,dim); // a circle at position xy
}
}