HOME - - - - - - - - Table of contents, my Arduino "How To" articles
Other material for programmers Delicious Bookmark this on Delicious   Recommend to StumbleUpon

Using Arduino Arrays... a good idea.

One "gotcha"

An array is a place where you can store data. There is one little thing which needs care.

Suppose you had an assembly line packing cashew nuts in 100g bags.

Further suppose that at a quality control station, some mechanism diverted over-full bags to one "try again" hopper, under-weight bags to a different "try again" hopper, and allowed not-too-full-, not-full-enough bags to pass on.

It would be easy to install three sensors, one for each path, to detect bags going by. And to connect those sensors to an Arduino to keep count of the bags passing the three sensors.

You might well choose to use an array called "Bags" to hold the count of sensor events for each channel.

You could use three ordinary variables... "BagsRight", "BagsUnderFilled", "BagsOverFilled". This would be "easier"... but would deny you the benefits which you could accrue if you were to learn the limited "extra" skill of using arrays... a skill which will repay you many times over. The need for an array in our example is limited. In other instances, an array is pretty well essential for sensible completion of the task.

Forgive that diversion? Back to doing it with an array...

Choices....

To do the job with an array, we will have an array with three elements.

Their names might be....

However, there are certain advantages to "counting from zero", from calling the first array element "Bag[0]". Once you get use to it, it isn't as strange as it may seem at first. So, my recommendation for most circumstances... use the following in a situation like the one we are using as our example....

The advantage of calling the first element of your array "0" (zero) is so strong, that the Nice People who made our programming environment have assumed that you will always want an element called "0".

Here's the thing to be careful about....

To set up an array, there are various choices, but the simplest is....

int Bags[2];

That would create an array of type "int" with elements as follows...

The "gotcha" is this: You said "Bags[2]" and it created three elements, but did NOT create an element "Bags[3]".

A particularly deadly "gotcha"....

This is a particularly deadly gotcha, because the Arduino environment does not offer range checking.

If you have declared the array with int Bags[2];, and then do something like....

WhatsIt=Bags[3];

... the system won't complain. And you will get something in WhatsIt. But what you will get is anyone's guess. And so what will happen when you use what's in WhatsIt is anyone's guess. Not good.

But there's worse to come!

if you do....

Bags[3]=WhatsIt;

.... you will change what is in a part of the Arduino's memory. But not a part that is set aside for something called Bags[3]. There is no Bags[3] if you used int Bags[3]; to create the array "Bags". So what have you changed? I don't know. You won't know. The system won't complain. But is it a racing certainty that one day it will "misbehave", quite possibly in a weird and hard to fathom manner.

Apologies... this error appears in some of my programs

I'm told from time to time that this problem exists in some of the things I have published on the web. Apologies! I will try to get the error expunged. But please be vigilant. Watch out for it. Emails to alert me to the error's continued existence welcome!

The Official Word....

Here's a link to the page about arrays in the official Arduino reference

Quotes from there for you....

"...you should be careful in accessing arrays. Accessing past the end of an array (using an index number greater than your declared array size - 1) is reading from memory that is in use for other purposes. Reading from these locations is probably not going to do much except yield invalid data. Writing to random memory locations is definitely a bad idea and can often lead to unhappy results such as crashes or program malfunction. This can also be a difficult bug to track down."

(The Arduino environment)... "does no checking to see if array access is within legal bounds of the array size that you have declared."





   Search this site or the web        powered by FreeFind
 
  Site search Web search
Site Map    What's New    Search

The search engine is not intelligent. It merely seeks the words you specify. It will not do anything sensible with "What does the 'could not compile' error mean?" It will just return references to pages with "what", "does", "could", "not".... etc.
In addition to the tutorials for which this page serves as Table of Contents, I have other sites with material you might find useful.....

Sequenced set of tutorials on Arduino programming and electronics interfacing.
Tutorials about the free database supplied with Open Office version 2. (If you experienced Adabas with Star Office, ooBase is nothing like it!)
Some pages for programmers.
Using the parallel port of a Windows computer.


If you visit 1&1's site from here, it helps me. They host my website, and I wouldn't put this link up for them if I wasn't happy with their service.




Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!

Click here to visit editor's Sheepdog Software (tm) freeware, shareware pages.


And if you liked that, or want different things, here are some more pages from the editor of these tutorials....

Click here to visit the homepage of my biggest site.

Click here to visit the homepage of Sheepdogsoftware.co.uk. Apologies if the "?FrmAht" I added to that link causes your browser problems. Please let me know, if so?

Click here to visit editor's pages about using computers in Sensing and Control, e.g. weather logging.



To email this page's editor, Tom Boyd.... Editor's email address. Suggestions welcomed!


Valid HTML 4.01 Transitional Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org


Why does this page cause a script to run? Because of the Google panels, and the code for the search button. Why do I mention the script? Be sure you know all you need to about spyware.

....... P a g e . . . E n d s .....