Need Help Understanding This Code!!

Hi Guys,

I've been using processing and arduino for about a month now. I found this code online and it was a project for a radar using a ping sensor. Basically you have a ping sensor that moves side to side and detects objects' distances. Using that information, it plots points (green circles) on the screen when you run the code for processing. I was hoping you guys could help me out and help me understand a few things written for this code. I know string acts sort of like an array? Why is String used in this case and why are the parameters left blank? Also under void serialEvent (Serial Port) I have trouble understanding what the code does from data = port.readStringUntil('.'); to Theta = data.substring (index+1 , data.length());
If you guys could answer these two questions, I would appreciate it greatly. I know it's a bit much but I am a beginner and want to understand the language better.

I've posted the codes for both arduino and processing below:

#define ECHOPIN 7        // Pin to receive echo pulse 
#define TRIGPIN 8
#include <Servo.h>

Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
void setup() {
 Serial.begin(9600); 

  myservo.attach(9); // attaches the servo on pin 9 to the servo object
  pinMode(3,OUTPUT);
  pinMode(12,OUTPUT);
  pinMode(13,INPUT);
  pinMode(ECHOPIN, INPUT); 
  pinMode(TRIGPIN, OUTPUT); 
}

void Print (int R , int T)
{
   Serial.print(R);Serial.print(", ");
   Serial.print(T);Serial.println(".");
   delay(100);
}

float Distance () {
  
  digitalWrite(TRIGPIN, LOW); 
  delayMicroseconds(2); 
  digitalWrite(TRIGPIN, HIGH); 
  delayMicroseconds(10); 
  digitalWrite(TRIGPIN, LOW);
  // Distance Calculation
  
  float distance = pulseIn(ECHOPIN, HIGH); 
  distance= distance/58; 
  return(distance);
}

void loop() {
  
      myservo.write(45); // tell servo to go to position in variable 'pos'
  
 
  delay(2000);
  
  
  for(pos = 45; pos <= 135; pos += 3) // goes from 45 degrees to 135 degrees
  { // in steps of 1 degree
    myservo.write(pos); // tell servo to go to position in variable 'pos'
    
  Print(Distance() , pos);    
    delay(10); // waits 15ms for the servo to reach the position
  }
  delay(1000);
    
    
  for(pos = 135; pos>=45; pos-=3) // goes from 135 degrees to 45 degrees
  {
    myservo.write(pos); // tell servo to go to position in variable 'pos'
      Print(Distance() , pos);    

    delay(10); // waits 15ms for the servo to reach the position
  }  
  

  
}
import processing.serial.*;
Serial port;
Serial port2;
String data = "";
String Radius = "";
String Theta = "";
int index = 0;
float distance = 0;
float angle = 0;
float pi = 22.0/7;

void setup() 
{
 
 size(1000,1000); //set screen size
 background(255,255,255); //white background
  ellipse(500,500,1000,1000); //draw an ellipse 
 line(500,0,500,1000); //drawing a line
 line(0,500,1000,500);
 line(500,500,1000,0);
 line(500,500,0,0);

port = new Serial(this, "COM3", 9600); //set com and 9600 to speak with ard. 	  
port.bufferUntil('.');
}

void draw()

{


}

void serialEvent(Serial port)
{

  data = port.readStringUntil('.');
  data = data.substring(0, data.length() - 1);  
  index = data.indexOf(",");
  Radius = data.substring(0, index);
  Theta = data.substring (index+1 , data.length());
  
   translate(500,500); //specifices amount of displace objects within display 			window
			//x parameter = left/right y = up/down
			//displace of dots 
   point (0,0);
   
      distance = float(Radius); 
      angle = float(Theta) /180 * pi; 
      fill(30,200,30);
      ellipse(distance * cos(angle) ,  -1 * distance * sin(angle) , 5,5); 

}

Panik:
I know string acts sort of like an array? Why is String used in this case and why are the parameters left blank? Also under void serialEvent (Serial Port) I have trouble understanding what the code does from data = port.readStringUntil('.'); to Theta = data.substring (index+1 , data.length());

String is a class that encapsulates a string or array of characters and provides lots of
useful methods.

readStringUntil reads a stream character-by-character building up a result string,
and finishing only when the character passed as its argument is seen.

substring takes two indices and return the sub-string running for the first index to
just before the second index. Since the 2nd index is the String's length it is returning
just the right-hand part of the String starting at the first argument, namely index+1

It is easier to view strings as containers rather than arrays. They are two diffierent things completely.

The block of code which has caught your eye is loading information into the strings, much like loading a number into a variable
int var1=3;
string container="A sentence";

breaking down what is there, bearing in mind i didn't notice any code initialising them as strings but nevertheless:

** data = port.readStringUntil('.');**
//load the string into the string until a . character is found

** data = data.substring(0, data.length() - 1); **
substrings allow you to omit characters from the beginning and end of the string. An example of this would be if you were to parse a system datetime you may receive a string such as
10:00:00 02/04/2013
if you wanted to remove the date for example you could divide the string into two halves therefore removing the date

** index = data.indexOf(",");**
search the string for , and store it's position as an integer value.

** Radius = data.substring(0, index);**
Divide the string into a substring, using its ending position as the integer gained from the previous line.

** Theta = data.substring (index+1 , data.length());**
same again, but this is the second half of the string ignoring the first half. the position where it found, + 1, therefore skiping it and reading after it.

Wow! Thanks for the quick and descriptive replies. I really appreciate it. The code is now less intimidating XD

Hi, BradGee, excellent tutorial, with down to earth examples, good one mate.

Tom.... :slight_smile:
PS Have given you a karma.