Basics Arduino Programming (Supplementary)
Basics Arduino Programming (Supplementary)
Basics Arduino Programming (Supplementary)
Programming Language
Topics
• What is programming
• Basic structure and syntax
• Simple use of function
• Commonly encountered error in syntax
• Control Structure
• Commonly used conditional statements
• Variables and uses
• Data types
• Comment usage
What is programming?
Basic syntax
• Case Sensitive
• Semicolon ( ; )
• Paired parenthesis ( ) and curly braces { }
Case Sensitive
Semicolon
• To identify the boundary of the instruction of a certain statement
Example
Example
Function
void setup() {
// do some codes here
}
void loop() {
// do some codes here
}
Function
void setup() {
// do some codes here
}
void loop() {
// do some codes here
}
Curly braces
Curly braces
Curly braces
Comparison Operators
• == ( equal to )
• x == y (x is equal to y)
• != ( not equal )
• x != y (x is not equal to y)
•< ( less than )
• x < y (x is less than y)
•> ( greater than )
• x > y (x is greater than y)
• <= ( less than or equal to )
• x <= y (x is less than or equal to y)
• >= ( greater than or equal to )
• x >= y (x is greater than or equal to y)
Control structure
• if statement
• else if statement
If condition
• used in conjunction with a comparison operator, tests whether a certain
condition has been reached, such as an input being above a certain
number. The format for an if test is:
if (x > 50 ) { if (x > 50 ) {
// write some codes here // write some codes here
} }
else {
// write some codes here
}
Declaring variables
■ All variables have to be declared before they are used.
■ Declaring a variable means defining its type, and optionally, setting an initial value
(initializing the variable).
■ Variables do not have to be initialized (assigned a value) when they are declared, but it is
often useful.
int inputVariable1;
int inputVariable2 = 0; // both are correct
Common used data types:
• Int
• Integers are your primary data-type for number storage (whole numbers).
• Unsigned int
• unsigned int (unsigned integers) are the same as int but it only store
positive values
• Boolean
• A boolean holds one of two values, true or false, HIGH or LOW, ON or
OFF, 1 or 0.
• Float
• A number that has a floating-point numbers (decimal point).
Variables with constant variables
const int any_name = 13;
Example:
• int value = 100;
• int value = -100;
• unsigned int = 500
• boolean choice = true;
• float var_pi = 3.1416
Variables scoping
Local variables
• Declaration of a variable that can be access only within the scope inside
the curly braces { }
Local variable Error
Variables scoping
Global variables
• Declaration of a variable
that can be access in any
part of the program
Error variable Error
Tips:
• Multiline comment
• Everything that you write between these symbols will not be
compiled or even seen by your Arduino. Multiline comments
start with /* and end with */. Multiline comments are
generally used for documentation.
• Single-line comment
• When you put // on any line, the compiler ignores all text
after that symbol on the same line. This is great for
annotating specific lines of code or for “commenting out” a
particular line.