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);
}

}

}

Saturday, November 21, 2009

Saturday, November 7, 2009

update 7.11 2100

most of problems today were caused by trying to read a wrong [not active] COM port from processing. in 005_analogRead update there are lines highlighted and commented. that should help.

005 analogRead circles [processing] update













import cc.arduino.*;//import libraries to comunicate with arduino thru serial port
import processing.serial.*;

Arduino myArduino;//declare myArduino as a name for your board

float value;//declare a variable to store input data

void setup() {//exactly the same as in arduino platform
size(600,600);//screen size
println(Arduino.list());//this will list all availible COM ports
myArduino = new Arduino(this, Arduino.list()[0], 57600); //instead of 0 put one you use
background(100);//background colour
smooth();//smooth edges
}

void draw(){//the same as loop in arduino software (code repeats endlessly)
fill(100,10);
noStroke();
rect(0,0,width,height);
value=5*(myArduino.analogRead(0));//that's how we get input from arduino now
stroke(255,100,100);
strokeWeight(3);
noFill();
ellipse(mouseX,mouseY,value,value);//draw a circle using value as a radius
}

Friday, November 6, 2009

004 beep frequency [arduino]














int value;
void setup(){
pinMode(2,OUTPUT);
}
void loop(){
value=analogRead(0);
digitalWrite(2,HIGH);
delayMicroseconds(value);//we are using smaller delay intervals [microseconds], to cause vibration fast enough to be heard.
digitalWrite(2,LOW);
delayMicroseconds(value);

}

003 blink frequency [arduino]














int value;//we declare a variable. we will use this to store input from analog IN
void setup(){
pinMode(2,OUTPUT);
}
void loop(){
value=analogRead(0);//input from analog PIN 0 is assigned to the variable
digitalWrite(2,HIGH);
delay(value);//we can use the above variable to change delay value dynamically. value read by analog PIN is between 0 and 1024.
digitalWrite(2,LOW);
delay(value);
}

002 blink on/off [arduino]














void setup(){
pinMode(2,OUTPUT);//PIN 2 is output
pinMode(3,INPUT);//PIN 3 is input
digitalWrite(2,LOW);//just to make sure: turn PIN 2 in the begining
}
void loop(){

if(digitalRead(3)==HIGH){//if current flows thru PIN 3 do this:

digitalWrite(2,HIGH);
delay(100);
digitalWrite(2,LOW);
delay(100);

}

}

001 blink [arduino]














void setup(){//this part of code runs only once in the begining.here we setup the system.
pinMode(2,OUTPUT);//we setup PIN 2 as output
}
void loop(){//this sequence will cause blinking.
digitalWrite(2,HIGH);//send +5V to PIN 2
delay(100);//wait for 100 miliseconds
digitalWrite(2,LOW);//turn PIN 2 off
delay(100);//wait for 100 miliseconds
}