...
println(Serial.list());
myPort = new Serial(this, Serial.list()[0]"/dev/cu.usbmodem12131", 9600);
So sieht die Ausgabe bei Processing typischerweise aus…
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
import processing.serial.*; // Import the Processing Serial Library for communicating with arduino Serial myPort; // The used Serial Port int firstValue, secondValue; // fourthValue, fifthValue, ... // add more if needed void setup() { 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]"/dev/cu.usbmodem12131", 9600); // Open a new port and connect with Arduino at 9600 baud } void draw() { } void serialEvent(Serial myPort) // Is called everytime there is new data to read { if (myPort.available() > 0) { String completeString = myPort.readStringUntil(10'\n'); // Read the Serial port until there is a linefeed/carriage return if (completeString != null) // If there is valid data insode the String { completeString = trim(completeString); // Remove whitespace characters at the beginning and end of the string String seperateValues[] = split(completeString, ","); // Split the string everytime a delimiter is received firstValue = int(seperateValues[0]); secondValue = int(seperateValues[1]); } } } |
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
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]"/dev/cu.usbmodem12131", 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"); delay(1000); } |
...
toInt()
Diese Funktion wandelt einen Sting in einen Int
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
#define LED_PIN 6 #define NUM_OF_VALUES 3 String incomingString =""; int incomingValues[NUM_OF_VALUES]; void setup() { Serial.begin(9600); pinMode(LED_PIN, OUTPUT); } void loop() { if (Serial.available() > 0) { incomingString = Serial.readStringUntil('\n'); split(incomingString, incomingValues, NUM_OF_VALUES); } analogWrite(LED_PIN, incomingValues[0]); } void split(String inputString, int returnData[], int numOfValues) { int index = 0; int lastPos = 0; for(int i=0; i<inputString.length();i++) { if(inputString.charAt(i) == ',' && index < numOfValues) { String tempStr = inputString.substring(lastPos,i-1); returnData[index] = tempStr.toInt(); index++; lastPos = i+1; } } } |
...