100% found this document useful (1 vote)
247 views15 pages

Arduino Sketch Structure Explained

The document discusses the structure and syntax of Arduino sketches, including that sketches have a setup and loop structure containing blocks and statements, and that comments can be used to explain code. It also overview control structures for sequential, selection, and iteration flow and the use of variables, constants, and operators to perform operations on values.

Uploaded by

Costi.H
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
247 views15 pages

Arduino Sketch Structure Explained

The document discusses the structure and syntax of Arduino sketches, including that sketches have a setup and loop structure containing blocks and statements, and that comments can be used to explain code. It also overview control structures for sequential, selection, and iteration flow and the use of variables, constants, and operators to perform operations on values.

Uploaded by

Costi.H
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
  • Arduino Program Structure
  • Syntax
  • Overview
  • Example Sketch
  • Main Structure
  • Control Structures
  • Operators
  • Language Reference

Arduino Sketch

Structure Values Functions

1) Syntax 1) Variables

2) Main structure 2) Constants

3) Control
structures

4) Operators
Statement
• Ends with “;”
• Like a full stop at the end of a sentence in English

Block
• Enclosed by {}
• Has a name in front of first curly bracket
• Like a “paragraph”
Comments are ignored by the Arduino IDE.

Multi-line comment: between /* … */


/*
I am a comment
I am a comment too!
*/

Single line comment: everything after //

// This whole line is a comment


digitalWrite(led, HIGH); // comment here
1. Open Arduino IDE
2. Go to File > Examples >
[Link] > Blink
3. Delete all the comments
4. Identify the statements
and blocks
void setup() {
pinMode(13, OUTPUT);
Can you identify
}
the blocks and
statements in this
void loop() {
example?
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
void setup() {
led, OUTPUT); Observe that
} there are 2
Blocks.

void loop() { Recap:


Blocks are
enclosed by {}
}
void setup() {
pinMode(13, OUTPUT); Observe that
} there are 5
Statements.
void loop() {
digitalWrite(13, HIGH); Recap:
delay(1000); Statements end
digitalWrite(13, LOW); with “;”
delay(1000);
}
Structure refers to the parts and
organization of something.

The Arduino main structure refers to


the parts which MUST be present in
every sketch

[Link]
void setup() {}
The setup block contains the “settings” and runs just once when:
- The Arduino is reset/restarted or turned on
- New sketch is uploaded

void loop() {}
The loop block contains the “main” code and runs continuously
until:
- The Arduino is turned off or reset/restarted
- New sketch is uploaded
Usually the code will be organized in 3 sections.

1. Variable declarations
2. Setup – everything you want to do once
3. Loop – everything you want to repeat
void setup() {
//some thing to do once
}

void loop() {
//insert some pattern to illustrate repeat
or looping
}
Control Flow is the order in which statements and blocks are
executed

Control Structures are bits of code that decide the control flow

Sequential Selection Iteration

Statement by Choosing which Repeating a


statement block to do block
Values

1+1
Operator
Operators do something to values.
- Assignment (=)
- Arithmetic (+, -, *, /, %)
- Comparison (==, >, <, >=, <=, !=)
- Boolean (&&, ||, !)
Where do you look for
additional references?

Language Reference
[Link]
en/Reference/HomePage

Arduino Sketch 
Values
1) Variables
2) Constants
Functions
Structure
1) Syntax
2) Main structure
3) Control 
structures
4) Op
Statement
• Ends with “;”
• Like a full stop at the end of a sentence in English
Block 
• Enclosed by {}
• Has a name in fron
Comments are ignored by the Arduino IDE.
Multi-line comment: between /* … */
/*
I am a comment
I am a comment too!
*/
Single
1. Open Arduino IDE
2. Go to File > Examples > 
1.Basics > Blink
3. Delete all the comments
4. Identify the statements
and bl
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
void setup() {
led, OUTPUT);
}
void loop() {
}
Observe that 
there are 2 
Blocks.
Recap: 
Blocks are 
enclosed by {}
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
http://2012books.lardbucket.org/books/management-principles-v1.0/s11-organizational-structure-and-c.html
Structure refers to
void setup() {}
The setup block contains the “settings” and runs just once when:
- The Arduino is reset/restarted or turned o

You might also like