Skip to content

1.6 More Arduino Basics

John-Paul Chouery edited this page Aug 24, 2024 · 1 revision

Congratulations on your progress so far! As you prepare to dive into more complex projects like the power board and thrusters, it's essential to strengthen your understanding of some fundamental concepts in Arduino programming. Here are some key topics to focus on:

1. For Loops

Description: For loops allow you to repeat a block of code a specific number of times. This is particularly useful for tasks like iterating through arrays or performing repetitive actions.

Example:

for (int i = 0; i < 10; i++) {
  Serial.println(i); // Print numbers 0 to 9
}

2. While Loops

Description: While loops continue running as long as a specified condition is true. They are useful for situations where the number of iterations isn’t predetermined.

Example:

int count = 0;
while (count < 10) {
  Serial.println(count);
  count++; // Increment count
}

3. Arrays

Description: Arrays allow you to store multiple values in a single variable. This is helpful for managing lists of data, such as multiple sensor readings.

Example:

int sensorValues[5]; // Declare an array of 5 integers
sensorValues[0] = analogRead(A0); // Read value from analog pin A0

4. Functions

Description: Functions allow you to create reusable blocks of code. They can take parameters and return values, helping to organize your code and make it more modular.

Example:

void turnOnLED() {
  digitalWrite(13, HIGH); // Turn on LED
}

5. Libraries

Description: Libraries are pre-written code that adds functionality to your project. They can simplify tasks like controlling specific hardware components (e.g., servos, displays).

Example:

#include <Servo.h> // Include the Servo library

Servo myServo; // Create a servo object

6. Interrupts (Advanced)

Description: Interrupts allow you to pause the normal flow of the program to respond to an event, such as a button press. This is useful for making responsive applications.

Example:

void setup() {
  attachInterrupt(digitalPinToInterrupt(buttonPin), handleButtonPress, RISING);
}

7. Analog Read

Description: The analogRead() function is used to read analog signals from components like potentiometers or sensors. This allows for varied input beyond just HIGH or LOW.

Example:

int sensorValue = analogRead(A0); // Read analog value from pin A0

As you explore these concepts, you'll be better prepared for the challenges ahead. If you have any questions or need assistance, please reach out to a lead or senior member of the team. Happy coding!

Clone this wiki locally