This are my first steps in Programming and Processing and I wanted to several videos based on several sensor values from an arduino. However the variable myVideo gives a null and Java is not happy with that and so am I. The strange thing is that after some tries it suddenly does work. Anyone having a clue how this might be?
/*
* Sketch to start playing the right videofile when a certain sensor value is communicated (processing code)
*/
import processing.serial.*; //two libraries are added, one for the serial communication and the other one to play the movie files.
import processing.video.*;
Movie myMovie;
Serial port;
int pageValue; //This variable is later used to store the data input from the arduino
void setup() {
size(800, 480, P3D);//800x480 is the resolution of the video files, this is allready pretty sluggish and the intention was to play it on a 4" screen therefore the resolution is not higher.
noStroke(); //P3D is added to the setup so the videofile starts playing actually, otherwise processing will just crash.
port = new Serial(this, Serial.list()[0], 9600);
}
void draw() {
if (0 < port.available()) { //Check to see if there is actually data for me available
pageValue = port.read(); // Here I store the information from the datacommunication in the variable pageValue
pageCheck(); //Here the function pageCheck is called which starts to play videofiles determined on which page you are.
}
background(204);
println(pageValue); //Check to see what values the variable pageValue holds, another check to see how the communication is going and where an error occurs.
println(myMovie);
image(myMovie, 0, 0);
}
void movieEvent(Movie myMovie) {
myMovie.read();
}
boolean played1 = false;
boolean played2 = false;
boolean played3 = false;
boolean played4 = false;
boolean played5 = false;
boolean played6 = false;
boolean played7 = false;
boolean played8 = false;
void pageCheck() { //This function checks which value the variable pageValue is and then selects the right videofile for that and then starts playing that file. I could have done this in an array but because of all the troubles I did not manage to make this in time.
if (pageValue == 1 && played1 == false) {
played1 = true;
myMovie = new Movie(this, "pagina 5.mp4");
myMovie.play();
image(myMovie, 0, 0);
}
if (pageValue == 2 && played2 == false) {
played2 = true;
myMovie = new Movie(this, "pagina 7.mp4");
myMovie.play();
image(myMovie, 0, 0);
}
if (pageValue == 3 && played3 == false) {
played3 = true;
myMovie = new Movie(this, "pagina 9.mp4");
myMovie.play();
image(myMovie, 0, 0);
}
if (pageValue == 4 && played4 == false) {
played4 = true;
myMovie = new Movie(this, "pagina 11.mp4");
myMovie.play();
image(myMovie, 0, 0);
}
if (pageValue == 5 && played5 == false) {
played5 = true;
myMovie = new Movie(this, "pagina 13.mp4");
myMovie.play();
image(myMovie, 0, 0);
}
if (pageValue == 6 && played6 == false) {
played6 = true;
myMovie = new Movie(this, "pagina 15.mp4");
myMovie.play();
image(myMovie, 0, 0);
}
if (pageValue == 7 && played7 == false) {
played7 = true;
myMovie = new Movie(this, "pagina 17.mp4");
myMovie.play();
image(myMovie, 0, 0);
}
if (pageValue == 8 && played8 == false) {
played8 = true;
myMovie = new Movie(this, "pagina 19.mp4");
myMovie.play();
image(myMovie, 0, 0);
}
The arduino code sends 1,2,3,4,5,6,7 or 8 through the serial port continuously.