zurück


Kommunikation Processing zu Arduino Board

Um von Processing Daten zum Arduino Board zu schicken wird ebenfalls die serielle Schnittstelle des Boards verwendet.


Hier ein Beispiel welches per Mausdruck auf den Processing Sketch die im Arduino Board verbaute Led (Pin 13) ein- und ausschaltet:


Processing Code


/*

 switch to turn on/off led in arduino

 */

// libraries
import processing.serial.*;
// serial connection
Serial port;
// toggle light
int toggleLight = 0;

void setup() {

  // size
  size(800, 600);
  // print all available serial ports
  println(Serial.list());
  // init connection to arduino
  port = new Serial(this, Serial.list()[0], 57600);

}

void draw() {

  // clear background (check toggle light)
  if(toggleLight == 0){
    background(0);
  }
  else{
    background(255);
  }

}

void mousePressed(){

  // clear background (check toggle light)
  if(toggleLight == 0){
    toggleLight = 1;
  }
  else{
    toggleLight = 0;
  }
  // write message to arduino
  port.write(Integer.toString(toggleLight));
  // write line break
  port.write(13);

}


Arduino Code


Um Daten in Arduino zu empfangen verwenden wir die Messenger Library (http://www.arduino.cc/playground/Code/Messenger).


// libraries
#include <Messenger.h>
// instantiate Messenger object with the default separator (the space character)
Messenger message = Messenger(); 

// led pin
int ledPin = 13;

void setup() {
  // init serial connection
  Serial.begin(57600);
  // pin modes
  pinMode(ledPin,OUTPUT);
}

void loop() {
  // The following two lines is the most effective way of using Serial and Messenger
  while ( Serial.available() ) {
    if ( message.process(Serial.read() ) ){
      digitalWrite(ledPin,message.readInt()); // This command simpy echoes the first integer of a message
    }
  }
}