CIPAD 37: Introduction comment créer(déclarer) un tableau unidimensionnel (array)

3 min read 5 days ago
Published on Oct 02, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial will guide you through the process of creating and using one-dimensional arrays in Arduino programming, as presented in Daniel Talbot's video. Understanding arrays is essential for managing collections of data efficiently in your projects. By the end of this guide, you will be able to declare arrays and access their elements effectively.

Step 1: Understanding One-Dimensional Arrays

  • Definition: A one-dimensional array is a collection of elements that are all of the same type, stored in a single row.
  • Use Cases: Arrays are useful for storing multiple values in a single variable, such as sensor readings, or a list of numbers.

Step 2: Declaring an Array

To declare a one-dimensional array in Arduino, follow these steps:

  1. Choose a Data Type: Decide the type of data you want to store (e.g., int, float, char).
  2. Define the Array Size: Determine how many elements you want in your array.
  3. Write the Declaration: Use the following syntax:
    dataType arrayName[arraySize];
    
    Example for an integer array with 5 elements:
    int myArray[5];
    

Step 3: Initializing an Array

You can initialize an array at the time of declaration by providing values in curly braces.

  • Syntax:
    dataType arrayName[arraySize] = {value1, value2, value3, ...};
    
  • Example:
    int myArray[5] = {1, 2, 3, 4, 5};
    

Step 4: Accessing Array Elements

To access elements in the array, use the index of the element you want to retrieve. Remember that indexing starts at 0.

  1. Access Syntax:
    arrayName[index];
    
  2. Example:
    int firstElement = myArray[0]; // Gets the first element (1)
    

Step 5: Modifying Array Elements

You can change the value of an array element using the same indexing method.

  • Modification Syntax:
    arrayName[index] = newValue;
    
  • Example:
    myArray[2] = 10; // Changes the third element from 3 to 10
    

Step 6: Practical Example

Here’s a simple Arduino sketch that demonstrates declaring, initializing, and accessing an array:

void setup() {
    Serial.begin(9600);
    int myArray[5] = {10, 20, 30, 40, 50};

    // Access and print each element
    for (int i = 0; i < 5; i++) {
        Serial.println(myArray[i]);
    }

    // Modify an element
    myArray[1] = 25; // Change second element
}

void loop() {
    // Nothing here for this example
}

Conclusion

You’ve now learned how to create, initialize, and manipulate one-dimensional arrays in Arduino. Arrays are crucial for handling multiple data points in your projects, making your code more organized and efficient.

For further practice, try implementing arrays in a simple project, such as storing and processing sensor data. If you want to explore more, check out the provided Tinkercad links for hands-on examples.