Go Down

Topic: I want to make a video play when someone walks past (Read 1 time) previous topic - next topic

Mossi

Apr 06, 2013, 08:39 pm Last Edit: Apr 08, 2013, 03:51 pm by Mossi Reason: 1
Hello,

I'm rather new to this. I'm doing an art installation and using an IR sensor I want to make a video play once the person has walked past it. They will be roughly 50cm away before it should play.

I have started code with help from a friend and when I hit run the numbers get bigger when I move my hand closer and the video sound is heard (I would like to see the picture too) at the beginning once i've first pressed run but once it has finishes when I put my hand back there the video doesn't replay. Here is the code -

Code: [Select]
/**
* Serial Call-Response
* by Tom Igoe.
*
* Sends a byte out the serial port, and reads 3 bytes in.
* Sets foregound color, xpos, and ypos of a circle onstage
* using the values returned from the serial port.
* Thanks to Daniel Shiffman  and Greg Shakar for the improvements.
*
* Note: This sketch assumes that the device on the other end of the serial
* port is going to send a single byte of value 65 (ASCII A) on startup.
* The sketch waits for that byte, then sends an ASCII A whenever
* it wants more data.
*/
import processing.video.*;

Movie myMovie;

import processing.serial.*;

int bgcolor;      // Background color
int fgcolor;      // Fill color
Serial myPort;                       // The serial port
int[] serialInArray = new int[3];    // Where we'll put what we receive
int serialCount = 0;                 // A count of how many bytes we receive
int xpos, ypos;              // Starting position of the ball
boolean firstContact = false;        // Whether we've heard from the microcontroller

void setup() {
 size(256, 256);  // Stage size
 noStroke();      // No border on the next thing drawn
 myMovie = new Movie(this, "MajorProjectVid.mov");
 // Set the starting position of the ball (middle of the stage)
 xpos = width/2;
 ypos = height/2;

 // Print a list of the serial ports, for debugging purposes:
 println(Serial.list());

 // I know that the first port in the serial list on my mac
 // is always my  FTDI adaptor, so I open Serial.list()[0].
 // On Windows machines, this generally opens COM1.
 // Open whatever port is the one you're using.
 String portName = Serial.list()[0];
 myPort = new Serial(this, portName, 9600);
}

void movieEvent(Movie myMovie) {
 myMovie.read();
}

void draw() {
 //background(255);
 //fill(fgcolor);
 // Draw the shape
 //ellipse(xpos, height/2, 20, 20);
 if(xpos > 190){
   image(myMovie,0,0,width, height);
 }else background(0);
}

void serialEvent(Serial myPort) {
 // read a byte from the serial port:
 int inByte = myPort.read();
 // if this is the first byte received, and it's an A,
 // clear the serial buffer and note that you've
 // had first contact from the microcontroller.
 // Otherwise, add the incoming byte to the array:
 if (firstContact == false) {
   if (inByte == 'A') {
     myPort.clear();          // clear the serial port buffer
     firstContact = true;     // you've had first contact from the microcontroller
     myPort.write('A');       // ask for more
   }
 }
 else {
   // Add the latest byte from the serial port to array:
   serialInArray[serialCount] = inByte;
   serialCount++;

   // If we have 3 bytes:
   if (serialCount > 2 ) {
     xpos = serialInArray[0];
     ypos = serialInArray[1];
     fgcolor = serialInArray[2];

     // print the values (for debugging purposes only):
     println(xpos + "\t" + ypos + "\t" + fgcolor);

     // Send a capital A to request new sensor readings:
     myPort.write('A');
     // Reset serialCount:
     serialCount = 0;
   }
 }
}


/*

//  Serial Call and Response
//  by Tom Igoe
//  Language: Wiring/Arduino
 
//  This program sends an ASCII A (byte of value 65) on startup
//  and repeats that until it gets some data in.
//  Then it waits for a byte in the serial port, and
//  sends three sensor values whenever it gets a byte in.
 
//  Thanks to Greg Shakar for the improvements
 
//  Created 26 Sept. 2005
//  Updated 18 April 2008


int firstSensor = 0;    // first analog sensor
int secondSensor = 0;   // second analog sensor
int thirdSensor = 0;    // digital sensor
int inByte = 0;         // incoming serial byte

void setup()
{
 // start serial port at 9600 bps:
 Serial.begin(9600);
 pinMode(2, INPUT);   // digital sensor is on digital pin 2
 establishContact();  // send a byte to establish contact until Processing responds
}

void loop()
{
 // if we get a valid byte, read analog ins:
 if (Serial.available() > 0) {
   // get incoming byte:
   inByte = Serial.read();
   // read first analog input, divide by 4 to make the range 0-255:
   firstSensor = analogRead(0)/4;
   // delay 10ms to let the ADC recover:
   delay(10);
   // read second analog input, divide by 4 to make the range 0-255:
   secondSensor = analogRead(1)/4;
   // read  switch, multiply by 155 and add 100
   // so that you're sending 100 or 255:
   thirdSensor = 100 + (155 * digitalRead(2));
   // send sensor values:
   Serial.print(firstSensor, BYTE);
   Serial.print(secondSensor, BYTE);
   Serial.print(thirdSensor, BYTE);              
 }
}

void establishContact() {
while (Serial.available() <= 0) {
     Serial.print('A', BYTE);   // send a capital A
     delay(300);
 }
}


*/

Grumpy_Mike

Read the how to use this forum sticky post. Then modify that post so the code is in the correct format with code tags.

Mossi

Done :)

I have since found a new plug in called GSMovie which seems to allow you to see the video. The numbers are still at the bottom but again it is not playing the video when I hover my hand over...

Code: [Select]
/**
* Serial Call-Response
* by Tom Igoe.
*
* Sends a byte out the serial port, and reads 3 bytes in.
* Sets foregound color, xpos, and ypos of a circle onstage
* using the values returned from the serial port.
* Thanks to Daniel Shiffman  and Greg Shakar for the improvements.
*
* Note: This sketch assumes that the device on the other end of the serial
* port is going to send a single byte of value 65 (ASCII A) on startup.
* The sketch waits for that byte, then sends an ASCII A whenever
* it wants more data.
*/
import codeanticode.gsvideo.*;

GSMovie movie;

import processing.serial.*;

int bgcolor;      // Background color
int fgcolor;      // Fill color
Serial myPort;                       // The serial port
int[] serialInArray = new int[3];    // Where we'll put what we receive
int serialCount = 0;                 // A count of how many bytes we receive
int xpos, ypos;              // Starting position of the ball
boolean firstContact = false;        // Whether we've heard from the microcontroller

void setup() {
  size(640, 480);  // Stage size
  noStroke();      // No border on the next thing drawn
   movie = new GSMovie(this, "MajorProjectVid.mov");
  movie.loop();
  // Set the starting position of the ball (middle of the stage)
  xpos = width/2;
  ypos = height/2;

  // Print a list of the serial ports, for debugging purposes:
  println(Serial.list());

  // I know that the first port in the serial list on my mac
  // is always my  FTDI adaptor, so I open Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
}

void movieEvent(GSMovie movie) {
  movie.read();
}

void draw() {
  //background(255);
  //fill(fgcolor);
  // Draw the shape
  //ellipse(xpos, height/2, 20, 20);
  if(xpos > 190){
    image(movie,0,0,width, height);
  }else background(0);
}

void serialEvent(Serial myPort) {
  // read a byte from the serial port:
  int inByte = myPort.read();
  // if this is the first byte received, and it's an A,
  // clear the serial buffer and note that you've
  // had first contact from the microcontroller.
  // Otherwise, add the incoming byte to the array:
  if (firstContact == false) {
    if (inByte == 'A') {
      myPort.clear();          // clear the serial port buffer
      firstContact = true;     // you've had first contact from the microcontroller
      myPort.write('A');       // ask for more
    }
  }
  else {
    // Add the latest byte from the serial port to array:
    serialInArray[serialCount] = inByte;
    serialCount++;

    // If we have 3 bytes:
    if (serialCount > 2 ) {
      xpos = serialInArray[0];
      ypos = serialInArray[1];
      fgcolor = serialInArray[2];

      // print the values (for debugging purposes only):
      println(xpos + "\t" + ypos + "\t" + fgcolor);

      // Send a capital A to request new sensor readings:
      myPort.write('A');
      // Reset serialCount:
      serialCount = 0;
    }
  }
}


/*

//  Serial Call and Response
//  by Tom Igoe
//  Language: Wiring/Arduino
 
//  This program sends an ASCII A (byte of value 65) on startup
//  and repeats that until it gets some data in.
//  Then it waits for a byte in the serial port, and
//  sends three sensor values whenever it gets a byte in.
 
//  Thanks to Greg Shakar for the improvements
 
//  Created 26 Sept. 2005
//  Updated 18 April 2008


int firstSensor = 0;    // first analog sensor
int secondSensor = 0;   // second analog sensor
int thirdSensor = 0;    // digital sensor
int inByte = 0;         // incoming serial byte

void setup()
{
  // start serial port at 9600 bps:
  Serial.begin(9600);
  pinMode(2, INPUT);   // digital sensor is on digital pin 2
  establishContact();  // send a byte to establish contact until Processing responds
}

void loop()
{
  // if we get a valid byte, read analog ins:
  if (Serial.available() > 0) {
    // get incoming byte:
    inByte = Serial.read();
    // read first analog input, divide by 4 to make the range 0-255:
    firstSensor = analogRead(0)/4;
    // delay 10ms to let the ADC recover:
    delay(10);
    // read second analog input, divide by 4 to make the range 0-255:
    secondSensor = analogRead(1)/4;
    // read  switch, multiply by 155 and add 100
    // so that you're sending 100 or 255:
    thirdSensor = 100 + (155 * digitalRead(2));
    // send sensor values:
    Serial.print(firstSensor, BYTE);
    Serial.print(secondSensor, BYTE);
    Serial.print(thirdSensor, BYTE);               
  }
}

void establishContact() {
while (Serial.available() <= 0) {
      Serial.print('A', BYTE);   // send a capital A
      delay(300);
  }
}


*/

Grumpy_Mike

Thanks for making the changes.

You need to test each end to see where it is going wrong.

So first off just make a sketch that sends a single character to your processing sketch and see if that works.
Then connect the serial monitor to the arduino and test that your hand waving sends the required character.

Mossi

Ok, I apologise I am really quite new to this and someone helped me set up the initial code (not entirely sure how they did it, I know they mixed Arduino and Processing). In order to do what your suggesting do I use an example from Arduino and one from Processing if so which one? Sorry again.

Go Up