Sunday, December 20, 2009

last saturday[12XII] exercises

this time no schemes! just explanation what and where. you schould be able to find all the necessary info int the net. check fritzing examples as well.
only thing you need to do is to load a servo library in the beginning of all four codes below
you go to sketch/import library/servo.

piezo triggers servo movement 004

//servo attached to pin 2 + piezo on analog pin 0 [needs amplifier, check here]
//load servo library!

Servo myServo;
const int threshold=200;//how hard do you need to knock?
int piezoRead;

void setup(){
myServo.attach(2);
}

void loop(){

piezoRead=analogRead(0);

if (piezoRead>threshold){//if the signal from piezo is strong enough, then...

for (int i=0; i<180;i+=1){ //turn the servo
myServo.write(i);
delay(10);
}

myServo.write(0);//and go back
delay(500);

}
}

servo and photocell 003

//hook up a servo to pin2 and a photocell[stabilize it with resistor] to analog pin 0
//load servo library!

Servo myServo;
int photoCellValue;
int mappedValue;

void setup(){
myServo.attach(2);
}

void loop(){

photoCellValue=analogRead(0);//reads value from the photocell
mappedValue=map(photoCellValue,0,1024,1,180);//check in the help, very usefull

myServo.write(mappedValue);
delay(5);

}

different speeds servo 002

//similar as below, only little change introduced in the loop
//load servo library!

Servo myServo;

void setup(){
myServo.attach(2);
}

void loop(){

for (int i=0; i<180;i+=1){ //repeats commands inbetween braces 180 times
myServo.write(i);//angle is a variable now
delay(10);
}

myServo.write(0);//go back
delay(500);
}

simple servo control codes [last saturday] 001

//for this code you'll need a servo conected to PIN2

//remember to load a library for servo control!

Servo myServo;//creates servo object to play with

void setup(){
myServo.attach(2);//defines where the servo is attached
}

void loop(){//repeats that:

myServo.write(0);//turns the servo to its 0 positioon
delay(500);//waits 500 ms
myServo.write(180);//turns the servo 180 degrees
delay(500);//waits again
}

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