Here is a basic circuit:
*Note: You may need to install the “New Ping” library.
/*
* Posted on https://randomnerdtutorials.com
* created by http://playground.arduino.cc/Code/NewPing
*/
#include <NewPing.h>
#define TRIGGER_PIN 11
#define ECHO_PIN 12
#define MAX_DISTANCE 200
// NewPing setup of pins and maximum distance
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup() {
Serial.begin(9600);
}
void loop() {
delay(50);
unsigned int distance = sonar.ping_cm();
// Serial.print(distance);
Serial.write(distance);
//Serial.println("cm");
}
import processing.serial.*;
import processing.video.*;
Serial myPort; // Create object from Serial class
String val; // Data received from the serial port
Integer valNum;
//still image
PImage bg1;
//live video
Capture cam;
//movie
Movie mov;
int newFrame = 0;
void setup()
{
size(1280,720);
bg1 = loadImage("img_01.jpg");
// I know that the first port in the serial list on my mac
// is Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
String portName = Serial.list()[1]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 9600);
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[15]);
cam.start();
}
mov = new Movie(this, "eye_studentAnim.mp4");
// Looping and playing the video
mov.loop();
}
void movieEvent(Movie m) {
m.read();
}
void draw()
{
//open webcam (doesn't show yet)
if (cam.available() == true) {
cam.read();
}
//open and read serial port
if ( myPort.available() > 0)
{ // If data is available,
val = myPort.readStringUntil('\n'); // read it and store it in val
int inByte = myPort.read();
println(inByte);
if(inByte > 0 && inByte< 30){
image(cam,0,0);
ellipse(100, 100, 55, 55);
} else if (inByte >29 && inByte< 100){
image(bg1,0,0);
ellipse(56, 46, 55, 55);
} else {
image(mov,0,0);
}
}
}
Check out the “Frames” Example in Processing > File > Examples > Libraries > Video Movie > Frames and see if you can make the frames change with serial and Arduino (perhaps try a potentiometer).