0% found this document useful (0 votes)
2 views

Arrays and Loops

The document explains the use of arrays and loops in Arduino programming, highlighting how arrays store multiple values under a single name and how loops, particularly 'for' loops, can efficiently iterate through these arrays. It provides syntax examples for declaring arrays, accessing their elements, and using loops to perform tasks such as printing values or controlling multiple LEDs. Key points include zero-based indexing for arrays and the importance of managing loop conditions to avoid infinite loops or out-of-bounds errors.

Uploaded by

aewe481
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Arrays and Loops

The document explains the use of arrays and loops in Arduino programming, highlighting how arrays store multiple values under a single name and how loops, particularly 'for' loops, can efficiently iterate through these arrays. It provides syntax examples for declaring arrays, accessing their elements, and using loops to perform tasks such as printing values or controlling multiple LEDs. Key points include zero-based indexing for arrays and the importance of managing loop conditions to avoid infinite loops or out-of-bounds errors.

Uploaded by

aewe481
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Arrays and Loops in Arduino

In Arduino programming, arrays and loops are essential tools for efficient and organized coding.

Arrays:

An array is a collection of variables stored under a single name, where each element is accessible using
an index. It allows storing multiple values of the same type in a single data structure, making it easier to
manage related data.

Syntax for Array Declaration:

int myArray[5]; // Declares an array of 5 integers


int myArray[] = {10, 20, 30, 40, 50}; // Initializes an array with 5 elements

Accessing Array Elements:


Arrays in Arduino (and C/C++) use zero-based indexing. This means the first element is accessed with an
index of 0, the second with 1, and so on.

• Indexing in arrays starts from 0 (zero-based indexing).

• For an array of size N, valid indices are 0 to N-1.

int value = myArray[2]; // Accesses the third element (30 in this case)
Loops with Arrays:

Loops, such as for and while, are often used to iterate through arrays, enabling efficient handling of
multiple elements.

Example: Printing Array Elements

int myArray[] = {10, 20, 30, 40, 50};

void setup() {
Serial.begin(9600); // Start serial communication
for (int i = 0; i < 5; i++) { // Loop through the array
Serial.println(myArray[i]); // Print each element
}

Indexing in Arrays:

• Index starts at 0. For an array of size N, the indices range from 0 to N-1.

• Attempting to access an index outside this range (e.g., myArray[5] in the above example) may
lead to undefined behavior.

• The index can be dynamically controlled using variables, often updated within loops.
By combining arrays and loops, Arduino programs can efficiently manage and manipulate data, whether
controlling multiple LEDs, reading multiple sensors, or processing data in bulk.

for Loop in Arduino

The for loop is a control structure used to repeatedly execute a block of code for a specified number of
iterations. It is particularly useful when you know in advance how many times you want the loop to run.

Syntax of for Loop

1. for (initialization; condition; update) {


2. // Code to execute repeatedly
3. }
4.
5. Initialization: Sets the starting value of a loop variable. It runs once before the loop begins.

6. Condition: Evaluates before each iteration. The loop continues as long as this condition is true.

7. Update: Adjusts the loop variable after each iteration, typically with an increment (++) or
decrement (--).

Example: Basic for Loop

void setup() {
Serial.begin(9600);
for (int i = 0; i < 5; i++) { // Loops from 0 to 4
Serial.println(i); // Prints the current value of i
}

Output:

4
Common Use Cases

1. Iterating Through Arrays

2. int myArray[] = {10, 20, 30, 40, 50};


3. void setup() {
4. Serial.begin(9600);
5. for (int i = 0; i < 5; i++) {
6. Serial.println(myArray[i]);
7. }
8. }
9.
2. Controlling Multiple LEDs

3. int ledPins[] = {2, 3, 4, 5}; // LED connected to pins


4. void setup() {
5. for (int i = 0; i < 4; i++) {
6. pinMode(ledPins[i], OUTPUT);
7. }
8. }
9. void loop() {
10. for (int i = 0; i < 4; i++) {
11. digitalWrite(ledPins[i], HIGH); // Turn on LEDs
12. delay(500);
13. digitalWrite(ledPins[i], LOW); // Turn off LEDs
14. }
15. }
16.
3. Counting Backwards

4. void setup() {
5. Serial.begin(9600);
6. for (int i = 10; i >= 0; i--) { // Loops from 10 to 0
7. Serial.println(i);
8. delay(1000); // 1-second delay
9. }
10. }
11.

Advantages of for Loop


• Compact: Combines initialization, condition, and update in one line.

• Efficiency: Great for managing repetitive tasks like iterating over arrays.

• Control: Offers clear control over the number of iterations.

Usage

• Ensure the condition avoids infinite loops (e.g., for (;;) runs forever).

• Avoid accessing out-of-bounds indices in arrays (e.g., myArray[i] where i exceeds array size).

• You can nest for loops for multi-dimensional operations but use them sparingly to maintain
readability.

You might also like