Home>

I am making a work using serial communication between Arduino and processing.
The switch ON/OFF data obtained from the two tact switches connected to Arduino is sent to Processing. I want to use the two tact switches for stopping and restarting.
Processing reads 20 images and displays them.

What I want to do is to stop/resume the loading of the image on the Processing side according to the value of the switch sent from Arduino.
In the following code, when the stop switch is pressed, sensors = 1 is sent, and reading of the image is stopped by noLoop () in the if statement. However, it cannot be resumed with the value sensors = 2 sent when the other resume switch is pressed.
It can be resumed by clicking the mouse with loop () in mousePressed (), but this time I want to resume using the value from Arduino.

Please give me an answer.

import processing.serial. *;
Serial myPort;
int numFrames = 20;
int sensors;// Store the sensor value
PImage [] images = new PImage [numFrames];
void setup () {
  background (255);
  size (1280, 800);
  frameRate (40);
  imageMode (CENTER);
  for (int i = 0;i<images.length;i ++) {
    images [i] = loadImage ("animation-" + nf (i, 3) + ". png");
    images [i] .resize (1280, 800);
  }
  myPort = new Serial (this, "/dev/cu.usbmodem143401", 9600);
}
void draw () {
  int frame = frameCount% numFrames;
  if (sensors == 0) {
    image (images [frame], width/2, height/2);
  }
  if (sensors == 1) {
    // image (images [frame], width/2, height/2);
    noLoop ();
  }
  if (sensors == 2) {
    loop ();
  }
}
void serialEvent (Serial p) {
  sensors = p.read ();
  println (sensors);
}
void mousePressed () {
  loop ();
}
  • Answer # 1

    I don't have an Arduino, so it looks like

    void draw () {
      int frame = frameCount% numFrames;
      if (sensors == 0) {
        image (images [frame], width/2, height/2);
      }
    // Go to serialEvent
    // if (sensors == 1) {
    // noLoop ();
    //}
    // if (sensors == 2) {
    // loop ();
    //}
    }
    void serialEvent (Serial p) {
      sensors = p.read ();
      println (sensors);
      if (sensors == 1) {
        noLoop ();
      }
      if (sensors == 2) {
        loop ();
      }
    }


    Is this okay? I'm sorry if I removed it.

  • Answer # 2

    This is a completely different method, but it has led to self-solving.
    It is a method to export 20 images in advance as one video, read it, and stop/resume with pause () and play ().
    However, it seems heavy to load videos one by one, so I think TN8001's answer is the best way.

    import processing.serial. *;
    Serial myPort;
    import processing.video. *;
    Movie myMovie;
    int sensors = 0;
    void setup () {
      size (1920, 800);
      background (0);
      frameRate (60);
      myMovie = new Movie (this, "myMovie.mov");
      myMovie.loop ();
        myPort = new Serial (this, "/dev/cu.usbmodem143401", 9600);
    }
    void draw () {
      background (255);
      if (sensors == 0) {
        image (myMovie, width/2, height/2);
      }
      if (sensors == 1) {
         myMovie.pause ();
      }
      if (sensors == 2) {
        myMovie.play ();
      }
    }
    void movieEvent (Movie m) {
      m.read ();
    }
    void serialEvent (Serial p) {
      sensors = p.read ();
      println (sensors);
    }