Atlassian uses cookies to improve your browsing experience, perform analytics and research, and conduct advertising. Accept all cookies to indicate that you agree to our use of cookies on your device. Atlassian cookies and tracking notice, (opens new window)
Confluence
/
Arduino und Processing
Updated Oct 10, 2018

Arduino und Processing

Oct 10, 2018

Analytics

Loading data...

Da Arduino auf Processing basiert eignen sich die beiden auch perfekt dazu Daten untereinander auszutauschen. Dazu verwenden wir die serielle Schnittstelle "Serial" die das asynchrone Austauschen von Daten ermöglicht.

Arduino zu Processing

Auf der Arduino Seite aktiviert man die serielle Schnitstelle in der "setup" Funktion und setzt eine Baudrate (bits/second) fest. Dann schreibt man die betreffenden Werte/Variablen mit Kommas getrennt in einer Zeile.

Hier ein Beispiel, welches die Werte von zwei Sensoren über die serielle Schnittstelle sendet:

Arduino
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 }

Auf Processing-Seite müssen wir zunächst die Serial Library importieren, und die serielle Schnittstelle in der "setup" Funktion öffnen. Danach könne wir in der Callback Funktion "serialEvent" jeweils eine Zeile lesen, und die einzelnen Werte wieder rauslesen.

Hier ein Beispiel dass die beiden Werte ausliest und eine Ellipse zeichnet.

Processing
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); }

Processing zu Arduino

Für die Umgekehrte Kommunikation schreiben wir die Daten in Processing auch als Komma getrennte Werde in Zeilen:

Processing
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"); }

Auf dem Arduino können wir dann diese Zeilen auch wieder in einzelnen Werte konvertieren:

Arduino
#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; } } }
For you

Electrical Engineering
Results will update as you type.
  • Aesthetic elements in Eagle
  • Actuators and Outputs
  • Aktuatoren (DE)
  • Analog Input
  • Analog Sensors
  • Arduino Grundlagen
  • Arduino Programmieren
  • Arduino und Processing
  • Shift Registers
  • Carvey
  • Capacitors
  • Bread Boards
  • Digitaler Input
  • Digitaler Output
  • Eagle
  • Energy Harvesting
  • H-Bridge
  • I2C (en)
  • Interrupt Service Routine
  • Mosfet Transistor
  • LED
  • PCBs Herstellen
  • Pololu Servo Controller
  • Relays as Switches
  • Pulse Width Modulation
  • RFiD
  • Schrittmotorentreiber
  • SD Karten
  • Signal Filtering
  • Transistors as Switches
  • Widerstand
  • PCB Manufacturing
  • PlatformIO
  • Lynx Smart Motion Servo
  • Electronics Basics (EN)
  • Arduino Basics (en)
  • Arduino Programming (EN)
  • Digital Outputs (en)
  • Digital Input (en)
  • Serial Communication (en)
  • Pulse Width Modulation (en)
  • Resistors (en)
  • Arduino and P5.js
  • Arduino and ml5.js
  • Project box 2021
  • Servo Motors
  • H-Bridge (DE)
  • Networking

    Two hearts overlapped with each other
    Welcome back
    Catch up on the top discussions and highlights from your team.
    {"serverDuration": 68, "requestCorrelationId": "9c38fc0f727b41d790d8e1227d325748"}