Sketch Structure

 

Sketch Structure




















Okay, let's break down the structure of an Arduino sketch (program) in plain English, with examples, without any extra resources or visuals.

An Arduino sketch has two required parts, always present:

  1. setup() function: This function runs once at the very beginning of your program when the Arduino starts or resets. Think of it as your initialization phase.

    • Example: arduino void setup() { pinMode(13, OUTPUT); // Set digital pin 13 as an output (for an LED, perhaps) Serial.begin(9600); // Initialize serial communication at 9600 bits per second } This setup() does two things: configures a pin and starts serial communication. These happen only once.
  2. loop() function: This function runs repeatedly after the setup() function has finished. It forms the main body of your program, handling ongoing tasks.

    • Example: arduino void loop() { digitalWrite(13, HIGH); // Turn the LED on (if pin 13 is connected) delay(1000); // Wait for 1 second (1000 milliseconds) digitalWrite(13, LOW); // Turn the LED off delay(1000); // Wait for 1 second } This loop() makes an LED blink on and off repeatedly. It never stops unless you power off or reset the Arduino.

Complete Minimal Example:

void setup() {
  // Put your setup code here, to run once:
}

void loop() {
  // Put your main code here, to run repeatedly:
}

This example does nothing, but it's a valid Arduino sketch.

Explanation of Components Used in the Examples:

  • void: Indicates the function doesn't return any value. setup() and loop() always return nothing.
  • pinMode(pin, mode): Sets the specified digital pin to be either an input or an output. pin is the pin number (e.g., 13). mode is either INPUT or OUTPUT.
  • digitalWrite(pin, value): Sets a digital pin to either HIGH (on, usually 5V) or LOW (off, usually 0V).
  • delay(milliseconds): Pauses the program for the specified number of milliseconds.
  • Serial.begin(baudRate): Initializes serial communication at a specific baud rate (e.g., 9600) for sending data to a computer.

Beyond the Core Structure:

  • Comments: Use // for single-line comments and /* ... */ for multi-line comments to explain your code.
  • Variables: Declare variables (e.g., int sensorValue;) outside of functions (globally) or inside functions (locally) to store data.
  • Functions: Create your own functions to organize your code into reusable blocks. You define them like this: void myFunctionName() { ... }
  • Include Statements: include libraries at the start of your sketch: #include <libraryName.h>.

Putting it All Together (More Complex Example):

// Global variable to store sensor reading
int sensorPin = A0; // Analog pin A0 connected to sensor
int sensorValue = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  sensorValue = analogRead(sensorPin); // Read the sensor value
  Serial.print("Sensor Value: ");
  Serial.println(sensorValue);         // Print the sensor value to the serial monitor
  delay(100);                         // Wait a short time before reading again
}

In this example:

  1. A global variable sensorValue is declared.
  2. The setup() initializes serial communication.
  3. The loop() reads an analog sensor connected to pin A0, prints the value to the serial monitor, and then waits.

That's the essence of the Arduino sketch structure. It's all about the setup() for initialization and the loop() for repeated execution.


Comments

Popular posts from this blog

Al-Khwarizmi: The Genius Who Counted the Future

Functions

Loops (for, while)