...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
import processing.serial.*; Serial myPort; void setup() { size(500, 500); // print all available ports println(Serial.list()); // open specific serial port myPort = new Serial(this, "/dev/cu.usbmodem14621", 9600); } void draw() { // prepare values int firstValue = 111; int secondValue = 222; int thirdValue = 333; // write values myPort.write(str(firstValue)); myPort.write(","); myPort.write(str(secondValue)); myPort.write(","); myPort.write(str(thirdValue)); myPort.write("\n"); } |
...
toInt()
Diese Funktion wandelt einen Sting in einen Int
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
#define NUM_VALUES 3 int incomingValues[NUM_VALUES]; void setup() { pinMode(LED_PIN, OUTPUT); // open the serial port with a baudrate of 9600 Serial.begin(9600); } void loop() { // check if data is available if (Serial.available() > 0) { // read string String str = Serial.readStringUntil('\n'); // split data and write to incomingValues split(str, incomingValues, NUM_VALUES); } // take first value and set LED analogWrite(6, 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; } } } |
...