Ultrasound sensor, Arduino, playing movie, using build-in Camera

edited August 2016 in Arduino

Hello everyone,

I am trying to get an ultrasound sensor to read the distance of a person. —When the person is far it should trigger a movie to play. —When the person is at mid distance camera should activate and should film him. When the person is near to the sensor, there should be again a movie being played. At the point the code was reading the distances properly but when the camera was added the distance sensor didn't work anymore or it didn't give me the values anymore, only the camera appears to be activated, but it doesn't show any picture. At use there are a MacBookPro, Build-in Camera, Arduino Uno, Ultrasound sensor and Processing... I am including the code maybe a soul can shed a light!

Thanks so much in advance, Ana

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

Movie movie_1; Movie movie_2; Capture cam;

int distance; Serial myPort; // The serial port String comPortString; int output = 0;

void setup() { // Serial stuff // List all the available serial ports printArray(Serial.list()); // Open the port you are using at the rate you want: myPort = new Serial(this, Serial.list()[2], 9600);

// Movie stuff frameRate(30); size(200, 200); movie_1 = new Movie(this, "01_CloseUp.mov"); movie_2 = new Movie(this, "01_CloseUp.mov");

// Camera stuff String[] cameras = Capture.list();

if (cameras.length == 0) { println("There are no cameras available for capture."); exit(); } else { println("Available cameras:"); for (int i = 0; i < cameras.length; i++) { println(cameras[i]); }

// The camera can be initialized directly using an 
// element from the array returned by list():
cam = new Capture(this, cameras[0]);
cam.start();     

}
} // With this void starts the camera to be detected void draw() {

if (output == 0) { image(movie_1, mouseX, mouseY); } else if (output == 1) { if (cam.available() == true) { cam.read(); } image(cam, 0, 0); } else if (output == 2) { image(movie_2, mouseX, mouseY); } }

void serialEvent(Serial cPort) { comPortString = cPort.readStringUntil('\n'); if(comPortString != null) { comPortString=trim(comPortString);

// Use the distance received by the Arduino to modify the lines distance = int(map(Integer.parseInt(comPortString),1,200,1,height)); println(distance);

// Values from 100 to 5000 if (distance < 1000) { // Play video 2 (dancing) output = 2; } else if (distance < 2500) { // Play video feed from camera (person) output = 1; } else { // Play video 1 (dancer sitting) output = 0; } }

}

Tagged:

Answers

  • Format your code, edit text, select code and hit ctrl+o.

    Kf

  • import processing.serial.*;
    import processing.video.*;
    
    Movie movie_1;
    Movie movie_2;
    Capture cam;
    
    int distance;
    Serial myPort;  // The serial port
    String comPortString;
    int output = 0;
    
    void setup() {
      // Serial stuff 
      // List all the available serial ports
      printArray(Serial.list());
      // Open the port you are using at the rate you want:
      myPort = new Serial(this, Serial.list()[2], 9600);
    
      // Movie stuff
      frameRate(30);
      size(200, 200);
      movie_1 = new Movie(this, "01_CloseUp.mov");
      movie_2 = new Movie(this, "01_CloseUp.mov");
    
      // Camera stuff
      String[] cameras = Capture.list();
    
      if (cameras.length == 0)
      {
        println("There are no cameras available for capture.");
        exit();
      } else {
        println("Available cameras:");
        for (int i = 0; i < cameras.length; i++) {
          println(cameras[i]);
        }
    
        // The camera can be initialized directly using an 
        // element from the array returned by list():
        cam = new Capture(this, cameras[0]);
        cam.start();     
      }      
    }
      // With this void starts the camera to be detected
    void draw() {
    
      if (output == 0)
      {
        image(movie_1, mouseX, mouseY);
      }
      else if (output == 1)
      {
        if (cam.available() == true)
        {
          cam.read();
        }
        image(cam, 0, 0);
      }
      else if (output == 2)
      {
        image(movie_2, mouseX, mouseY);
      }
    }
    
    void serialEvent(Serial cPort)
    {
      comPortString = cPort.readStringUntil('\n');
      if(comPortString != null)
      {
       comPortString=trim(comPortString);
    
       // Use the distance received by the Arduino to modify the lines
       distance = int(map(Integer.parseInt(comPortString),1,200,1,height));
       println(distance);
    
       // Values from 100 to 5000
       if (distance < 1000)
       {
         // Play video 2 (dancing)
         output = 2;
       }
       else if (distance < 2500)
       {
         // Play video feed from camera (person)
         output = 1;
       }
       else
       {
         // Play video 1 (dancer sitting)
         output = 0;
       }
     }
    
    }
    
  • Thanks for the tip! I just included the formated code. )

Sign In or Register to comment.