Making Processing and Arduino talk – Part 1

You can find a good tutorial on serial communication between arduino and processing HERE.

We will use those ideas and do some practical stuff. In this example we send a mixed string to arduino which then converts it to character array and checks if certain elements match certain numbers, if yes then it does something and if not then it does something else.

Processing side code —

import processing.serial.*;
Serial myport;

String sh = "right;233;left;210\n";//if you dont write \n then led does not glow.

void setup()
{
  String portname = "COM3";
  myport = new Serial(this, portname, 9600);
}
void draw()
{
  myport.write(sh);
  delay(1000);
}

Arduino side code—

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  // put your setup code here, to run once:

}

void loop() {
  String receiv = Serial.readStringUntil('\n');// dont write receive as it will get highlighted !!
  char msg[20] = "";
  receiv.toCharArray(msg, sizeof(msg));
  if (msg[6] == '2' && msg[7] == '3' && msg[8] == '3')
  {
    digitalWrite(13, HIGH);
  }
  else
  {
    digitalWrite(13, LOW);
  }
}


Suppose we want to send an integer then we can send them in string format. Although you can give integer inputs to myport.write() function but the problems comes due to the fact that Arduino cannot directly read integers. The supported functions only read bytes and strings, hence we are sending integers as strings. String has additional advantages because of the “\n” character that it allows to be attached in the end which in turn tells arduino to look for data till it encounters “\n”.

In this we exclusively send a string of numbers and on the arduino side we convert it to integer and if the proper message is received then we see its visual indication by glowing an led.

Processing side code—

import processing.serial.*;
Serial myport;

String num = "12345\n"; //note that if you exceed the limit of an integer then you wont be able to use commands like atoi or toint.
void setup()
{
  String portname = "COM3";
  myport = new Serial(this, portname, 9600);
}
void draw()
{
  //println(shantam);
  myport.write(num);
  delay(1000);
}

Arduino side code—-

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  // put your setup code here, to run once:

}

void loop() {
  String receiv = Serial.readStringUntil('\n');
  //any of the following methods i.e string to array and then using atoi or directly using toInt to convert string to int works perfectly.
  /*char msg[20] = "";
  receiv.toCharArray(msg,sizeof(msg));
  int num = atoi(msg);*/
  int num = receiv.toInt();
  if (num == 12345)
  {
    digitalWrite(13, HIGH);
  }
  else
  {
    digitalWrite(13, LOW);
  }
}

Suppose you want to send a large series of numbers then you cannot convert that to integer on the arduino side , so we store that in an array and then compare each element one by one to check and achieve whatever we want to. Here is an example —

Processing side code —

import processing.serial.*;
Serial myport;

String num = "12345678891234\n";
void setup()
{
  String portname = "COM3";
  myport = new Serial(this, portname, 9600);
}
void draw()
{
  //println(shantam);
  myport.write(num);
  delay(1000);
}

Arduino side code–

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  // put your setup code here, to run once:

}

void loop() {
  String receiv = Serial.readStringUntil('\n');

  char msg[20] = "";
  receiv.toCharArray(msg, sizeof(msg));

  if (msg[0] == '1' && msg[1] == '2' && msg[2] == '3' && msg[3] == '4'
      && msg[4] == '5' && msg[5] == '6' && msg[6] == '7' && msg[7] == '8'
      && msg[8] == '8' && msg[9] == '9' && msg[10] == '1' && msg[11] == '2'
      && msg[12] == '3' && msg[13] == '4')
  {
    digitalWrite(13, HIGH);
  }
  else
  {
    digitalWrite(13, LOW);
  }
}


Now we will send strings to processing from arduino and observe the behavior of inputs –

Arduino side code–

String shantam = "This is a beautiful world to live in!!.";
String sh = "right;233;left;210";
String shan = "fwd\n234\nright\n200";
String num = "12345";
void setup() {
  Serial.begin(9600);
  // put your setup code here, to run once:

}

void loop() {
  Serial.println(num);
  Serial.println(shantam);
  Serial.println(shan);
  Serial.println(sh);
  // put your main code here, to run repeatedly:

}

Processing side code–

import processing.serial.*;
Serial myport;
void setup()
{
  String portname = "COM3";
  myport = new Serial(this, portname, 9600);
  myport.bufferUntil('\n');
}
/*void draw()//directly writing it in draw() function can give you a lot of nulls .
 {
 //String msg = myport.readString();
 String msg = myport.readStringUntil('\n');
 print(msg);
 }*/
void draw()
{
  ;
}
void serialEvent(Serial myport)//calling serial event gives no nulls.
{
  String msg = myport.readStringUntil('\n');
  print(msg);
}

If write directly inside the draw function then initially you will receive a lot of nulls…20140927T112028

When we use serial event the serial monitor of processing does not print nulls but initially here also we have failed communication because initially we do see empty space in the area where it should have started printing and where it actually prints..

20140927T112229

In the first method we might receive lots of nulls but in the second we do not ever !! Why ?

Most of the time we don’t want our Processing sketch to sit and wait for data to appear from the serial port. We usually want to be doing other things and just be interrupted when there’s data available. However, we add in a line calling bufferUntil(). By calling this function we are asking not to be interrupted with data on the serial port until we receive a character of our choosing. We are asking to be interrupted only when we’ve received a full line of data as we don’t want to think we’ve received “4” when we were supposed to received “45” and just didn’t wait for the second digit. The serialEvent() is triggered only after a certain number of data elements are read by the bufferUntil() function.

Advertisements

About Shantam Raj

Currently I am a final year B.Tech undergraduate majoring in “Electronics and Communication Engineering” from IIT Guwahati. I am passionate about electronics and robotics. Apart from that I love writing, visit https://medium.com/@shantam_raj for some of my works. I am a die hard soccer fan. I also love to play badminton. I am a hardware hacking enthusiast and a tinkerer !!.
This entry was posted in Arduino, Processing, Serial Communication and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s