how to send data automatically via osc?

edited August 2014 in Library Questions

Hi, I'm a new processing user. I have create this sketch merging two sketches. one was taken from the site of the pulse sensor amped, and the other from an example of osc communication with vvvv. it works, sending the BPM value I need on vvvv. the problem is that I must press the mouse button every time I want to send data. it is possible change the function "mousePressed" with another that sends data automatically every 3 seconds?

thanks a lot!!!

/**
THIS PROGRAM WORKS WITH PulseSensorAmped_Arduino-xx ARDUINO CODE
THE PULSE DATA WINDOW IS SCALEABLE WITH SCROLLBAR AT BOTTOM OF SCREEN
PRESS 'S' OR 's' KEY TO SAVE A PICTURE OF THE SCREEN IN SKETCH FOLDER (.jpg)
MADE BY JOEL MURPHY AUGUST, 2012
*/


import processing.serial.*;
PFont f;

import oscP5.*;
import netP5.*;

OscP5 oscP5;
NetAddress myRemoteLocation;


int firstValue;
float secondValue;
String thirdValue="";
boolean data;





Scrollbar scaleBar;

Serial port;     

int Sensor;      // HOLDS PULSE SENSOR DATA FROM ARDUINO
int IBI;         // HOLDS TIME BETWEN HEARTBEATS FROM ARDUINO
int BPM;         // HOLDS HEART RATE VALUE FROM ARDUINO
int[] RawY;      // HOLDS HEARTBEAT WAVEFORM DATA BEFORE SCALING
int[] ScaledY;   // USED TO POSITION SCALED HEARTBEAT WAVEFORM
int[] rate;      // USED TO POSITION BPM DATA WAVEFORM
float zoom;      // USED WHEN SCALING PULSE WAVEFORM TO PULSE WINDOW
float offset;    // USED WHEN SCALING PULSE WAVEFORM TO PULSE WINDOW
color eggshell = color(255, 253, 248);
int heart = 0;   // This variable times the heart image 'pulse' on screen
//  THESE VARIABLES DETERMINE THE SIZE OF THE DATA WINDOWS
int PulseWindowWidth = 490;
int PulseWindowHeight = 512; 
int BPMWindowWidth = 180;
int BPMWindowHeight = 340;
boolean beat = false;    // set when a heart beat is detected, then cleared when the BPM graph is advanced


void setup() {
  size(700, 600);  // Stage size
  frameRate(100);  
  f = createFont("Arial",16,true);
  textFont(f);
  textAlign(CENTER);
  rectMode(CENTER);
  ellipseMode(CENTER);  


// GO FIND THE ARDUINO
  println(Serial.list());    // print a list of available serial ports
  // choose the number between the [] that is connected to the Arduino
  port = new Serial(this, Serial.list()[0], 115200);  // make sure Arduino is talking serial at this baud rate
  port.clear();            // flush buffer
  port.bufferUntil('\n');  // set buffer full flag on receipt of carriage return




size(400,400);

  f = createFont("Arial",16,true);

  // start oscP5, listening for incoming messages at port 12000
  oscP5 = new OscP5(this, 12000);

  // myRemoteLocation is a NetAddress. a NetAddress takes 2 parameters,
  // an ip address and a port number.
  myRemoteLocation = new NetAddress("127.0.0.1", 12001);

}





















// vvvv talks to Processing via OSC.
// Click the mouse in the window. vvvv will reply. 
//
// vvvv patch can be found in \girlpower\IO\Networking\2_Advanced (OSC) 
// of the vvvv distribution, which can be downloaded here:
// vvvv.org/downloads
//
// This sketch is adapted version of these examples:
// http://www.sojamo.de/libraries/oscp5/examples/oscP5message/oscP5message.pde
// http://www.sojamo.de/libraries/oscp5/examples/oscP5oscArgument/oscP5oscArgument.pde

// **********************
//
// Add the OSC library: 
// 1. Sketch > Import Library > Add Library ...
// 2. Type OSC and choose oscP5
//
// **********************




void draw() {
  background(0); 





  fill(150);
  textFont(f,15);
  text(BPM + "BPM",120, 120);


  // if the oscEvent() function (see below) receives the OSC data it will be drawn 
  fill(255);
  if (data)
  {
    textFont(f,10);
    text(firstValue + " " + secondValue, 10,150);
    textFont(f,15);
    text(thirdValue, 10, 170);
  }
}

void mousePressed() {
  // in the following different ways of creating osc messages are shown by example 

  // this is the address 
  OscMessage myMessage = new OscMessage("/TheDarkSideOfTheMoon");

  myMessage.add(42); /* add an int to the osc message */
  myMessage.add(random(1)); /* add a float to the osc message */
  myMessage.add(BPM); /* add a string to the osc message */
  myMessage.add(new byte[] {0x00, 0x01, 0x10, 0x20}); /* add a byte blob to the osc message */
  myMessage.add(new int[] {1,2,3,4}); /* add an int array to the osc message */

  /* send the message */
  oscP5.send(myMessage, myRemoteLocation); 
}


/* incoming osc message are forwarded to the oscEvent method. */
void oscEvent(OscMessage theOscMessage) {

  /* print the address pattern and the typetag of the received OscMessage */
  print("### received an osc message.");
  print(" addrpattern: "+theOscMessage.addrPattern());
  println(" typetag: "+theOscMessage.typetag());

  /* get and parse the arguments */
  firstValue = theOscMessage.get(0).intValue();
  secondValue = theOscMessage.get(1).floatValue();
  thirdValue = theOscMessage.get(2).stringValue();

  data=true;
}

Answers

  • edited August 2014 Answer ✓

    Hi,

    Please format your code so nobody gets a headache looking at it:

    • in the Processing IDE, hit ctrl+t before copying to the forum
    • in the forum, highlight the code and hit ctrl+k (or click the C button).

    Usually this kind of thing is done like this:

    int intervalTime = 3000;
    int prevTime = 0;
    
    void draw()
    {
      if(millis() > prevTime + intervalTime)
      {
        //send the OSC message
        prevTime = millis();
      }
    }
    
  • edited August 2014

    https://www.dropbox.com/sh/xs4r6crdkv9070j/AAAiRXubWE7K4_Ik-760mzpaa

    you can also open the file "ato" in this link.

    thanks for the

Sign In or Register to comment.