new to processing, need help with data conversion
in
Integration and Hardware
•
8 months ago
hello!
for a little project im working on i need to send data to an arduino via touchosc. i have written code for the arduino that reads data from the serial port in a specific protocol to control and read digital/analog pins, and it works quite well. However, i cant seem to get the processing side to work. the general data that should be written to the serial port is of the form "!1A13255@" where:
! = handshake
1 = arduino ID ( i plan to have multiple hooked up)
A = command for analogWrite
13 = pin number (00-13)
255 = value for analogWrite (000-255)
@ = terminator
so basically I want to read in the fader value from touchOSC and then concatenate that with dynamic values for the arduino ID, command (A,a,D,d), value, and terminator.
How do I go about doing that?
I have posted the code I have so far below. the value "x" prints just fine to the console with numbers 0-255.
- import oscP5.*; // Load OSC P5 library
- import netP5.*; // Load net P5 library
- import processing.serial.*; // Load serial library
- Serial arduinoPort; // Set arduinoPort as serial connection
- OscP5 oscP5; // Set oscP5 as OSC connection
- void setup() {
- size(100,100); // Processing screen size
- noStroke(); // We don’t want an outline or Stroke on our graphics
- oscP5 = new OscP5(this,8000); // Start oscP5, listening for incoming messages at port 8000
- //println(Serial.list());
- arduinoPort = new Serial(this, Serial.list()[1], 9600); // Set arduino to 9600 baud
- }
- void oscEvent(OscMessage theOscMessage) { // This runs whenever there is a new OSC message
- //read in OSC message and save the value of the "orange" fader to val
- String addr = theOscMessage.addrPattern();
- float val = theOscMessage.get(0).floatValue();
- float orangeAmount;
- if (addr.equals("/1/orange")){ orangeAmount = val;}
- //convert that floating value of "val" to an integer from 0-255
- float m = map(val, 0, 1, 0, 255);
- int x = int(m);
- char[] print;
- char handshake = "!";
- char terminator = "@";
- //print[1] = "1";
- //print[2] = "A";
- //print[3] = "1";
- //print[4] = "3";
- //print[1] = char(x);
- //print[2] = "@";
- concat(handshake,terminator);
- }
- void draw()
- {
- }
Thanks for any help
1