Execute OSCsend only once within draw() function

For my Simon Game I want to execute a sound only once by sending a message to Pure Data (Pd), as soon as the color appear.
Have you any tipps, how to do that?

import oscP5.*;
import netP5.*;

OscP5 oscP5;
NetAddress myRemotePd;
int pressedButton = -1;
int port = 12001;

boolean isPlayerReady = false;
ArrayList<Integer> gameList;
ArrayList<Integer> playerList;
int counter = 8;
int gameListPosition = 0;

int tone = -1;

PImage bg;
PImage green;
PImage red;
PImage blue;
PImage yellow;

int startTime;
int timeOut;
float DISPLAY_DURATION = 250;


void setup()
{
  gameList = new ArrayList<Integer>();
  
  for (int i = 0; i < ROUNDS; i++)
  {
      gameList.add(int(random(1, 5)));
  }
  
  println(gameList);
  
  size(685, 565);
  // oscP5 listens for incoming messages at port 1000
  oscP5 = new OscP5(this, port);
  bg = loadImage("img/Simon.png");
  green = loadImage("img/green.png");
  red = loadImage("img/red.png");
  blue = loadImage("img/blue.png");
  yellow = loadImage("img/yellow.png");
  myRemotePd = new NetAddress("localhost", 12002);
  startTime = millis();
  //timeOut = millis() + 2000;
  timeOut = millis() + 1000;
}

void draw()
{
  background(bg);

  if (!isPlayerReady)
  {
    if (gameListPosition == counter)
    {
        isPlayerReady = !isPlayerReady;
    }
    
    if (millis() >= timeOut)
    {
      timeOut = millis() + 300;
      startTime = millis();
      tone = gameList.get(gameListPosition);
      gameListPosition++;
      
    }
         
    if (gameListPosition != counter)  
    {
      println(tone);      
      if (millis() - startTime < DISPLAY_DURATION)
      {
        OscMessage myMessage = new OscMessage("/test");
        if (tone == 1)
        {
          image(green, 90, 30); 
          myMessage.add(1); /* add an int to the osc message */
          /* send the message */
          oscP5.send(myMessage, myRemotePd);
        }
        else if (tone == 2)
        {
          image(red, 325, 30);
          myMessage.add(2); /* add an int to the osc message */
          /* send the message */
          oscP5.send(myMessage, myRemotePd);
        }
        else if (tone == 3)
        {
          image(yellow, 90, 260);
          myMessage.add(3); /* add an int to the osc message */
          /* send the message */
          oscP5.send(myMessage, myRemotePd);
        }
        else if (tone == 4)
        {
          image(blue, 325, 260);
          myMessage.add(4); /* add an int to the osc message */
          /* send the message */
          oscP5.send(myMessage, myRemotePd);
        }
      }
    }
  }
}

1 Like

You could keep track of the frameCount , and only execute in an if block if one is added.

2 Likes

The frameCount is a good hint, but I found another way to solve this:

boolean soundOn = false;
...
if (millis() >= timeOut)
    {
      timeOut = millis() + 300;
      startTime = millis();
      tone = gameList.get(gameListPosition);
      gameListPosition++;
      soundOn = !soundOn;
    }

...
    if (soundOn)
    {
          //code to execute once
    }