Iphone to OSC to Processing to Arduino

Can someone explain in lame-mans-terms :slight_smile: how data is getting passed around here? I am trying to modify this program to work with a different motor controller.

See Processing code and Arduino code below

//----------- Start Processing code ----------------

import oscP5.;
import netP5.
;
import processing.serial.*;
Serial arduinoPort;
OscP5 oscP5;

float fader = new float [3];

void setup() {
oscP5 = new OscP5(this,8000);
arduinoPort = new Serial(this, Serial.list()[0], 9600);
}

void oscEvent(OscMessage theOscMessage) {

String addr = theOscMessage.addrPattern();

if(addr.indexOf("/1/fader") !=-1){
String list = split(addr,’/’);
int xfader = int(list[2].charAt(5) - 0x30);
if(theOscMessage.get(0).floatValue() !=0){
fader[xfader] = theOscMessage.get(0).floatValue();
}
}
}

void draw() {
//---------------------------------Motor A
if(fader[1] > 0.65){
arduinoPort.write(β€œAF100#”);
}
if(fader[1] < 0.35){
arduinoPort.write(β€œAR100#”);
}
//--------------------------------Motor B
if(fader[2] > 0.65){
arduinoPort.write(β€œBF100#”);
}
if(fader[2] < 0.35){
arduinoPort.write(β€œBR100#”);
}
//----------------------------stop commands
if(fader[1] < 0.65 && fader[1] > 0.35 ){
arduinoPort.write(β€œAF0#”);
}
if(fader[2] < 0.65 && fader[2] > 0.35 ){
arduinoPort.write(β€œBF0#”);
}

}

//---------------------- End Processing Code ------------------

//-----------------------Start Arduino Code--------------

// Test program for SparkFun Ardumoto board
// Copyright (c) 2009 mechomaniac.com

// To use, connect the Arduino to a computer and send commands using a serial terminal.
// eg AR40# motor A forwards with a speed of 40

#define PwmPinMotorA 10
#define PwmPinMotorB 11
#define DirectionPinMotorA 12
#define DirectionPinMotorB 13
#define SerialSpeed 9600
#define BufferLength 16
#define LineEnd β€˜#’

char inputBuffer[BufferLength];

void setup()
{
// motor pins must be outputs
pinMode(PwmPinMotorA, OUTPUT);
pinMode(PwmPinMotorB, OUTPUT);
pinMode(DirectionPinMotorA, OUTPUT);
pinMode(DirectionPinMotorB, OUTPUT);

Serial.begin(SerialSpeed);
}

// process a command string
void HandleCommand(char* input, int length)
{
Serial.println(input);
if (length < 2) { // not a valid command
return;
}
int value = 0;
// calculate number following command
if (length > 2) {
value = atoi(&input[2]);
}
int* command = (int*)input;
// check commands
// note that the two bytes are swapped, ie β€˜RA’ means command AR
switch(*command) {
case β€˜FA’:
// motor A forwards
analogWrite(PwmPinMotorA, value);
digitalWrite(DirectionPinMotorA, HIGH);
break;
case β€˜RA’:
// motor A reverse
analogWrite(PwmPinMotorA, value);
digitalWrite(DirectionPinMotorA, LOW);
break;
case β€˜FB’:
// motor B forwards
analogWrite(PwmPinMotorB, value);
digitalWrite(DirectionPinMotorB, LOW);
break;
case β€˜RB’:
// motor B reverse
analogWrite(PwmPinMotorB, value);
digitalWrite(DirectionPinMotorB, HIGH);
break;
default:
break;

}
}

void loop()
{
// get a command string form the serial port
int inputLength = 0;
do {
while (!Serial.available()); // wait for input
inputBuffer[inputLength] = Serial.read(); // read it in

} while (inputBuffer[inputLength] != LineEnd && ++inputLength < BufferLength);
inputBuffer[inputLength] = 0; // add null terminator
HandleCommand(inputBuffer, inputLength);
}

//---------------------End Arduino Code----------------------

In the Processing code, the arduinoPort.write statements send data to the serial port.

In the Arduino code, Serial.available() reports how many bytes of data are available to be read, and Serial.read() reads one byte.

Pretty simple, really.

What issues are you having?

Basically I need to know what data is passed from Processing and in what format?

What I really need to do is modify the code for the Ardumoto to be able to work with my Sabertooth which supports Analog PWM, or Serial.

I am assuming that Processing sends the arduinoPort.write to the Arduino and the Arduino takes the bytes and decides what to do with them. This is where I am lost :-[

Where is the value in - analogWrite(PwmPinMotorA, value); coming from.

Please bare with me I am new to this and am trying really hard to understand without any formal classes.

I am assuming that Processing sends the arduinoPort.write to the Arduino and the Arduino takes the bytes and decides what to do with them.

Serial communication can be compared to communicating on this forum.

A user posts a requests for information. This is like Processing sending data to the serial port, using the arduinoPort.write command.

The data that gets sent, by the arduinoPort.write command or by the user posting to the forum, needs to be in an agreed upon format. It does no good to post a request for help in the English forums in Scandinavian.

Once data is available to read, the other forum users can read that data (as the Arduino does from the serial port, using the Serial.read() function), and process the data.

The forum is read one character at a time, and stored in a buffer, until the termination character (. on the forum) is encountered. The information is then parsed.

The Serial.read function reads one character at a time. It is doing this in the do/while loop in the loop function. The character is appended to a buffer. Reading stops when the termination character (#) is received.

The parsing is then done in HandleCommand.