code help for ir sensor

I am using a proximity sensor to control a live camera playback in processing.
I want to map a specific range from the sensor to control when the live camera feed is visible and at which transparency.

I want is for the still images to run independently and continusouly, then when the sensor is triggered the live camera feed will overlap the still images at various transparency based on the sensor reading.

Right now the still images run in the background but the live camera feed is always on.. Also the camera feed flickers constantly. I am using a basic logitech webcam. Sometimes it recognizes the range readings and sometimes it doesn't. When I open my serial port with the mapcode active I get range values and random numberics and symbols. when I only have the println active the sensor value reads fine.

here is my arduino code
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
//Serial.println(map(sensorValue, 0, 545, 0, 255));
Serial.write(map(sensorValue, 30, 440, 0, 255));

// send sensor value as number between 0 - 255
//Serial.write(sensorValue/4);
delay(100); // delay in between reads for stability
}

my processing code
int imgCount= 12;
PImage [] imgs = new PImage[imgCount];
float imgW;
float offset = 0;
float easing = 0.05; //using sensor value distance to change its position. this program overlays one image over another by modifying the alpha value of the image with the tint() function.

int currentImage=0;
int lastShown=0;
int interval=3000; // each slide shows for this many millis
int blendedInterval=9000;

int lastRangeReading = 0;
int videoResetInterval=5000; // if we haven't gotten any close range values for a second, reset video transparency

//keep track of loaded images (true or false)
boolean[] loadStates = new boolean [imgCount];

// for loading images
float loaderX, loaderY, theta;

import processing.video.;
import processing.serial.
;

Capture video;
int count;
int writeRow;
int maxRows;
int topRow;
int buffer[];

boolean showLiveVideo=false;
Serial serialPort;
int rangeVal=440;

PImage videoBuffer;

void setup() {
size (480, 640, P3D);
background(0);
smooth();
imgW= width/imgCount;
// load images asynchronously
for (int i = 0; i < imgCount; i++) {
imgs = requestImage("warhol_img"+nf(i, 4)+".jpg");

  • }*
  • // Setup VIDEO Capture*
  • String[] cameras = Capture.list();*
  • //video = new Capture(this, 640, 480);*
  • video = new Capture(this, cameras[9]);*
  • video.start();*
    _ maxRows = height *2;_
    _ buffer = new int[width * maxRows];_
  • writeRow = height - 1;*
  • topRow = 0;*
  • frameRate(80);*
  • background(0);*
  • loadPixels();*
  • // Setup SERIAL PORT*
  • println(Serial.list());*
  • String portName = Serial.list()[0];*
  • serialPort = new Serial(this, portName, 9600);*
    }
    void draw()
    {
  • if (video.available()) {*
  • video.read();*
  • videoBuffer=video;*
  • }*
  • if (videoBuffer !=null) {*
  • image(videoBuffer, 0, 0);*
  • }*
  • // check for new range finder data*
  • while ( serialPort.available () > 0) { // If data is available,*
  • rangeVal = serialPort.read(); // read it and store it in val*
  • lastRangeReading=millis();*
  • //println(rangeVal);*
  • }*
    if(millis()-lastRangeReading >videoResetInterval) {
  • lastRangeReading =millis();*
  • rangeVal=255; // make video completely transparent *
    }
  • // draw pollock image*
  • background(0);*
  • //tint(255, 255);*
  • //image(img, 0, 0, displayWidth, displayHeight);*
  • for (int i = 0; i < imgs.length; i++) {*
  • // Check if individual images are fully loaded*
    if ((imgs_.width != 0) && (imgs*.width != -1)) {
    // As images are loaded set true in boolean array*
    loadStates = true;
    * }
    }
    if (checkLoadStates()) {
    // all the images are loaded*

    * if (currentImage<imgCount) {
    // we are in the first set of single images*

    * drawNextImage();
    if (millis()-lastShown>interval) {
    currentImage=currentImage+1;
    lastShown=millis();
    }
    }
    else {
    // we are drawing the blendef image*

    * drawBlendedImages();
    if (millis()-lastShown>blendedInterval) {
    // if we've shown the blended image for long enough*

    * // reset the whole system*
    * currentImage=0; // start at first image;
    lastShown=millis(); // reset the timer*

    * tint(255);
    }
    }
    }
    // draw video image*

    * tint(255, rangeVal); // display at half opacity*
    * image(video, 0, 0, 480, 640);
    }
    void drawNextImage() {
    int y = (height - imgs[0].height) / 2;
    image(imgs[currentImage], 0, y, imgs[currentImage].height, imgs[currentImage].height);
    }
    void drawBlendedImages() {
    int y = (height - imgs[0].height) / 2;
    for (int i = 0; i < imgs.length; i++) {_

    //image(imgs, width/imgs.lengthi, y, imgs_.height, imgs*.height);
    tint(255, 128);
    image(imgs, 0, y, imgs.height, imgs.height);
    }
    }
    // Loading animation*

    void runLoaderAni() {
    * // Only run when images are loading*
    * if (!checkLoadStates()) {
    ellipse(loaderX, loaderY, 10, 10);
    loaderX += 2;
    loaderY = height/2 + sin(theta) * (height/8);
    theta += PI/22;
    // Reposition ellipse if it goes off the screen*

    * if (loaderX > width + 5) {
    loaderX = -5;
    }
    }
    }
    // Return true when all images are loaded - no false values left in array*

    boolean checkLoadStates() {
    * for (int i = 0; i < imgs.length; i++) {
    if (loadStates == false) {
    return false;
    }
    }
    return true;
    }
    void captureEvent(Capture c) {
    c.read();
    }*_

Read the sticky post in this section. Then modify that post to include the code tags, then your code will not be mangles and we can see what it should be.

  int sensorValue = analogRead(A0);
  // print out the value you read:
 Serial.println(sensorValue);
  //Serial.println(map(sensorValue, 0, 545, 0, 255));
  Serial.write(map(sensorValue, 30, 440, 0, 255));

When you decide that some code doesn't work, it's OK to delete it. It won't be offended. And, even if it is, who cares? It didn't work.

Now, which of the values that you send to Processing - the raw value followed by the carriage return and line feed, in ASCII format, or the binary value, are you expecting Processing to read?

What are you going to be sending Processing if the return value from the analogRead() function is between 0 and 29? What are you going to be sending if the return value is between 440 and 1023?

The map function does not care that the input value is not between the specified from range.

I am working on a piece that I want to incorporate a live camera feed and a looping .mov. Someone suggested that I use Director to control the videos/movies since I am doing a prototype and don't want to buy an external media player at this time. What are you using to play / control the videos? I am really new to this, so please excuse me if the question sounds really stupid. Oh, I am also using a PIR sensor to create the variables for the images opacity. Thanks!