Lesson 5: Data Management – Arrays, Strings, and Efficient Coding

Data Management: Arrays, Strings, and Efficient Coding

Real programs juggle many values at once: several LEDs, a list of readings, lines of text. This lesson gives you the tools to keep that data organized. You will learn arrays for storing collections under one name, strings for handling text, and the memory habits that matter on a small board. You will drive a sequence of LEDs from an array and build cleaner Serial messages that combine text with live sensor values.

What data management means

Data management is simply organizing, storing, and manipulating the information your program works with. As soon as a program handles more than a couple of values, keeping them tidy makes everything easier to write and debug. Data structures are the containers that keep related values together, and the most fundamental one is the array.

Arrays: a collection under one name

An array is a set of variables of the same type stored under a single name. You declare it by naming the type, the array name, and the size in square brackets, then listing the values. For example, int numbers[5] = {1, 2, 3, 4, 5} creates an array of five integers. You must state the size because the board allocates that much memory up front. The array is like a grocery list: one name, many items of the same type.

Why arrays start at zero

This trips up every beginner, so hold onto it: array indexes start at zero, not one. In an array of length five, the valid indexes are 0, 1, 2, 3, and 4. So numbers[0] is the first value, and numbers[4] is the fifth and last. There is no numbers[5]; asking for it reads memory that does not belong to the array, which is an out-of-bounds error. The length is five, but the highest index is four. This is exactly why a for loop over an array uses the condition i < 5: it visits 0 through 4 and stops before the invalid index.

Strings are arrays of characters

A string is a sequence of characters used to store text, and under the hood it is just an array of individual character values. The word hello is really the characters h, e, l, l, o stored in order. Because text is so common, the language gives strings a friendlier syntax: you declare one with quotation marks, such as String message = "hello world", and you can print the whole thing with a single Serial.println, without writing a loop over each character. The array is still there; the language just hides it for you.

Working with text and mixing in numbers

Common string operations include concatenation, which joins strings together with the plus symbol, checking length, and taking a substring, a smaller slice of a longer string. A very useful pattern is turning a raw sensor number into a labeled message. Instead of printing a bare 742, you build "Sensor value: " + String(sensorValue), which converts the number to text and joins it to a label. Converting a number to a string is reliable because it just turns digits into characters, though converting between other types, such as a decimal into an int, can silently drop information.

Memory awareness on a small board

A microcontroller has very little memory compared to a phone or laptop. Each character in a string takes space, so a large or growing pile of strings can eat up memory quickly and cause strange behavior. On modern devices this rarely matters, but on a low-power board it does. Use strings freely where you need them, but do not store far more text than the task requires.

Working through it

Wire three LEDs. Place LEDs on pins 8, 9, and 10, each through its own 220 ohm resistor to ground. These three pins will become the values in your array.

Store the pins in an array. Declare const int ledPins[3] = {8, 9, 10}. This holds all three pin numbers under one name instead of three separate variables.

Configure them with a loop. In setup, use a for loop from i equal 0 while i < 3 to call pinMode(ledPins[i], OUTPUT). Each pass sets one pin as an output, so three lines become one loop.

Blink them in sequence. In loop, use another for loop over the array to turn each LED on, wait, and off in turn. The array plus the loop expresses the whole sequence in a few lines instead of a long, repetitive block.

Build a labeled sensor message. Wire a potentiometer to A0. In loop, read it with analogRead, then build String message = "Sensor value: " + String(sensorValue) and print it. Turn the potentiometer and watch a clear, human-readable message update instead of a bare number.

Sequence three LEDs from an array