sending value TO an arduino sketch FROM processing

Total newb query. I am using the AFmotor library in Arduino to power a DC motor. I want to get the value for setSpeed of this motor from amplitude taken from real time audio recording.

Regarding arduino: so far i just set the setSpeed value and everything works dandy.

Regarding audio recording: The audio recording part seems to be fine and supercollider is communicating with processing. Im using a supercollider script to take audio input and send the amplitude via OSC to Processing. In Processing i take that value and convert from a float to int.

The Question: how do i send this value to arduino so that i can use it as the setSpeed value?

Thanks.

Arduino:
#include <AFMotor.h>

AF_DCMotor motor(1, MOTOR12_1KHZ); // create motor #2, 64KHz pwm

void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println(“Motor test!”);

motor.setSpeed(255); // set the speed to 200/255
}

void loop() {
Serial.print(“tick”);

motor.run(FORWARD); // turn it on going forward
delay(200);

Serial.print(“tock”);
motor.run(BACKWARD); // the other way
delay(130);

Serial.print(“tack”);
motor.run(RELEASE); // stopped
delay(1500);
}


Processing:

import processing.serial.;
import cc.arduino.
;
import oscP5.*;
OscP5 oscP5;
Arduino arduino;
int ledPin = 9;

void setup() {
size(400,800, P2D);
frameRate(24);
smooth();
background(0);
oscP5 = new OscP5(this, 30000);
arduino = new Arduino(this, Arduino.list()[0], 57600);
arduino.pinMode(ledPin, Arduino.OUTPUT);
}

void draw(){}

void oscEvent(OscMessage theOscMessage) {
if(theOscMessage.checkAddrPattern("/amp")==true) {
float val = theOscMessage.get(0).floatValue() * 5000;
int intval = (int)val;
if(val > 255) {
val = 255;}
println(val);
background(0);
rect(200, height-(val*height), 30, 30);
arduino.analogWrite(ledPin, intval);
return;
}
}

void mousePressed(){
background(0);
}


SuperCollider

(
SynthDef(\amponset,{
var in, amp, freq, hasFreq, signal;
in = SoundIn.ar(0);
amp = Amplitude.kr(in, 0.05, 0.05);
//amp.poll;
SendTrig.kr(Impulse.kr(20), 555, amp);
}).load(s);
)

(
Synth(\amponset);
o.remove; // remove old responders
n = NetAddr(“127.0.0.1”, 30000); // initialise Processing channel
o = OSCresponderNode(nil, ‘/tr’, {arg time, responder, msg;
// msg.postln;
n.sendMsg("/amp", msg[3]); // send to Processing
}).add;
)

with something like this?
receiver (arduino):

int recv() { // receive 15 bit; -1 means error
  if (Serial.available() < 4) return -1; // we use 4 byte
  if (Serial.read() != 'S') return -1; // 'S' is the "start byte"
  const uint8_t i = Serial.read(); // high-byte
  if (i >= 128) return -1; // just 7 bits allowed here
  const uint8_t j = Serial.read(); // low-byte
  if (Serial.read() != (uint8_t)(i^j)) return -1; // check parity (high-byte XOR low-byte)
  return i*256 + j;
}

-arne