Hi Forum
I have a question regarding a code made in Processing which is also connected to Arduino.
There is a buzzer which should be pushed and the signal goes directly to Processing and then to Arduino. The Code btw. the Buzzer-Signal should play a video (comes out of a projector) and when the video is at the end, there should be the possibility to push again the buzzern and then video 2 will be shown. The code should always switch between these two videos, but it does not work like sometime it does, sometimes not. I do not find the point where the code makes this issue… Is there anybody who can help?
Code from Processing:
import processing.video.*;
import processing.serial.*;
final String arduinoSerialPort = "/dev/cu.usbmodem641";
final int lf = 10;
final float EPSILON = 0.1;
Serial port = null;
Movie myMovie;
String buffer = null;
boolean isPlaying = false;
Movie[] movies = new Movie[2];
int movieIndex = 0;
void setup() {
fullScreen(P2D);
//size(1080, 1080,P2D);
//frameRate(30);
movies[0] = new Movie(this, "martina.mp4");
movies[1] = new Movie(this, "vanessa.mp4");
// print a list of all available ports
boolean portFound = false;
for (String port : Serial.list())
{
println(port);
if (port.equals(arduinoSerialPort))
{
portFound = true;
}
}
println();
if (!portFound)
{
println("!!! Arduino Serial Port not found: " + arduinoSerialPort);
} else
{
// open serial port
port = new Serial(this, arduinoSerialPort, 9600);
port.clear();
port.readStringUntil(lf);
}
noCursor();
}
void draw() {
if (frameCount < 10)
{
background(55);
// wait 10 frames to be sure the video's are loaded
textAlign(CENTER, CENTER);
textSize(20);
text("loading...", width / 2, height / 2);
return;
}
// serial port handling
if (port != null && port.available() > 0)
{
// read string from arduino into buffer
buffer = port.readStringUntil(lf);
if (buffer != null)
{
// interpret command
if (isPlaying == false && buffer.startsWith("on"))
{
startMovie();
}
}
}
background(0);
// draw the current movie
if (isPlaying)
{
Movie m = movies[movieIndex];
if (m.available()) {
//loads a new frame
try
{
m.read();
}
catch(Exception ex)
{
println("Error movie reading: " + ex.getMessage());
ex.printStackTrace();
return;
}
}
if (abs(m.time() - m.duration()) < EPSILON) {
isPlaying = false;
m.stop();
println("stopped movie [" + movieIndex + "]");
movieIndex = (movieIndex + 1) % movies.length;
}
float x = (width / 2f) - (m.width / 2f);
float y = (height / 2f) - (m.height / 2f);
// show video 1. zentriert / 2. nicht zentriert
image(m, x, y);
//image(m, 0, 0);
}
surface.setTitle("Dinamo FPS: " + frameRate);
}
void startMovie()
{
if (isPlaying == false)
{
movies[movieIndex].play();
isPlaying = true;
println("start movie [" + movieIndex + "]");
}
}
void stopMovie()
{
isPlaying = false;
movies[movieIndex].stop();
println("stop movie");
}
void keyPressed()
{
if (key == 's')
{
startMovie();
}
if (key == 'p')
{
stopMovie();
}
}
Thanks for feedback, Martina