How to make a timer for this program

Hello, This program is receiving various signals from an arduino multiple times a second and for each signal, there's a different image that is supposed to display on the screen. My problem is that the image stays on the screen. I need to make a timer type code so that after a period of time, if the timer was not reset with a new signal, the image will not show. Below is the code:

import processing.serial.*;
Serial myPort;
int val;
PImage img;
boolean[] position = new boolean [4];


void setup()
{
   size(2592,1944);
   String portName = Serial.list()[0];
   println(Serial.list());
   myPort = new Serial(this, portName, 9600);
   img = loadImage("Bayer (1).jpg");
   frameRate(1500);

   for(int i = 0; i<4; i++)
      {
        position[i] = false;
      }
}

void draw()
{

   if (myPort.available() > 0)
   {
      val = myPort.read();
      background(img);

      ////////////////////////////////////////////////
      if(val == 1)
      {
        position[0] = true;
      }
      if(val == 2)
      {
        position[1] = true;
      }
      if(val == 3)
      {
        position[2] = true;
      }
      if(val == 4)
      {
        position[3] = true;
      }
      ///////////////////////////////////////////////
      if(position[0] == true)
      {
        fill(255,0,0);
        rect(900,700,100,100);
      }
      if(position[1] == true)
      {
        fill(255,0,0);
        rect(1260,860,100,100);
      }
      if(position[2] == true)
      {
        fill(255,0,0);
        rect(740,1060,100,100);
      }
      if(position[3] == true)
      {
        fill(255,0,0);
        rect(1120,1240,100,100);
      }
   }
}

Answers

  • edited March 2017

    You need to set the other position-booleans to false when setting one to true. So the code in for example if(val == 1) should look like

    if(val == 1)
      {
        position[0] = true;
        position[1] = false;
        position[2] = false;
        position[3] = false;
      }
    

    alternatively you could replace the position[]-array with a integer variable. Then the whole code would look like:

    import processing.serial.*;
    Serial myPort;
    int val;
    PImage img;
    int position = 0;
    void setup()
    {
       size(2592,1944);
       String portName = Serial.list()[0];
       println(Serial.list());
       myPort = new Serial(this, portName, 9600);
       img = loadImage("Bayer (1).jpg");
       frameRate(1500);
    }
    void draw()
    {
       if (myPort.available() > 0)
       {
          val = myPort.read();
          background(img);
    
          if(val == 1) position = 1;
          else if(val == 2) position = 2;
          else if(val == 3) position = 3;
          else if(val == 4) position = 4;
    
          if(position == 1)
          {
            fill(255,0,0);
            rect(900,700,100,100);
          }
          else if(position == 2)
          {
            fill(255,0,0);
            rect(1260,860,100,100);
          }
          else if(position == 3)
          {
            fill(255,0,0);
            rect(740,1060,100,100);
          }
          else if(position == 4)
          {
            fill(255,0,0);
            rect(1120,1240,100,100);
          }
       }
    }
    

    Note that there could be bugs in my code.

    If my code doesn't work because I dont understand things, you should nevertheless use else if sometimes. Please excuse my bad English

  • edited March 2017

    are all four rects independent? can you see more than one at a time? the following assumes you can.

    you need an int for each image. (this can replace the booleans)

    when you get a signal for that image, set the variable to the time when it expires (millis() + 5000 for instance). when you are drawing the images, check the variable and only draw the image if the current time is less than the expiry time.

    background() and the rect drawing code shouldn't depend on myPort.available though, move it outside the condition.

  • The following post shows the concept and should be use as a reference: https://forum.processing.org/two/discussion/comment/92483/#Comment_92483

    Notice this is untested code below.

    Kf

    import processing.serial.*;
    
    Serial myPort;
    int val;
    PImage img;
    int position = 0;
    
    int nextTime;
    int waitTime=3000; //Wait for 3 seconds
    
    void setup()
    {
       size(2592,1944);
       String portName = Serial.list()[0];
       println(Serial.list());
       myPort = new Serial(this, portName, 9600);
       img = loadImage("Bayer (1).jpg");
       frameRate(1500);
       nextTime=millis()+waitTime;
    }
    void draw()
    {
       boolean showImg=true;
       if(millis()>nextTime)
         showImg=false;
    
    
       if (myPort.available() > 0)
       {
          val = myPort.read();
    
          if(val>=1&&val<=4)
            nextTime=millis()+waitTime;  //Reset time only if valid data received      
    
          if(showImg==true)
            background(img);
    
          if(val == 1) position = 1;
          else if(val == 2) position = 2;
          else if(val == 3) position = 3;
          else if(val == 4) position = 4;
    
          if(position == 1)
          {
            fill(255,0,0);
            rect(900,700,100,100);
          }
          else if(position == 2)
          {
            fill(255,0,0);
            rect(1260,860,100,100);
          }
          else if(position == 3)
          {
            fill(255,0,0);
            rect(740,1060,100,100);
          }
          else if(position == 4)
          {
            fill(255,0,0);
            rect(1120,1240,100,100);
          }
       }
    }
    
Sign In or Register to comment.