This code detects the speed of visitor entry using two pressur pads and sends the information to a servo motor for its movement.
int ledPin= 13;
int padStart = 3;
int padStop = 4;
int val = 0;
int val2 = 0;
int h=0;
long m = 0;
long mm = 0;
long time;
int mode = 1;
int MaxLengthOfTime = 5000;
long maxTime = 0;
int myspeed = 0;
int startServoFlag;
int angle;
void setup(){
Serial.begin(9600);
pinMode(padStart, INPUT);
pinMode(padStop, INPUT);
pinMode(ledPin,OUTPUT);
digitalWrite(ledPin,HIGH);
delay(1000);
digitalWrite(ledPin,LOW);
delay(1000);
startServoFlag = 0;
angle = 0;
for(int i=0; i <= 100 ; i++){
servo();
}
}
void loop(){
val=digitalRead(padStart);
val2=digitalRead(padStop);
if (mode == 1){ Starting
if (val == HIGH){
time = millis();
maxTime = time + MaxLengthOfTime; Max time
mode = 2;
// myspeed=1;
//Serial.print(1,BYTE);
digitalWrite(ledPin,HIGH);
}
else{
}
}
if (mode == 2){ Starting
if (millis() > maxTime){
mode = 1; Reset the floor
digitalWrite(ledPin,LOW);
}
else{
if (val2 == HIGH){
digitalWrite(ledPin,LOW);
long elapsedTime = (millis() - time);
byte toSend = (elapsedTime * 255)/MaxLengthOfTime; Send values to serial port
//Serial.print(toSend,BYTE);
//Serial.print(1,BYTE);
mode = 3;
myspeed = (elapsedTime * 50)/MaxLengthOfTime;
startServoFlag=1; Start the servo
}
}
}
if (mode== 3){
if (startServoFlag == 1){ Wait to start servo
for(int i=0; i <= 50 ; i++){
angle = (i)* 2;
delay(myspeed);
servo();
}
delay(700);
for(int i=0; i <= 50 ; i++){
angle = (50 - (i)) * 2;
delay(myspeed);
servo();
}
delay(700);
myspeed= myspeed+1;
if(myspeed>30){
myspeed=30;
}
}
}
}
void servo(){
int pulse = 1000 + (angle * ((2000-1000)/(90)));
digitalWrite(5,HIGH);
delayMicroseconds(pulse);
digitalWrite(5,LOW);
delay(20);
}
Processing code
This code detects the green color and create an array of circles according the position of the blobs creating a “path of light” that fills the environment.
Download J. Myron library from the website and add this to the “libraries” folder inside the Processing application folder.
import JMyron.*;
import processing.serial.*;
Serial port; Create object from Serial class
int val=0; Data received from the serial port
Ring[] rings; Declare the array
float coseno;
float f = 0.0;
float inc;
int numRings = 5000 ;
int currentRing = 0;
float fx , fy;
int hx,hy;
float diafake;
JMyron m; Creates a new camera instance
void setup(){
int w = 720;
int h = 576;
size(w,h);
m = new JMyron();
m.start(720,576);
m.findGlobs(1);
println(”Myron ” + m.version());
rings = new Ring[numRings]; Create the array
for (int i = 0; i < numRings; i++) {
rings[i] = new Ring(); Create each object
}
println(Serial.list());
port = new Serial(this, Serial.list()[2], 9600); Sets the port where the information comes from
frameRate(10);
smooth();
}
void mousePressed(){
m.settings();
}
void draw(){
background(0);
Uncomment the following piece to show the camera view
/*int[] img = m.image();
//first draw the camera view onto the screen
loadPixels();
for(int i=0;i<width*height;i++){
pixels[i] = img[i];
}
updatePixels();
*/
inc = (TWO_PI/50.0); Sets the moviment of the breath (scales the circles in and out)
coseno = 150+sin(f)*60.0;
f = f + inc;
mousePosition(); Starts to draw the circles
for (int i = 0; i < numRings; i++) { Creates the array of classes
rings[i].grow();
rings[i].display();
}
m.trackColor(1,255,1,255); Color that is tracked (green in this case)
m.update();
noFill();
int[][] a;
if (0 < port.available()) { If data is available,
val = port.read(); read it and store it in val
println(”val ” + val);
//background(val); Set background to white
}
}
public void stop(){
m.stop();
super.stop();
}
void mousePosition() {
if(mouseX>0 && mouseX<width && mouseY>0 && mouseY<height){
if( val !=0){
currentRing=0;
int[][] a;
a = m.globBoxes();
stroke(255,0,0);
for(int i=0;i<a.length;i++){
int[] b = a[i];
rect(b[0], b[1], b[2], b[3]); draws the bounding boxes around the blobs
rings[currentRing].start((b[0])+b[2]/2, (b[1])+b[3]/2);
currentRing++;
}
}
if(val == 2){
currentRing=0;
}
else{
rings[currentRing].start(random(380,420),random(350,390)); Creates a little hole where the experience begins
if(currentRing<20){
currentRing++;
}
if(currentRing == 20){
currentRing=20;
}
}
if (currentRing >= numRings) {
currentRing = 0;
}
}
class Ring {
float x, y; X-coordinate, y-coordinate
float diameter; Diameter of the ring
boolean on = false; Turns the display on and off
void start(float xpos, float ypos ) {
x = xpos;
y = ypos;
on = true;
diameter = 80;
}
void display() {
if (on == true) {
fill(255,70);
noStroke();
ellipse(x, y, diameter, diameter);
}
}
void grow() {
if (on == true) {
diameter += 0.5;
if (diameter >100) {
diameter = (coseno/10) + 100;
}
}
}
}
KALMA | details | technology | code | prototype | final considerations | credits | download |