Sending data from Arduino to Processing

From Interaction Station Wiki
Revision as of 09:46, 28 November 2022 by Annasa (talk | contribs)
Jump to navigation Jump to search

Sometimes you will want to send your Arduino's sensors (or others) data to Processing to generate a visual output. This is possible by using Arduino and Processing Serial communication. With this function, you can easily trigger visuals accordingly to the data coming from Arduino.

Arduino send from serial port

The first step you will have to take is to initiate Serial communication in your Arduino code. In this example we are sending distance data coming from an Ultrasonic sensor to Processing via serial communication.

 1const int trigPin = 3;
 2const int echoPin = 2;
 3long duration;
 4int distance;
 5
 6void setup(){
 7  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
 8  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
 9  Serial.begin(9600); // Starts the serial communication
10}
11
12void loop(){
13  // Clears the trigPin
14  digitalWrite(trigPin, LOW);
15  delayMicroseconds(2);
16  // Sets the trigPin on HIGH state for 10 micro seconds
17  digitalWrite(trigPin, HIGH);
18  delayMicroseconds(10);
19  digitalWrite(trigPin, LOW);
20  // Reads the echoPin, returns the sound wave travel time in microseconds
21  duration = pulseIn(echoPin, HIGH);
22  // Calculating the distance
23  distance = duration * 0.034 / 2;
24  // Prints the distance on the Serial Monitor
25  Serial.print("Distance: ");
26  Serial.println(distance);
27}

If you are using data coming from other sensors or actuators you should just make sure this gets send to Processing via the Serial.print() function.

1void setup(){
2Serial.begin(9600);
3}
4void loop(){
5Serial.print(your data!);
6}

Open the Arduino Serial Monitor to test if the data gets printed. Close it again before testing the Processing code. This was all on the Arduino side.

Processing receive from serial port

Now we are going to receive the previously sent data on Processing.

 1import processing.serial.*;
 2
 3Serial myPort;
 4String val;
 5
 6void setup()
 7{
 8  // I know that the first port in the serial list on my mac
 9  // is Serial.list()[0].
10  // On Windows machines, this generally opens COM1.
11  // Open whatever port is the one you're using.
12  String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
13  myPort = new Serial(this, portName, 9600);
14}
15
16void draw()
17{
18  if ( myPort.available() > 0) 
19  {  // If data is available,
20  val = myPort.readStringUntil('\n');// read it and store it in val
21  } 
22  println(val); //print it out in the console
23}

Now you should be able to see if the data gets printed in the console. If this does not happen try to modify the port number on line 12. It might be that you are simply not on the correct port.
When Processing read sensor data coming from Arduino through Serial it reads it as a string. To be able to use these values to generate and trigger your visuals on Processing though, you will need to convert the string into a numerical variable.

 1 import processing.serial.*;
 2 Serial myPort;
 3 String val;
 4 int sensorVal = 0;
 5 
 6 void setup()
 7 {
 8  // I know that the first port in the serial list on my mac
 9  // is Serial.list()[0].
10  // On Windows machines, this generally opens COM1.
11  // Open whatever port is the one you're using.
12  String portName = Serial.list()[1]; //change the 0 to a 1 or 2 etc. to match your port
13  myPort = new Serial(this, portName, 9600);
14  }
15
16void draw()
17{
18 if ( myPort.available() > 0) {  // If data is available,
19  val = myPort.readStringUntil('\n'); 
20    try {
21     sensorVal = Integer.valueOf(val.trim());
22    }
23    catch(Exception e) {
24    ;
25    }
26  println(sensorVal); // read it and store it in vals!*/
27  } 
28  if (sensorVal > 50){
29    println("ciao");
30  }
31  
32}



It is also possible to revert this communication and send data from Processing to Arduino. You can do it by following this guidelines.