Sie sind auf Seite 1von 53

Basics In Arduino

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?

• The process of developing and implementing various sets of


instruction to enable a computer to do a certain task.

• It uses a specific language when writing a code.


Arduino program structure
• The two required parts of the program:
• setup()
• setup() function will only run ONCE after each power-
up or reset of the Arduino board.

Note: It is used to initialize variables, pin modes, start using


libraries, etc.
• loop()
• loop() function loops consecutively, allowing your
program to change and respond.
• It runs over and over again.
Arduino program structure
Arduino Programming
• C/C++ programming language
• Programming platform that the Arduino uses

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

• A function is a group of statements that performs a task.


• Basic structure of a function:
• function name
• function parameters ( enclosed in parenthesis )
Note: for conditional purposes

void setup() {
// do some codes here
}
void loop() {
// do some codes here
}
Function

• A function is a group of statements that performs a task.


• Basic structure of a function:
• function name
• function parameters ( enclosed in parenthesis )
Note: for conditional purposes

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
}

Note: If statements will only be performed if the condition is true.


Example:
Let’s say the if condition is true
If condition
If condition
If condition
If condition
If condition
If condition
If condition
If condition
If condition
If condition
Example:
Let’s say the if condition is false
If condition
If condition
If condition
If condition
If condition
If condition
If condition
If condition
If condition
If condition
else if condition
• Allows greater control over the flow of code than the basic if statement,
by allowing multiple tests to be grouped together.

if (variable_x < 500)


{
// do Thing A
}
else if (variable_x >= 1000)
{
// do Thing B
}
else
{
// do Thing C
}
else if condition
• Value of variable_x is 400
if (variable_x < 500)
{
// do Thing A
}
else if (variable_x >= 1000)
{
// do Thing B
}
else
{
// do Thing C
}
else if condition
• Value of variable_x is 1400
if (variable_x < 500)
{
// do Thing A
}
else if (variable_x >= 1000)
{
// do Thing B
}
else
{
// do Thing C
}
else if condition
• Value of variable_x is 800
if (variable_x < 500)
{
// do Thing A
}
else if (variable_x >= 1000)
{
// do Thing B
}
else
{
// do Thing C
}
Variables
• A variable is a way of naming and storing a value

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.

Das könnte Ihnen auch gefallen