...
Hier ein Beispiel, welches die Werte von zwei Sensoren über die serielle Schnittstelle sendet:
| Code Block | ||||
|---|---|---|---|---|
| ||||
void setup()
{
// open the serial port with a baudrate of 9600
Serial.begin(9600);
}
void loop()
{
// reading of pins
int firstValue = analogRead(A0);
int secondValue = analogRead(A1);
// writing of a single line to serial "100,200"
Serial.print(firstValue);
Serial.print(',');
Serial.print(secondValue);
Serial.println(); // <- finish of line
} |
...
Hier ein Beispiel dass die beiden Werte ausliest und eine Ellipse zeichnet.
| Code Block | ||||
|---|---|---|---|---|
| ||||
import processing.serial.*;
Serial myPort;
int firstValue, secondValue;
void setup()
{
size(500, 500);
// print all available ports
println(Serial.list());
// open specific serial port
myPort = new Serial(this, "/dev/cu.usbmodem12131", 9600);
}
void draw()
{
// draw ellipse
background(0);
fill(255);
ellipse(width/2, height/2, firstValue, secondValue);
}
void serialEvent(Serial myPort)
{
// check if data is available
if (myPort.available() == 0)
{
return;
}
// read a line as a string
String str = myPort.readStringUntil('\n');
if (str == null)
{
return;
}
// remove leading and trailing whitespace
str = trim(str);
// split string
String vals[] = split(str, ",");
if (vals.length != 2) {
return;
}
// convert values
firstValue = int(vals[0]);
secondValue = int(vals[1]);
println(firstValue, secondValue);
} |
...