We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpElectronics,  Serial Library › Intergrating sensor values
Page Index Toggle Pages: 1
Intergrating sensor values (Read 1129 times)
Intergrating sensor values
Apr 22nd, 2010, 10:45am
 
Currently I have two processing sketches, one taking in a live video feed and then working out a light/darkness value and replacing pixels with Images to create a mosaic of the live feed,
the other is using processing to take in a value from a proximity sensor and simply printing out the value, once mapped.

What i am trying to do is, depending on the value taken from the proximity sensor, display the images in the mosaic at a certain size, ie the closer to the screen (higher value) the larger the image and vice-versa.

Currently I was thinking I would need to work along the lines of having my switch statement inside another switch statement of the sensor value.

any help would be greatly appreciated.

Re: Intergrating sensor values
Reply #1 - Apr 23rd, 2010, 5:23am
 
Hope this helps a little bit more here is my code;
Code:

// Reference appropriate
import processing.video.*;
PImage black, white, blue, green, pink, dark, light, lighter, people, street2, streetcar2, crowd, lightcrowd, car; //define all the colour variable names we're to use here

Capture video;
int sx, sy;
int cellsizeX,cellsizeY;
float density;
int[][][] world;

// define temp variables for image co-ordinates
int tempX,tempY;

void setup()
{
frameRate (1);
size(1440, 990);
//println(width + ", " + height);
cellsizeY = 28;
cellsizeX = 33;
density = 0.5;
sx = width/cellsizeX;
sy = height/cellsizeY;
world = new int[sx][sy][2];
video = new Capture(this, width, height, 30);
noStroke();
smooth();

// preload images
black = loadImage("black.jpg");
white = loadImage("white.jpg");
blue = loadImage("blue.jpg");
green = loadImage("green.jpg");
pink = loadImage("pink.jpg");
dark = loadImage("darksmall.jpg");
light = loadImage("light.jpg");
lighter = loadImage("lighter.jpg");
people = loadImage("people.jpg");
street2 = loadImage("street2.jpg");
streetcar2 = loadImage("streetcar2.jpg");
crowd = loadImage("crowd.gif");
lightcrowd = loadImage("lightcrowd.jpg");
car = loadImage("car.jpg");

}

void draw()
{
imageMode(CENTER);

video.read();
image(video, 0, 0, width, height); // Draw webcam feed to screen
video.loadPixels(); // Load pixels
int index = 0; // Index defines which pixel is referred to (sequentially, rather than with an x and y value)
background(0);

for (int x = 1; x <= width; x +=cellsizeX)
{
for (int y = 1; y <= height; y +=cellsizeY)
{
index = (((y+1)*width)-x); // Convert x and y values of the pixel in focus to the sequential format of 'index'
color pixelValue = video.pixels[index]; // Retrieve colour value of current pixel
int colour = int(abs(red(pixelValue)-0)+abs(green(pixelValue)-255)+abs(blue(pixelValue)-0)); // Convert the colour value to something I understand, in the range 0.0 (white) to 510.0 (black)... I think
int loc = (video.width + x + 1) - y*video.width; //trying to Reversing x to mirror the image

// what co-ordinates are we using? variables defined in case we need to mess with them.
tempX = x;
tempY = y;

// map colour to a smaller scale
colour = int(map(colour,255,500,0,100));
println(colour); // Output colour value to the console

switch (colour) { // check using switch - it's faster!

case 0: // the blackests of the blacks.
case 1:
case 2:
case 3:
case 4:
image(dark,tempX,tempY);
break;

case 5: // the blackests of the blacks.
case 6:
case 7:
case 8:
case 9:
image(dark,tempX,tempY);
break;
// etc for more cases.
}
}
}
}

and
Code:

import processing.serial.*;
import cc.arduino.*;

Arduino arduino;
int sensorPin = 0;
int sensorValue;

void setup() {
// size(470, 280);
println(Arduino.list());
arduino = new Arduino(this, Arduino.list()[0], 115200);

arduino.pinMode(sensorValue, Arduino.INPUT);
}

void draw() {

sensorValue = arduino.analogRead(sensorPin);

//map sensor value, smaller scale
sensorValue = int(map(sensorValue,0,500,0,25)); //short-range sensor

println(sensorValue);
delay(1000);
}
Page Index Toggle Pages: 1