To stop and resume loops with an if statement

Asked 2 years ago, Updated 2 years ago, 127 views

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

What I want to do is to stop and resume loading the processing side image based on the value of the switch sent from Arduino.
In the code below, when you press the stop switch, sensors=1 will be sent, and noLoop() in the if statement.However, it cannot resume with the value sensors=2 sent when the other restart switch is pressed.
With loop() in mousePressed(), you can resume by clicking on the mouse, but this time I would like to resume using the value from Arduino.

I look forward to hearing from you.

import processing.serial.*;
Serial myPort;

int numFrames=20;
int sensors;// Store sensor values

Pimage [ ] images = new Pimage [numFrames ];

void setup() {
  background(255);
  size (1280,800);
  frameRate(40);
  imageMode (CENTER);
  // images.resize (1280,800);
  for (inti=0; i<images.length;i++) {
    images[i] = loadImage("animation-"+nf(i,3)+".png");
    images[i].resize(1280,800);
  }

  myPort=newSerial(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){
    image(images[frame], width/2, height/2);
  }
}

void serialEvent (Serialp) {
  sensors = p.read();
  println(sensors);
}

void mousePressed() {
  loop();
}

arduino processing

2022-09-30 21:44

1 Answers

In the first place, why do I need to make noLoop()?Since noLoop() is an instruction that stops calling the draw() function, of course, the processing described in the draw() function will not work until you issue the loop() instruction.
int frame = frameCount %numFrames;
If you want to pause the displayed image (stop updating the frame variable) by dividing the number of frames by 20, why don't you only process frameCount%numFrames when sensors==0 or sensors==2?
In other words,

void draw(){

  int frame;
  if(sensors==0){
    frame = frameCount %numFrames;
    image(images[frame], width/2, height/2);
  } 
  if(sensors==2){
    frame = frameCount %numFrames;
    image(images[frame], width/2, height/2);
  }
}

That's right. I'm sorry if I misunderstood.


2022-09-30 21:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.