Making Processing and Arduino talk – Part 2

This is just an extension of the previous post where we were sending different types of datatypes and observing their behavior. Here we will consider byte arrays. Byte arrays store the characters in ascii format. So we will see how to print the true characters from a byte array containing the ascii characters because the data we will get from processing will be in bytes and visualizing them in ascii code would make no sense.

Arduino code —

void setup() {
  Serial.begin(9600);
}

void loop() {
  byte bt[3] = "";
  bt[0] = 48;
  bt[1] = 43;
  bt[2] = 50;
  Serial.print(bt[0]); Serial.print(" "); Serial.print(bt[1]); Serial.print(" "); Serial.print(bt[2]);
  int i = int(bt[2]);
  char ch = char(bt[0]);
  char c = char(bt[2]);
  Serial.print(" ");
  Serial.print(ch); Serial.print(" "); Serial.print(i); Serial.print(" "); Serial.println(c);
}

20141010T220034

Note that converting byte to  integer does not give the true integer. Converting it to a character gives the true integer or character.

Now we send a byte packet from Processing containing  some information that will be used by Arduino for doing something. The first task will be very simple , just to check if we are on the right track or not. After that in the next post we will transmit the processed information from one node to another.

Processing sends some numbers as bytes that is received by the Arduino which checks only the first byte and if it matches the data that we are sending then  it glows an led on pin 13. As soon as you stop the processing sketch the led stops glowing.

Processing code —


import processing.serial.*;
String s1 = "+";
String s2 = "-";
String s3 = "1023";
String s4 = "1003";
Serial myport;
void setup()
{
import processing.serial.*;
String s1 = "+";
String s2 = "-";
String s3 = "1023";
String s4 = "1003";
Serial myport;
void setup()
{
size(400, 400);
String  portname = "COM14";
myport = new Serial(this, portname, 9600);
}
void draw()
{
String send = s1+s3+s2+s4+"\n";

byte[] bt = new byte[15];
for (int i = 0; i < send.length (); i++)
{

bt[i] = byte(send.charAt(i));

myport.write(bt[i]);

println(bt[i]);
}
}

Arduino code —

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

}

void loop() {
  byte msg[15] = "";
  Serial.readBytesUntil('\n', msg, 15);
  if (char(msg[1]) == '1')
  {
    digitalWrite(13, HIGH);
  }
  else
  {
    digitalWrite(13, LOW);
  }
  // put your main code here, to run repeatedly:

}

Since every thing works fine in the next post we will send strings from processing to arduino and do something from the received data.

 

 

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