This document discusses arrays in Arduino programming. It defines arrays as collections of variables of the same data type that allow storing multiple values under a single name. Accessing array elements requires specifying an index number. The document provides examples of declaring integer arrays, initializing arrays with starting values, and accessing array values using indexes to control LED pins on a circuit. It includes code for an array program that turns on LEDs with a 1 second delay to demonstrate using arrays. Finally, it presents an assignment to write a program using arrays to turn an LED on for 1 second and off for 2 seconds based on a provided circuit diagram.
3. Arrays
An array is a collection of variables with the same data types. It is a very
useful enhancement to variables that allows a single variable name to
store multiple values. To access the stored information inside an array,
the location of the data needs to be specified when using an array. This
location is also known as the Index of the array and the stored
information inside the array is called Elements. The index of an array
always starts the counting from Zero.
4. Array
• int – is a variable data type that can store whole numbers.
• ledPin - is the variable name. This follows the rules when naming
variables.
• [ ] Square Brackets - After the variable name, a set of square brackets
must be put with a number inside of it. This number represents the size
of the array, also known as the number of "Elements” inside an
array.
; semicolon - A semicolon is placed after the square brackets to indicate
the end of the statement.
5. Array
With this example, we have 5 elements that can store integer information. If
we want to access the first element, we need to call index zero. Just like this:
ledPin [0]
Since the index starts at zero and we only have 5 elements inside the array,
we can say:
6. Array
If we want to store information in every element, we can
declare in the parameter:
ledPin [0]
ledPin [1]
ledPin [2]
ledPin [3]
ledPin [4]
Similar to a regular variable, an array must be declared
before it can be used.
7. How do we initialize an array?
When an array is used in a function, typically it is not assigned yet will
values. With this, we can call the index of the array and store
information, just like the example given above. In other cases, arrays
can be explicitly given values. These values need to be inside a set of
curly braces after an equal symbol.
For example:
int ledPin [5] = {2, 3, 4, 5, 8};
This line of code is similar to:
8. How do we initialize an array?
Where:
• = Equal symbol - is used to assign values to an array.
• { } Curly Braces - The elements of an array need to be stored inside a
set of curly braces to define the start and end of a code block.
• , Comma - The elements inside an array needs to be separated with a
comma. In C and C++, commas are used as a separator of definitions
and function calls.
9. How do we initialize an array?
With this example, we have 5 elements that can store integer
information. If we want to access the second element, we need to call
index one. Just like this:
ledPin [1]
With the evolution of C++, it has led to the adoption of universal
initialization also for arrays. An equal sign can no longer be used
between the declaration and the initialized values. The following
examples are similar:
int ledPin [5] = {2, 3, 4, 5, 8};
10. Accessing the Values in an Array
How can we access the values stored in an Array?
The values inside an array can be accessed similar to
how the values of regular variables are accessed with the
same data type.
For example:
int ledPin [5];
Just like our example previously, this is similar to:
11. Accessing the Values in an Array
If we want to store integer information in Index 1, we can
do it like this:
ledPin [1] = 3;
An array with a specified index that was previously stored
with information, can be used also to assign values to other
declared values.
For example,
ledPin [1] = 3;
13. Array Programming Example
For the code, copy the program shown below. It will light up the LED lights, and it
will explain further the use of arrays in programming.
int ledPins [5] = {3,5,6,9};
void setup()
{
pinMode (ledPins [0],OUTPUT);
pinMode (ledPins [1],OUTPUT);
pinMode (ledPins [2],OUTPUT);
pinMode (ledPins [3],OUTPUT);
}
15. TRANSFER
Using the Diagram below create a program using array function
which will make the led turn ON with 1 second ( 1000) delay and
upload it in the arduino to test the program.
16. Assignment:
On a short coupon bond, using the diagram provided, write a the
appropriate program using array function putting the led ON with a 1000
(1 second) delay and OFF with a 2000 (2 seconds) delay. Present to the
class tomorrow.