Arduino sketch structure

The structure of an Arduino sketch in the C programming language typically consists of two main parts: the setup() function and the loop() function.

The setup() Function:

The setup() function is called once when the Arduino board is powered on or reset. It is used to initialize variables, pin modes, and any other settings that need to be configured before the main program loop starts running. The setup() function has the following syntax:

void setup() {
  // Initialization code goes here
}

The loop() Function:

The loop() function is the heart of an Arduino sketch. It is executed repeatedly after the setup() function is called. The code inside the loop() function will be executed over and over again, allowing the Arduino to perform its main tasks. The loop() function has the following syntax:

void loop() {
  // Main program code goes here
}

Inside the loop() function, you can write the code that controls the behavior of your Arduino board. This code will be executed repeatedly until the Arduino is powered off or reset. You can use various functions and libraries provided by Arduino to interact with sensors, actuators, and other components connected to the board.

Putting it All Together:

Here is an example of a basic Arduino sketch that blinks an LED connected to pin 13:

void setup() {
  pinMode(13, OUTPUT); // Set pin 13 as output
}

void loop() {
  digitalWrite(13, HIGH); // Turn on the LED
  delay(1000); // Wait for 1 second
  digitalWrite(13, LOW); // Turn off the LED
  delay(1000); // Wait for 1 second
}

In this example, the setup() function sets pin 13 as an output, and the loop() function repeatedly turns on and off the LED connected to pin 13 with a 1-second delay between each state change.

This is the basic structure of an Arduino sketch in the C programming language. You can add more functions, variables, and logic to create more complex Arduino projects.