String addition and parseInt

Many a times we want to communicate from our systems to arduino or vice-versa to control the actuators or to receive/visualize the data and send it to programs that utilize the processing capabilities of our pcs. for e.g we may want to control a robot by the movements of our mouse or the keyboard. In those  cases  serial communication comes in handy.

First we will see how do string addition operators and parseInt/parseFloat functions work and in the next post we will see how to make Processing and Arduino talk to each other.

Working with string addition operator in processing or arduino–

We just join some decisions like whether to move forward or backward with their corresponding analog values that come from the position of mouse in the processing code and analog readings in the arduino code.(Choosing forward or backward will again depend on some conditions and also that analog readings from arduino can be only 10 bits long and analogWrite only takes 8 bit values….but we will not worry about this as our purpose here is to just get comfortable with the functioning)

For Processing —

import processing.serial.*;
Serial myport;
String s1 = "fwd";
String s2 = "bck";
void setup()
{
  size(400, 400);
  String portname = "COM3";
  //myport = new Serial(this,portname,9600);
}
void draw()
{
  String t1 = s1+';'+mouseY;
  String t2 = s2+';'+mouseX;
  //myport.write(t1+t2);
  println(t1+';'+t2);
}

For Arduino —

String s1 = "fwd";
String s2 = "bck";
void setup() {
  Serial.begin(9600);
  // put your setup code here, to run once:

}

void loop() {
  Serial.println(s1 + ';' + analogRead(A0) + ';' + s2 + ';' + analogRead(A1));
  // put your main code here, to run repeatedly:

}

In this example we send a combination of letters and numbers mixed together and check the function called parseInt. For the visual indicator we use a led that glows only when a particular number is received.

NOTE: YOU CANNOT CHECK THE NUMBER OR THE RECEIVED DATA ON ARDUINO’S SERIAL MONITOR BECAUSE IT WONT OPEN AS THE SERIAL COMMUNICATION PORT IS ALREADY IN USE BY PROCESSING.

Serial.parseInt() returns the first valid (long) integer number from the serial buffer. Characters that are not integers (or the minus sign) are skipped. Serial.parseInt() is terminated by the first character that is not a digit.

Processing  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 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');
  int j = Serial.parseInt();
  if (j == 233)
  {
    digitalWrite(13, HIGH);
  }
  else if (j == 210)
  {
    digitalWrite(12, HIGH);
  }
  else
  {
    digitalWrite(13, LOW);
    digitalWrite(12, LOW);
  }

  // put your main code here, to run repeatedly:

}

When we press the run button in processing then the led connected to pin 13 lights up and not the other led as it should be due to the description of the function and as soon as we press stop it stops glowing.


Advertisements

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