...
Code Block |
---|
language | java |
---|
title | Beispiel Processing zu Arduino |
---|
collapse | true |
---|
|
import processing.serial.*; // Import the Processing Serial Library for communicating with arduino
Serial myPort; // The used Serial Port
void setup()
{
background(0);
size(500, 500);
println(Serial.list()); // Prints the list of serial available devices (Arduino should be on top of the list)
myPort = new Serial(this, Serial.list()[0], 9600); // Open a new port and connect with Arduino at 9600 baud
}
void draw()
{
int firstValue = 111;
int secondValue = 222;
int thirdValue = 333;
myPort.write(str(firstValue));
myPort.write(",");
myPort.write(str(secondValue));
myPort.write(",");
myPort.write(str(thirdValue));
myPort.write(",\n");
myPort.write("r");
delay(1000);
} |
Um auf der Seite des Arduino auf eingehende Nachrichten über die serielle Schnittstelle zu horchen müssen wir einige Funktionen benutzen. Diese sind:
Code Block |
---|
language | java |
---|
title | Beispiel Werte in Arduino empfangen |
---|
collapse | true |
---|
|
#define LED_PIN 6
String incomingString ="";
int incomingValues[3];
void setup()
{
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
}
void loop()
{
if (Serial.available() > 0)
{
incomingString = Serial.readStringUntil('\n');
split(incomingString,incomingValues);
}
analogWrite(LED_PIN, incomingValues[0]);
}
void split(String inputString, int *returnData)
{
int index = 0;
int lastPos = 0;
for(int i=0; i<inputString.length();i++)
{
if(inputString.charAt(i) == ',')
{
String tempStr = inputString.substring(lastPos,i-1);
returnData[index] = tempStr.toInt();
index++;
lastPos = i+1;
}
}
} |