Difference between revisions of "Sending data from Arduino to Processing"
(8 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | |||
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. | 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=== | ===Arduino send from serial port=== | ||
Line 7: | Line 8: | ||
const int echoPin = 2; | const int echoPin = 2; | ||
long duration; | long duration; | ||
− | int distance | + | int distance; |
− | void setup() { | + | void setup(){ |
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output | pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output | ||
pinMode(echoPin, INPUT); // Sets the echoPin as an Input | pinMode(echoPin, INPUT); // Sets the echoPin as an Input | ||
Line 15: | Line 16: | ||
} | } | ||
− | void loop() { | + | void loop(){ |
// Clears the trigPin | // Clears the trigPin | ||
digitalWrite(trigPin, LOW); | digitalWrite(trigPin, LOW); | ||
Line 45: | Line 46: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Open the Arduino Serial Monitor to test if the data gets printed. Close it again before testing the Processing code. | 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. | + | This was all on the Arduino side. |
===Processing receive from serial port=== | ===Processing receive from serial port=== | ||
Line 69: | Line 70: | ||
if ( myPort.available() > 0) | if ( myPort.available() > 0) | ||
{ // If data is available, | { // If data is available, | ||
− | val = myPort.readStringUntil('\n'); | + | val = myPort.readStringUntil('\n');// read it and store it in val |
} | } | ||
println(val); //print it out in the console | println(val); //print it out in the console | ||
Line 75: | Line 76: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | Now you should be able to see if the data gets printed in the console. If | + | 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. |
+ | <br> | ||
+ | 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. | ||
+ | |||
+ | <syntaxhighlight lang="java" line="line"> | ||
+ | import processing.serial.*; | ||
+ | Serial myPort; | ||
+ | String val; | ||
+ | int sensorVal = 0; | ||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | // I know that the first port in the serial list on my mac | ||
+ | // is Serial.list()[0]. | ||
+ | // On Windows machines, this generally opens COM1. | ||
+ | // Open whatever port is the one you're using. | ||
+ | String portName = Serial.list()[1]; //change the 0 to a 1 or 2 etc. to match your port | ||
+ | myPort = new Serial(this, portName, 9600); | ||
+ | } | ||
+ | |||
+ | void draw() | ||
+ | { | ||
+ | if ( myPort.available() > 0) { // If data is available, | ||
+ | val = myPort.readStringUntil('\n'); | ||
+ | try { | ||
+ | sensorVal = Integer.valueOf(val.trim()); | ||
+ | } | ||
+ | catch(Exception e) { | ||
+ | ; | ||
+ | } | ||
+ | println(sensorVal); // read it and store it in vals!*/ | ||
+ | } | ||
+ | if (sensorVal > 50){ | ||
+ | println("ciao"); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </syntaxhighlight> | ||
+ | |||
It is also possible to revert this communication and send data from Processing to Arduino. You can do it by following [[Writing-fromProcessing-toArduino | this guidelines]]. | It is also possible to revert this communication and send data from Processing to Arduino. You can do it by following [[Writing-fromProcessing-toArduino | this guidelines]]. | ||
[[Category:Arduino]][[Category:Processing]] | [[Category:Arduino]][[Category:Processing]] |
Latest revision as of 09:38, 10 January 2023
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}
It is also possible to revert this communication and send data from Processing to Arduino. You can do it by following this guidelines.