A newbie with a question about controlling video in Processing via Arduino

edited January 2016 in Arduino

Hi there,

I'm completely new to both Arduino and Processing and pulling my hair out over a 'wouldn't it be cool?' project I've started and now have got utterly stuck on. It's certainly a niche kind of thing. Essentially, what I'm attempting to do is transition between three video pieces based on the level of of RF/EMF detected using the Arduino setup found here: https://code.google.com/p/arduino-rfsensor/ this link contains full documentation.

The good news is, this is all working and is outputting data through the serial port, as it should. This is shown as MinScale MaxScale, Analog Value and Decibels (this runs from a value of -60 to -2 at the extremes. These are separated by commas not @ signs as shown in the code linked, I don't know if that makes a difference. The problem I'm having is in Processing at present, I don't know how to extrapolate the decibel value alone as the source, I want it to be interpreted as a kind of off/low - medium and high signal value with the video files kicking in and out as different levels are reached.

At the moment it semi works in that when the Arduino is switched on, the video files are displayed, but seemingly uniformly alternating between them and that's it. No response to changes in value from the Arduino, not that you'll be surprised when you see the code. I'll paste that below - go easy on me, I really do have no idea what I'm doing, but I'm keen to learn for any mad future ideas I dream up.

So there's three key questions... Does the Baud rate from the Arduino serial have a huge bearing on Processing's ability to deal with it? It's set at 115200 at present, not the default value,

The video currently plays at half it's expected speed - it's full HD resolution, which I suppose is not good for a Java based system.. How can I optimise this?

Finally and most importantly, how do I get Processing to deal with the decibel value, or is there a better way of looking at this?

Code at present is as follows, it's been subjected to some speculative tweaking rather than solid knowledge so some of it is likely to not make sense. At present it attempts (badly) to call the analogValue, but I know now that's probably not a good idea. Thanks in advance in a huge way to anyone who can help in the slightest!

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

Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port
float analogValue;

Movie breakUp;
Movie bg;
Movie strongSignal;

void setup() 
{
  size(1920, 1080); // 

  println(Serial.list());

  String portName = Serial.list()[1]; 

  myPort = new Serial(this, portName, 115200);

  bg = new Movie(this, "background.mp4"); //change name inside " " This should be the background video
  breakUp = new Movie(this, "degraded.mp4"); //change name inside " " This should be the distorted video
  strongSignal = new Movie(this, "fullsignal.mp4"); //change name inside " " This should be the video that plays when the
  // signal is strong

  bg.loop(); // loop the background video
}

void draw()
{
  if ( myPort.available() > 0) {  // If data is available from Arduino
    analogValue = myPort.read();         // read it and store it in val

    if (analogValue < 50 ) {

      bg.loop(); // loop the background video

      //tint(255, 0); //remove any previous tint from before

      breakUp.stop();
      strongSignal.stop();

      image(bg, 0, 0, width, height); // place the background image onto the canvas
    }

    if (analogValue > 50 && analogValue < 100) { // You'd have to change the number depending on when you want it to change.

      strongSignal.stop();

      breakUp.loop(); //Start playing the distorted video

      image(bg, 0, 0, width, height); // continue to display the background image

      tint(255, 126); // We can use tint to manage the alpha channel of whatever's below. 
      // so its tint(gray, alpha) - gray should be white - alpha is whatever transparency you want. 

      image(breakUp, 0, 0, width, height); // Start the video that breaks up.
    }

    if (analogValue > 100) {

      breakUp.stop();
      //bg.stop();
Tagged:

Answers

  • With the decibel values, you could write an algorithm that will look at each decibel value and separate the highest, and lowest and store those as int. Then when you receive a new decibel reading, you can look at the highest, lowest, and find if it is a lower number, higher number, lowest, highest, or in between and deal with those numbers accordingly. I recommend that you begin by taking your string input from serial (serial.readString()) and gather your data and convert it to numbers.

    Note, in your current code, line 33 you are using myPort.read(). This is what .read() does: "Returns a number between 0 and 255 for the next byte that's waiting in the buffer. Returns -1 if there is no byte, although this should be avoided by first cheacking available() to see if data is available." If you were to use myPort.readStringUntil() you can wait for your comma separator and then convert your string number into a int that can be sorted out and used by processing.

    I hope this all made sense as it was an ear full, feel free to contact me; I would love to help.

Sign In or Register to comment.