Introduction
In this tutorial, we will be showing you how to send data from Arduino to a computer. The data will be received by a Processing program.
Arduino Code
Code explanation
Initialise variables
int a = -500;
int b = 1000;
We initialize two variables which we are going to send to the computer.
Setup data rate
Serial.begin(9600);
This configures the serial port with a certain number of bits per second (baud). This has to be the same in the Arduino and later in the Processing programs.
Send
Serial.print(a);
Serial.print(" ");
Serial.print(b);
Serial.println();
Prints the two variables to the serial port with a space in between.
Serial.println();
sends a new line character, which can be used to determine where one message ends.
Change variables
a++;
b--;
We could use input from sensors here instead.
Run the program
Upload the code to Arduino. Then open the serial monitor. Shortcut: Ctrl + Shift + M
After a few seconds you will see the changing values of a
and b
.
Processing Code
In this section, you will learn how to write the code for controlling the servo motor. You will be able to set an angle for the rotation of the servo motor.
As we did in the previous tutorial, we have put the code below so you can read it once
and then will run you through it.
Code explanation
Import serial library
import processing.serial.*;
This library provides function to send and receive data from the serial port.
Setup serial
Serial serialPort;
Declares the serial variable.
String portName = Serial.list()[1];
Gets the serial port name for arduino.
Serial.list()
is an array containing a list of available serial ports. You can print this array with the printArray(array)
function to find out which number to choose.
Remeber that the first array index is 0
serialPort = new Serial(this, portName, 9600);
serialPort.bufferUntil('\n');
Initialises the serial object with 9600
as baud rate and the previously selected port name.
In the Arduino program we send one line at a time. Each line ends with a new line character '\n'
The serial object will read the serial port until it reaches the new line character.
Receive data
The draw()
functions runs continously.
while (serialPort.available() > 0)
The program will receive strings as long as the serial port has some data available.
String receivedString = serialPort.readStringUntil('\n');
Stores the line in a string.
Sometimes the serial can not be read properly. To avoild crashing the program we use if (receivedString != null)
to check if we received any data.
Use data
We then pass the received string to the processSerialInput
function as a parameter.
float[] nums = float(split(receivedString, ' '));
splits the string into a array of floats using 'space' as the character between two numbers.
float a = nums[0]; float b = nums[1];
stores the first and second elements of the array in seperate variables.
println("a=" + a + " b=" + b);
prints the variables with some text on a new line.
You can use the variables in some other way. For example by drawing a diagram.
Run the program
Upload the code by clicking the play button. You will see the values in the box on the bottom of the window.
Exercises
Exercise 1
Add another variable in the Arduino code. And send it to the Processing sketch. For example, use the ultrasonic distance.
Exercise 2
Sometimes the processing sketch might not receive the whole string. How can you make sure that no error occur during the runtime?
Hint: To check if the element at index 1
exists use: if (nums.length > 1)
Exercise 3
Draw a shape in Processing according to the size of one or more variables.
Find out about Processing functions. For example
ellipse()
and
rect()
Exercise 4
Draw a line graph of one or multiple received variables in Processing.
Find out how to store and access multiple values in a datastucture like ArrayList
Do you need to store all values? Can you just store the last 50
values to save memory?
Challenge
For those of you that want more of a challenge, find out how to send data the other way (Processing to Arduino). Links: