However, after spending another 3 hours working on the code with Spencer, the turntable will now automatically rotate to a set co-ordination whilst playing an animation. Once it has reached to the co-ordinations, the current running animation will end and another animation will play. I can set the variables of the animations on whether I want the animations to rotate with the turntable or to get the turntable to rotate to the specific co-ordination and then play the animation.
Some of the animations will be static (i.e. stay in the same place and play) or moving (i.e. play with the turntable rotating).
The coding for this concept was quite difficult and complicated. There wasn't really a simple way around it either. However, finally after 3 hours of coding and testing, the turntable is now working as instructed to the new set of coding.
Here is the full coding of Processing:
This is how I am instructing the turntable to rotate and play the animations, etc.
I have added notes to those areas of the coding to explains how this concept works.
import processing.serial.*;
import jmcvideo.*;
import processing.opengl.*;
import javax.media.opengl.*;
int fcount=0;
int clickedval=0;
int first=1;
int cansend=1;
int playing=0;
int coords=0;
String thisSlice="";
String lastSlice="";
String showVal;
//PImage maskthing;
int xpos=0;
int incval=1;
JMCMovieGL myMovie;
JMCMovieGL myMovie2;
PFont myFont;
int pvw, pvh;
Serial myport;
int[] coordinates={0,20,30,30,40,50,0}; //specific co-ordinations
String[] movs={"bird.mov","sky_divers.mov","01_squirrel.mov","02_squirrel.mov","leaves.mov","snow_falling.mov","blank.mov"};
//List of all the videos that will be loaded when requested to. They are listed in order of the specific co-ordinations.
int[] doesMove={1,0,1,0,1,0,1}; //States whether the animation is Static or moving. 1=Moving 0=Static. The order of these are listed in order for the animations and co-ordinations above.
String lastVideo=""; //Variable for last animation
int goPlace=0; //Variable for setting target co-ordination
int triggerVid=1; //1 or 0 (true or false)
int offsetter=0;
int vidNum = 0;
All the coding above is setting variables that will be used in statements below.
void setup() {
size(800,600, OPENGL);
frame.setResizable(true);
background(255);
myport=new Serial(this, Serial.list()[0],9600);
myMovie = movieFromDataPath("bird.mov");
myMovie.loop();
myFont = createFont("FFScala", 32);
//maskthing=loadImage("mask.png");
textFont(myFont);
}
void draw()
{
background(255);
if(doesMove[clickedval]==0 && playing==1){
if(myMovie.getPlaybackPercentage()>.99){
println("movie finished");
clickedval=clickedval+1;
goPlace=goPlace+1;
println("interested in mov:"+movs[clickedval]+" goplace="+coordinates[clickedval]+" doesmove="+doesMove[clickedval]);
triggerVid=1;
cansend=1;
if(doesMove[clickedval]==1){
offsetter=-1;
}
The above coding is basically telling the turntable to rotate automatically and to jump to the next co-ordination that are stated above. It also tells Arduino to print line the following statements:
"interested in mov:"+movs[clickedval]+" //
"goplace="+coordinates[clickedval]+" //
"doesmove="+doesMove[clickedval]" //
With these statements printing, I can tell that code is working.
}
}
if(triggerVid==1 && cansend==1){
println("triggering");
cansend=0;
triggerVid=0;
clickedval=goPlace;
println("in range");
if(doesMove[clickedval]==1){
myport.write(coordinates[(clickedval+1)+offsetter]);
offsetter=0;
//The coding above is telling the turntable to rotate to the next co-ordination
if(!lastVideo.equals("blank.mov")){ //if its not the last video,
if(first==0) myMovie.dispose(); //dispose the video, this is so this concept does not slow down whilst repeating the whole instructions.
myMovie = movieFromDataPath(movs[clickedval]);
//delay(200);
first=0;
lastVideo=movs[clickedval];
myMovie.loop();
}
playing=1;
}
//The above coding instructs the animation to play and loop as it is rotating with the turntable.
println("c="+coordinates[clickedval]);
if(doesMove[clickedval]==0){
playing=0;
myport.write(coordinates[clickedval]);
}
}
if(myport.available()>0){
int dat=myport.read();
if(dat==13){
//println(thisSlice);
if(thisSlice.length()==4){
println("playvideo");
if(lastVideo.equals("blank.mov")){
offsetter=0;
goPlace=0;
triggerVid=1;
cansend=1;
lastVideo="";
clickedval=0;
}
//The above coding is instructing the turntable that if the last video is "blank.mov" reset all the variables.
else{
if(clickedval>-1 && doesMove[clickedval]==1){
if(!lastVideo.equals("blank.mov")){
clickedval=clickedval+1;
goPlace=goPlace+1;
//If its not the last video "blank.mov" then go to the next co-ordinations and plus one.
println("Loading:"+movs[clickedval]);
if(!lastVideo.equals(movs[clickedval])){
if(first==0) myMovie.dispose();
myMovie = movieFromDataPath(movs[clickedval]);
//delay(200);
first=0;
lastVideo=movs[clickedval];
myMovie.play();
}
playing=1;
}
}
//The above coding is telling the turntable to play the animation to the next co-ordination whilst rotating.
if(clickedval>-1 && doesMove[clickedval]==0){
// clickedval=clickedval+1;
// goPlace=goPlace+1;
println("Loading:"+movs[clickedval]);
if(!lastVideo.equals(movs[clickedval])){
if(first==0) myMovie.dispose();
myMovie = movieFromDataPath(movs[clickedval]);
//delay(200);
first=0;
lastVideo=movs[clickedval];
myMovie.play();
}
playing=1;
}
}
}
lastSlice=thisSlice;
thisSlice="";
}else{
thisSlice=thisSlice+char(dat);
}
}
if(playing==1){
if(myMovie.getPlaybackPercentage()>.01){ //This tells processing to play the animation but missing the first few frames. This was added to stop the animations flickering in between each animations.
PGraphicsOpenGL pgl = (PGraphicsOpenGL) g;
GL gl = pgl.beginGL();
{
if (pvw != width || pvh != height)
{
background(255);
gl.glViewport(xpos,0, width, height);
pvw = width;
pvh = height;
}
if(xpos>1600){
xpos=-800;
}
background(255);
gl.glViewport(xpos,0, width, height);
myMovie.centerImage(gl);
// xpos=xpos+incval;
}
pgl.endGL();
}
}
//image(maskthing,0,0);
stroke(255);
fill(255);
if(lastSlice.length()<4){
showVal=lastSlice;
}
//The above coding is used to mask the animation when being played.
fill(100);
text(showVal,390,30);
for(int lop=0;lop
rect(10+(lop*20),20,10,10);
//The coding above is for the text of the co-ordination that is displayed when running Processing and also for the rectangles that are drawn for each of the specific co-ordinations displayed in the top left hand side of the screen. For the final run of this coding I will delete these as these won't be necessary.
if(mousePressed==false){
cansend=1;
}
if(mousePressed==true && cansend==1){
println("got click");
if((mouseY>20 && mouseY<30)>10+(lop*20) && mouseX<20+(lop*20))){
cansend=0;
clickedval=lop;
println("in range");
myport.write(coordinates[lop]);
println("c="+coordinates[lop]);
playing=0;
}
}
}
//////
}
JMCMovieGL movieFromDataPath(String filename)
{
return new JMCMovieGL(this, filename, RGB);
}
The whole of the coding above plays the animations that I have done so far. If I add more animations, all I will need to do is to add in the co-ordinations and whether the animation is static or moving at the top of the coding and also add the name of the file to the list.
No comments:
Post a Comment