We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello everyone! I have an arduino button that is read from processing via serial. HIGH is 1 and LOW is 0. This is my first code and I made it cutting some codes from examples. I think is not very clean and as I have no experience I will be happy if someone could check it! I need that processing read the button input and when the button is LOW the software should send an OSC message and when the button is HIGH processing should send an other one.
EVERYTHING WORKS but when processing read the button state it send an infinite number of corresponding OSC message instead only one. If i press with my mouse in the processing window (when the script is playing) the message inside void mousePressed() is send only once instead if I use my button on arduino it will be sent infinitely and I think is better to send only once...! Right?
Here the code (I know there are some visual input and other just to have a test feedback but please could you check it please? Thanks!
import oscP5.*;
import netP5.*;
import processing.serial.*;
OscP5 oscP5;
NetAddress myRemoteLocation;
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
void setup() {
size(200,200);
String portName = Serial.list()[0];
myPort = new Serial(this, "COM4", 9600);
// start oscP5, telling it to listen for incoming messages at port 7000
oscP5 = new OscP5(this,7000);
// set the remote location to be the localhost on port 7000
myRemoteLocation = new NetAddress("127.0.0.1",7000);
}
void draw()
{
// This is just to have a visual feedback
if ( myPort.available() > 0) { // If data is available,
val = myPort.read(); // read it and store it in val
}
background(255); // Set background to white
if (val == 0) { // If the serial value is 0,
fill(0); // set fill to black
}
else { // If the serial value is not 0,
fill(204); // set fill to light gray
}
rect(50, 50, 100, 100);
// THIS IS WHAT I REALLY NEED: THE IF STATEMENT THAT SEND OSC
if ( myPort.available() > 0) { // If data is available,
val = myPort.read(); // read it and store it in val
}
if (val == 0) { // If the serial value is 0,
// create an osc message
OscMessage myMessage = new OscMessage("/activeclip/audio/position/direction");
myMessage.add(2); // add an int to the osc message
myMessage.add(12.34); // add a float to the osc message
myMessage.add("Pause the Music!");
oscP5.send(myMessage, myRemoteLocation);
}
else {
OscMessage myMessage = new OscMessage("/activeclip/audio/position/direction");
myMessage.add(1); // add an int to the osc message
myMessage.add(12.34); // add a float to the osc message
myMessage.add("Play the Music!");
oscP5.send(myMessage, myRemoteLocation);
}
}
// This is a mouse OSC test from examples
void mousePressed() {
// create an osc message
OscMessage myMessage = new OscMessage("/test");
myMessage.add(123); // add an int to the osc message
myMessage.add(12.34); // add a float to the osc message
myMessage.add("some text!"); // add a string to the osc message
// send the message
oscP5.send(myMessage, myRemoteLocation);
}
//This will read the OSC
void oscEvent(OscMessage theOscMessage)
{
// get the first value as an integer
int firstValue = theOscMessage.get(0).intValue();
// get the second value as a float
float secondValue = theOscMessage.get(1).floatValue();
// get the third value as a string
String thirdValue = theOscMessage.get(2).stringValue();
// print out the message
print("OSC Message Recieved: ");
print(theOscMessage.addrPattern() + " ");
println(firstValue + " " + secondValue + " " + thirdValue);
}
I don't know and even think what is and if I need the OSC fload and string Value but for now I leaved there on the code... Hope someone could give me a feedback. Thank you!
Answers
you could us a minimal delay of 2
also store the old value and only when changed display osc: oldVal=val;
Thank you Chrisir for youl help. I've add the dalay you suggest in my if function after oscP5.send(myMessage, myRemoteLocation); but nothing changed!
I've found an other solution: As I need only 2 messages and as the difference from both is an integrer I used this method (with "state" varable!) which change only the number (from 1 to 2):
Anyway even if I don't need it I'm curious to know better how to display 2 completely different messages with the same if statement I used as I didn't found any solution! Hope u can help me again! Thank You