Button Video Playback Project (Processing + Arduino)

Hey guys!

As my first project using Arduino I'm trying to make a movie start and stop by the press of a button. I'm pretty new to coding so under the advise of some friends I'm using Processing to put this project forward.

So far, with the code as it the video starts with the program and when the button is pressed it only stops for a fraction of a moment. I tried changing the variables for when the video start (set the Arduino to send 1 when LOW instead of HIGH. On processing changed the value that trigger the video). And also trying messing up with the delay() in the Arduino side trying to see if would change something, so far nothing.
I can't see where the mistakes are.

Any help would be welcome!

Thanks.

Follow the codes:

Arduino

int switchPin = 4;                       // Switch connected to pin 4

void setup() {
  pinMode(switchPin, INPUT);             // Set pin 0 as an input
  Serial.begin(9600);                    // Start serial communication at 9600 bps
}

void loop() {
  if (digitalRead(switchPin) == HIGH) {  // If switch is ON,
    Serial.write(0);               // send 1 to Processing
  } else {                               // If the switch is not ON,
    Serial.write(1);               // send 0 to Processing
  } 
  delay(100);                            // Wait 100 milliseconds
}

Processing:

import processing.video.*;
import processing.serial.*;
Serial port;
Movie myMovie;
int val = 0;
void setup() {
 size(960, 540);
 background(0);
 myMovie = new Movie(this, "Video Dec 08, 19 20 04.mov");
 myMovie.loop();

 println(Serial.list());
 // print a list of all available ports

 port = new Serial(this, Serial.list()[1], 9600);
 // choose the port to which the Arduino is connected
 // on the PC this is usually COM1, on the Macintosh
 // this is usually tty.usbserial-XXX
}
void draw() {
 background(255);

 if (0 < port.available()) {
 val = port.read();
 }

 image(myMovie, 0, 0);

 if (val == 0) {
 myMovie.speed(1);
 } else {
 myMovie.speed(0);
 }
}
// Called every time a new frame is available to read
void movieEvent(Movie m) {
 m.read();
}

You need to look at the state change detection example. You want to send something only when the state of the switch changes.

 if (0 < port.available()) {

Do you think that way? Most people do not.

 if (port.available() > 0)
 {

is how most people think.

 if (val == 0) {
 myMovie.speed(1);
 } else {
 myMovie.speed(0);
 }

Regardless of whether there was new serial data to read, diddle with the movie speed based on the last serial input. Why?