Willkommen auf unserem Seminar-Blog

Immer auf dem aktuellen Stand bleiben

Dieser Seminar-Blog befindet sich noch im Aufbau und wird in den kommenden Tagen entsprechend verfeinert.

Member Login

Lost your password?

Registration is closed

Sorry, you are not allowed to register by yourself on this site!

You must either be invited by one of our team member or request an invitation by email at viad.info {at} zhdk {dot} ch.

12 Kommunikation Processing / Arduino

30. November 2011

Das OSC Protokoll (Open-Sound-Control) erlaubt es, auf eine einfache Art und Weise Daten über ein Netzwerk zu schicken. Das Arduino hat leider keine direkte Möglichkeit, dieses Protokoll zu verwenden, weshalb man den ‘Umweg’ über Processing in Kauf nehmen muss. Dieses Beispiel zeigt, wie die Werte eines Potentiometers von Arduino nach Processing geschickt werden und dann via OSC an einen anderen Computer im Netzwerk. Arduino
#define BUFFER_SIZE 20 // actual size of the buffer for integer values: (numberOfValsToRead*6)+(numberOfValsToRead-1)
#define LED_PIN1 11
#define LED_PIN2 10
#define LED_PIN3 9

char incommingBuffer[BUFFER_SIZE]; // buffer to store incomming values
char incomming; // primary buffer to store single incommning bytes
int incommingCounter = 0; // counter for counting the positions inside the buffer
 
int firstValue, secondValue, thirdValue; // fourthValue, fifthValue, ... // add more if needed
 
void setup()
{
  Serial.begin(9600);
  pinMode(LED_PIN1,OUTPUT);
  pinMode(LED_PIN2,OUTPUT);
  pinMode(LED_PIN3,OUTPUT);
}
 
void readSerial()
{
  while(Serial.available())
  {
    incomming = Serial.read(); // read single incommning bytes
 
    if(incomming != '\r') //if no carriage return is received proceed in reading the serial port
    {
      incommingBuffer[incommingCounter++] = incomming; // go on the next position in the buffer
    }
    else //read until a carriage ('\r') is received
    {
      incommingBuffer[incommingCounter] = '\0'; // set the last byte to NULL to sign it for the string operators
 
      char *a = strtok(incommingBuffer, ","); // split the string after delimiters into tokens
      char *b = strtok(NULL, ","); // ...
      char *c = strtok(NULL, ","); // ...
      //char *d = strtok(NULL, ",.;"); // add another line if needed
 
      firstValue = atoi(a); // convert the strings into integers
      secondValue = atoi(b); // ...
      thirdValue = atoi(c); // ...
      //fourthValue = atoi(d); // add another line if needed   
 
      incommingCounter = 0; // reset the counter
      memset(incommingBuffer, 0, BUFFER_SIZE); //overwrite the incommingBuffer
      
    }
  }
}
 
void loop()
{
  readSerial(); // read the values available at the serial port
  analogWrite(LED_PIN1, int(firstValue));
  analogWrite(LED_PIN2, int(secondValue));
  analogWrite(LED_PIN3, int(thirdValue));
 
  Serial.print(firstValue); // debugging
  Serial.print("\t");
  Serial.print(secondValue);
  Serial.print("\t");
  Serial.print(thirdValue);
  Serial.print("\t");
  //Serial.print(fourthValue); // add these lines if needed
  Serial.println(); // send a carriage return for debugging
}
Processing
import processing.serial.*;

Serial myPort;
int potiVal1 = 0;
int potiVal2 = 2;

void setup() {
  size(500,500);
  println(Serial.list());
  myPort = new Serial(this,Serial.list()[0],9600);
  ellipseMode(CENTER);
  noStroke();
  fill(255,255,0,200);
  smooth();
}

void draw() {
  background(0);
  ellipse(250, 250, potiVal1/2, potiVal2/2);
}

void serialEvent(Serial myPort) {
  if(myPort.available() > 0) {
    String completeString = myPort.readStringUntil(10);
    if(completeString != null) {
      trim(completeString);
      println(completeString);
      int[] seperateVal = int(split(completeString,','));
      potiVal1 = seperateVal[0];
      potiVal2 = seperateVal[1];
    }
  }
}