IAN LANG ELECTRONICS

This chapter is a bit unusual in that we aren't going to couple anything up to the Arduino board other than a USB connection to the PC. Yep. Nothing on any pin, no schematics or wiring diagrams, no delving into complexities of components, just a bare board and a computer, and on the computer all we need is the IDE and the serial monitor. Huzzah!

 

The reason for this is that we are going to go into the topic of handling strings. Those of you who have done any sort of work with computers will be highly familiar with this term, and for those of you that haven't, an explanation follows.

 

A string is a block of text. It's called a string because it strings one character after another. The characters are any number, letter or punctuation mark on your computer keyboard, the proviso being that you can't do mathematical operations on a string if it's a number. What you can do is shorten, lengthen, append and find blocks in a string. Let's delve in with a starter sketch to copy, paste and upload to your Arduino:

 

 

String STRINGA ="Matthew,Mark,Luke,John";

void setup(){

  Serial.begin(9600);

}

 

void loop(){

 

Serial.print("There are ");Serial.print(STRINGA.length());Serial.print(" characters in the string:");

Serial.println();

 

for (int t=1;t<STRINGA.length()+1;t++){

 Serial.println(STRINGA.substring(0,t));delay(1000); }

 Serial.println();

 

}

 

Having copied, pasted and uploaded, open your serial monitor. Here's a sample output of what you should see:

 

There are 22 characters in the string:

M

Ma

Mat

Matt

Matth

Matthe

Matthew

Matthew,

Matthew,M

Matthew,Ma

Matthew,Mar

Matthew,Mark

Matthew,Mark,

Matthew,Mark,L

Matthew,Mark,Lu

Matthew,Mark,Luk

Matthew,Mark,Luke

Matthew,Mark,Luke,

Matthew,Mark,Luke,J

Matthew,Mark,Luke,Jo

Matthew,Mark,Luke,Joh

Matthew,Mark,Luke,John

 

There are 22 characters in the string:

M

Ma

Mat

Matt

 

 

And it'll keep doing that until you switch the power off. Try changing the message. I'm using the phrase "Ian Lang is without doubt the greatest genius ever to have graced God's Earth" but if you don't want to pander to my vanity use your own. Here's the sample:

 

There are 77 characters in the string:

I

Ia

Ian

Ian

Ian L

Ian La

Ian Lan

Ian Lang

Ian Lang

Ian Lang i

Ian Lang is

Ian Lang is

Ian Lang is w

Ian Lang is wi

Ian Lang is wit

Ian Lang is with

Ian Lang is witho

Ian Lang is withou

Ian Lang is without

Ian Lang is without

Ian Lang is without d

Ian Lang is without do

Ian Lang is without dou

Ian Lang is without doub

Ian Lang is without doubt

Ian Lang is without doubt

Ian Lang is without doubt t

Ian Lang is without doubt th

Ian Lang is without doubt the

Ian Lang is without doubt the

Ian Lang is without doubt the g

Ian Lang is without doubt the gr

Ian Lang is without doubt the gre

Ian Lang is without doubt the grea

Ian Lang is without doubt the great

Ian Lang is without doubt the greate

Ian Lang is without doubt the greates

Ian Lang is without doubt the greatest

Ian Lang is without doubt the greatest

Ian Lang is without doubt the greatest g

Ian Lang is without doubt the greatest ge

Ian Lang is without doubt the greatest gen

Ian Lang is without doubt the greatest geni

Ian Lang is without doubt the greatest geniu

Ian Lang is without doubt the greatest genius

Ian Lang is without doubt the greatest genius

Ian Lang is without doubt the greatest genius e

Ian Lang is without doubt the greatest genius ev

Ian Lang is without doubt the greatest genius eve

Ian Lang is without doubt the greatest genius ever

Ian Lang is without doubt the greatest genius ever

Ian Lang is without doubt the greatest genius ever t

Ian Lang is without doubt the greatest genius ever to

Ian Lang is without doubt the greatest genius ever to

Ian Lang is without doubt the greatest genius ever to h

Ian Lang is without doubt the greatest genius ever to ha

Ian Lang is without doubt the greatest genius ever to hav

Ian Lang is without doubt the greatest genius ever to have

Ian Lang is without doubt the greatest genius ever to have

Ian Lang is without doubt the greatest genius ever to have g

Ian Lang is without doubt the greatest genius ever to have gr

Ian Lang is without doubt the greatest genius ever to have gra

Ian Lang is without doubt the greatest genius ever to have grac

Ian Lang is without doubt the greatest genius ever to have grace

Ian Lang is without doubt the greatest genius ever to have graced

Ian Lang is without doubt the greatest genius ever to have graced

Ian Lang is without doubt the greatest genius ever to have graced G

Ian Lang is without doubt the greatest genius ever to have graced Go

Ian Lang is without doubt the greatest genius ever to have graced God

Ian Lang is without doubt the greatest genius ever to have graced God'

Ian Lang is without doubt the greatest genius ever to have graced God's

Ian Lang is without doubt the greatest genius ever to have graced God's

Ian Lang is without doubt the greatest genius ever to have graced God's E

Ian Lang is without doubt the greatest genius ever to have graced God's Ea

Ian Lang is without doubt the greatest genius ever to have graced God's Ear

Ian Lang is without doubt the greatest genius ever to have graced God's Eart

Ian Lang is without doubt the greatest genius ever to have graced God's Earth

 

There are 77 characters in the string:

I

Ia

Ian

 

 

Well, it pays to advertise.

 

Anyway, you get the idea. The string prints out one character extra per time, on a new line. Spaces between words are also a character, and so whenever there's a space you get the same line printed out twice:

 

Ian Lang is without doubt the greatest genius ever

Ian Lang is without doubt the greatest genius ever

 

The first is without the space, the second with.

 

There are two important commands here that we need to study. The first is:

 

STRINGA.length()

 

This measures how many characters are in the string from beginning to end. The second is:

 

STRINGA.substring(0,t)

 

which prints out a portion of the string from wherever you set the leftmost and rightmost parameters. In the string Matthew,Mark,Luke,John if we had said STRINGA.substring(4,7) we'd get:

 

 hew

 

Because it starts at the character after the leftmost you supply and stops at the character after the rightmost:

 

Handling Strings on your Arduino

Go Back

Matt        ,Mark,Luke,John

hew

Over the page we have a canter through the code to see how it all fits together.

More>