Sketch structure
While there are no rules for structuring a sketch other than using the setup() and loop() functions as intended, it helps to organize your code in a consistent way, especially for larger sketches.
The code structure used in our projects is explained below.
arduino_sketch_template.ino
/*
Project: A project may comprise multiple circuits and sketches.
Function: The function this sketch serves.
Circuit: The circuit within the project this sketch is for.
Boards Supported: Boards supported.
Description: Function description.
Author: STEMVentor Educonsulting
This code is copyrighted. Please do not reuse or share for any purpose other than for learning with subscribed STEMVentor programs. This code is for educational purposes only and is not for production use.
*/
// LIBRARIES
// Libraries are programs that can be used within a sketch to simplify various functions, such as reading data from complex sensors, connecting to WiFi and Bluetooth, and other such commonly used functions. Libraries may be provided by the board OEM or by individuals. Libraries can be installed from the Arduino IDE using the library installation option.
// PIN DEFINTIONS
// Components are connected to pins on the board and these pins must be specified for use in the sketch.
// CONSTANT DEFINITIONS
// A sketch may need some values that are used for computations in multiple functions but are fixed and do not change. These can be defined here as constants.
// GLOBAL VARIABLES
// A sketch may need some values that are used for computations in mulitple functions and may change through the processing steps. These can be defined here as global variables.
// INITIALIZE OBJECTS
// Libraries usually follow an object-oriented approach that requires an instance of the class to call its methods.
// LOCAL FUNCTIONS
// These functions separate out reusable or specific tasks and can be called in the setup() or loop() functions as required. In addition to reusability, this helps organize and manage code better.
// THE setup FUNCTION RUNS ONCE WHEN YOU PRESS RESET OR POWER THE BOARD.
void setup() {
Serial.begin(9600);
Serial.println("Board connected. No program loaded.");
}
// THE loop FUNCTION RUNS OVER AND OVER AGAIN FOREVER UNTIL THE BOARD IS POWERED OFF.
void loop() {
delay(1000*10); // In milliseconds
Serial.println("Board connected. No program loaded.");
}