Sunday, November 29, 2009

Particles+Arduino




//to fully use this sketch, you need to upload SimpleDigitalFirmata to your Arduino, and hook up switches to PINs 2 and 3, and LED to PIN 4
import processing.opengl.*;
import cc.arduino.*;
import processing.serial.*;

Arduino myArduino;

Particles[] particles;
int amount=15000;
float angle=0;

void setup(){
size(800,600,OPENGL);
noStroke();
println(Arduino.list());//this will list all availible COM ports
myArduino = new Arduino(this, Arduino.list()[0], 57600);
myArduino.pinMode(4,Arduino.OUTPUT);

particles=new Particles[amount];//declare na array
for (int i=0; i
particles[i]=new Particles(0,0,0,random(1,5),random(1,5),color(random(0,255),random(0,20),random(0,255)));//create objects of a class "Particles", each with specific properties
}
}
void draw(){
angle+=0.01;//update angle
background(100);
translate(width/2,height/2,0);
scale(2);//"zoom out"
rotateY(angle);//rotate around Y axis
myArduino.digitalWrite(4,Arduino.LOW);
for (int i=0; i
particles[i].behave();
particles[i].display();
particles[i].trigger();
}
}

class Particles// a class contains functions and properties shared by the objects derived from it
{
//first:global variables
float xPos,yPos,zPos;
float step,dimension;
color cc;

Particles(float sX,float sY,float sZ,float dim, float speed, color c){//constructor-this is called when we create new objects in the setup
//variables different for each objects
xPos=sX;
yPos=sY;
zPos=sY;
step=speed;
dimension=dim;
cc=c;
}

void behave(){
if (myArduino.digitalRead(2)==Arduino.HIGH){//move slower if button 1 is pressed
xPos+=random(-step*0.3,step*0.3);
yPos+=random(-step*0.3,step*0.3);
zPos+=random(-step*0.3,step*0.3);
}
if (myArduino.digitalRead(3)==Arduino.HIGH){//move faster if button 2 is pressed
xPos+=random(-step*2,step*2);
yPos+=random(-step*2,step*2);
zPos+=random(-step*2,step*2);
}
if(keyPressed){//move if any key is pressed
xPos+=random(-step,step);
yPos+=random(-step,step);
zPos+=random(-step,step);
}

}

void display(){
stroke(cc);

strokeWeight(dimension);

point(xPos,yPos,zPos);
}
void trigger(){
strokeWeight(10);
stroke(255,150,0);
point(50,50,50);
if(dist(xPos,yPos,zPos,50,50,50)<5){//whenever a particle passes close to point (50,50,50), turn on the LED
myArduino.digitalWrite(4,Arduino.HIGH);
}

}

}

No comments:

Post a Comment