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...
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". 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".
To set up an array, there are various choices, but the simplest is....
That would create an array of type "int" with elements as follows...
The "gotcha" is this: You said "Bags" and it created three elements, but did NOT create an element "Bags".
This is a particularly deadly gotcha, because the Arduino environment does not offer range checking.
If you have declared the array with int Bags;, and then do something like....
... 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....
.... 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. There is no Bags if you used int Bags; 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.
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!
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."
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.
Click here to visit editor's Sheepdog Software (tm) freeware, shareware pages.
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.
Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org
....... P a g e . . . E n d s .....