Arduino Sketch Structure and Flow

 

Arduino Sketch Structure and Flow

A basic Arduino sketch consists of two functions called setup() and loop().

Open the Arduino IDE and select File >>> New

To see the two functions. These two functions now appear in a default new Arduino IDE window,

What is a Function?

Functions will be covered in more detail later,
Rules when writing Arduino program

    
    1. The function name is followed by opening and closing parentheses () that may or may not contain something.
    2. All functions must have a return type. Both setup and loop have a void return type.
    3. The body of a function consists of an opening and closing brace ({ and }).
    4. All functions must have a unique name, setup is one example of a unique function name (setup and loop are special functions in Arduino programming and form part of the structure of a basic sketch).

Arduino program has three main parts:

1. variable declaration section
2. setup section
3. loop

1. VARIABLE DECLARATION SECTION

Programs often begin with introductory comments that explain what the program is doing.
These comments come before the variable declarations.

Variables are usually declared at the beginning of a program .
All of the variables that you are using in your code should be listed here, before the setup and loop sections.

 
2. SETUP SECTION

The setup section, which runs once when the program begins, then follows the variable declaration section.
Statements that lay the foundation for actions that happen later on in the program are put in the setup section.
3. LOOP SECTION

After the setup section runs, the loop section runs over and over until the LilyPad is turned off or reprogrammed—hence the word loop.
The statements that carry out the main action of your program are in this section.

Sketch Example
Now open Arduino IDE, navigate to file >>> New, copy and paste the following code



void setup() {
  Serial.begin(9600);
  Serial.println("Hello, Engineers!");
}

void loop() {
}


Press Ctrl + S from the keyboard or  File >> Save As... from the Arduino IDE menu and then renaming the file to hello.

Running our first Sketch

Plug your Arduino into your PC using a USB cable. and then click the Upload button to load the program to the Arduino.

Now open the Arduino IDE Serial Monitor window to see the sketch run and print the text message.


Source: ele obo

Learn more

Source: Basement Electronics






Comments

Popular posts from this blog

Arduino function libraries~Math Library Function

Project Four: Pattern, Blink 4 leds using Arduino board

Project 6: Arduino print hello