How to use ultrasonic sensor to trigger a video ? (Arduino+processing)

edited November 2016 in Arduino

Hey ! First time I'm using Arduino and Processing at the same time and I'm completely lost ! I'm designing an interactive installation, and I need the trigger the position in a video with the distance : as we get closer to the sensor, the video will move. I've set up a code for Arduino which gives the distance. For processing, I found a code which triggers the position with the webcam (and the size of the head of the spectator, as he gets closer, his head become bigger, and the position moves forward). Do you have any idea/ thoughts about how I can modify this code ? It's the same idea, with arduino instead of the webcam but I don't know where to start...

Thank you very much !

Arduino :

#include <NewPing.h>
#include <Servo.h>


#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

int LEDpin = 13;
Servo myservo;
int val;

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
  pinMode(LEDpin, OUTPUT);
  myservo.attach(9);// attaches servo to pin 9

}

void loop() {
  delay(400);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  //Serial.print("Ping: ");
  Serial.println(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  //Serial.println("cm");
  //if(uS / US_ROUNDTRIP_CM < 100) {digitalWrite(LEDpin, HIGH);}
  //else if (uS / US_ROUNDTRIP_CM > 100) {digitalWrite(LEDpin, LOW);}
 // else if (uS / US_ROUNDTRIP_CM > 100) {digitalWrite(LEDpin, LOW);}
  //delay (200);

  val = (uS / US_ROUNDTRIP_CM);
  val = map(val, 0, 172, 15, 180);
  myservo.write(val);
  delay (150);


}

PROCESSING

import gab.opencv.*;
import processing.video.*;
import java.awt.*;
//-------------------------------------------------
/*


*/
//-------------------------------------------------
Capture video;
OpenCV opencv;
//---
Movie monSuperFilm;// déclaration du film
float positionDuFilmEnSecondes;//position ds le film
Integer surfaceVisages,surfaceMin,surfaceMax;
//-------------------------------------------------
/*


*/
//-------------------------------------------------
void setup() {
  //----------------
  size(1080, 720);//dimensions de votre film en pixels; mettre la définition de votre film
  //-----------------
  // partie video cam
  //-----------------
  video = new Capture(this, 640/2, 480/2);
  opencv = new OpenCV(this, 640/2, 480/2);
  opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);  
  video.start();
  //-----------------
  // partie film
  //-----------------
    monSuperFilm = new Movie(this,"pattern1080-6low-1.mov");// chargement du film ; mettre le nom de votre film qui est dans le dossier data
    monSuperFilm.loop();
  //-----------------
  // partie visages
  //-----------------
  // plus la surface du visage est grande dans l'image retournée par la caméra du mac et plus on est près
  // le programme cumule les surfaces de tous les visages capturés par la caméra; à essayer à plusieurs devant le mac !
    surfaceMin=400;// min 0 //surface du visage sur la camera qui correspond au début du film
    surfaceMax=1500;// max 640x480=307200 //surface du visage sur la camera qui correspond à la fin du film
}
//-------------------------------------------------
/*


*/
//-------------------------------------------------
void draw() {
  //background(255);
  //scale(2);
  opencv.loadImage(video);

  //image(video, 0, 0 );

  //noFill();
  stroke(0, 255, 0);
  strokeWeight(3);
  Rectangle[] faces = opencv.detect();
  println(faces.length);
//---------------------------------------------
// calcul de la surface occupée par les visages
//---------------------------------------------
surfaceVisages=0;
  for (int i = 0; i < faces.length; i++) {
    //println(faces[i].x + "," + faces[i].y);
    //rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
    surfaceVisages=surfaceVisages+faces[i].width*faces[i].height;
  }
  fill(0);
  text(str(surfaceMin)+" / "+str(surfaceVisages)+" / "+str(surfaceMax),10,50);
   monSuperFilm.read();//on lit l'image du film 
  positionDuFilmEnSecondes=map(surfaceVisages,surfaceMin,surfaceMax,0,monSuperFilm.duration()); 
  monSuperFilm.jump(positionDuFilmEnSecondes);// on se déplace dans le film
  image(monSuperFilm, 0, 0);//on affiche l'image courante du film

}
//-------------------------------------------------
/*


*/
//-------------------------------------------------
void captureEvent(Capture c) {
  c.read();
}

Answers

  • Please format your code. Edit your previous post, select your code and hit ctrl+o.

    Kf

Sign In or Register to comment.