0% found this document useful (0 votes)
180 views31 pages

Week 3 - Arduino Programming

Arduino programming uses a C++-based language with a basic structure that includes two required functions: setup() and loop(). Setup() initializes variables and runs once at power-up, while loop() continuously executes the main code. Variables and constants can be used, along with basic math operators and conditional statements like if/else and for/while loops to control the Arduino board's behavior.

Uploaded by

Jef
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
180 views31 pages

Week 3 - Arduino Programming

Arduino programming uses a C++-based language with a basic structure that includes two required functions: setup() and loop(). Setup() initializes variables and runs once at power-up, while loop() continuously executes the main code. Variables and constants can be used, along with basic math operators and conditional statements like if/else and for/while loops to control the Arduino board's behavior.

Uploaded by

Jef
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 31

• Arduino Programming is C++ based.

• It is the unit of code that is uploaded to and run on an


Arduino board.
• The basic structure of the Arduino programming language is fairly simple and runs
in at least two parts. These two required parts, or functions, enclose blocks of
statements.

void setup() {
//setup code
}
void loop() {
//looping code
}
setup()

• The setup() function is called when a sketch starts. Use it to initialize


the variables, pin modes, start using libraries, etc. The setup function
will only run once, after each power up or reset of the Arduino board.
setup()

• Only runs once

void setup {
pinMode(pin,OUTPUT); //instantiates that pin as OUTPUT
}
loop()

• After creating a setup() function, which initializes and sets the initial
values, the loop() function does precisely what its name suggests, and
loops consecutively, allowing your program to change and respond.
Use it to actively control the Arduino board.
loop()
• loop() runs endlessly once setup is done.

void loop(){
digitalWrite(pin,HIGH); // +5V to pin
outputs
delay(1000); //pauses for a second
digitalWrite(pin,LOW); // 0V to pin
outputs
delay(1000); //pauses for a second
//loops
}
{ }

• Curly braces (also referred to as just "braces" or "curly brackets")


define the beginning and end of function blocks and statement blocks
such as the void loop() function and the for and if statements.
;

• A semicolon must be used to end a statement and separate


elements of the program. A semicolon is also used to
separate elements in a for loop.
//

• Single line comments begin with // and end with the next line
of code. Like block comments, they are ignored by the
program and take no memory space.
/* ... */

• Block comments, or multi-line comments, are areas of text


ignored by the program and are used for large text descriptions
of code or comments that help others understand parts of the
program. They begin with /* and end with */ and can span
multiple lines.
void Boolean char Unsigned byte int Unsigned word
char int
long Unsigned short float double array String- String-
long char-array object
• A variable is a way of naming and storing a numerical value for later use by
the program.
•A variable needs to be declared and optionally assigned to the value needing
to be stored.
• All variables have to be declared before they can be used. Declaring a variable
means defining its value type.
• A global variable is one that can be seen and used by every function
and statement in a program. This variable is declared at the beginning
of the program, before the setup() function.
• A local variable is one that is defined inside a function or as part of a
for loop. It is only visible and can only be used inside the function in
which it was declared.
// 'value' is visible to any function
int value;
void setup() {
// no setup needed
}
void loop()
{
for (int i=0; i<20;) { // 'i' is only visible inside the for loop
i++;
}
float f; // 'f' is only visible inside loop
}
• The Arduino language has a few predefined values, which are called constants. They
are used to make the programs easier to read. Constants are classified in groups.
HIGH / LOW

• These constants define pin levels as HIGH or LOW and are used
when reading or writing to digital pins. HIGH is defined as logic
level 1, ON, or 5 volts while LOW is logic level 0, OFF, or 0 volts.
TRUE / FALSE

• These are Boolean constants that define logic levels. FALSE is easily
defined as 0 (zero) while TRUE is often defined as 1, but can also be
anything else except zero. So in a Boolean sense, -1, 2, and -200 are all
also defined as TRUE.
INPUT / OUTPUT

• Constants used with the pinMode() function to define the


mode of a digital pin as either INPUT or OUTPUT.
A = 20 ; B = 10;
Name Symbol Description Result when is A the first
term of operation

Assignment = Stores the value to the right of the equal sign in the 20
variable to the left of the equal sign.
Addition + Adds two operands 30

Subtraction - Subtracts second operand from the first 10

Multiplication * Multiply both operands 200

Division / Divide numerator by denominator 2

Modulo % Modulus Operator and remainder of after an 0


integer division
A = 20 ; B = 10;
Name Symbol Description Result when is A the first
term of operation

not equal to != True when both values are the same TRUE

less than < True when left operand has lesser value than right FALSE

greater than > True when left operand has greater than right TRUE

less than or equal True when left operand is less than or equal to the
<= FALSE
to right.
greater than or True when left operand is greater than or equal to
>= TRUE
equal to the right
A = 20 ; B = 10;
Name Symbol Description Result when is A the first
term of operation

Called Logical AND operator. If both the


and && operands are non-zero then then condition TRUE
becomes true.

Called Logical OR Operator. If any of the two


or || operands is non-zero then then condition TRUE
becomes true.

Called Logical NOT Operator. Use to reverses the


not ! logical state of its operand. If a condition is true then FALSE
Logical NOT operator will make false.
Name Symbol Name Symbol

and & not ~

or | shift left <<

xor ^ shift right >>


Name Symbol Name Symbol

increment ++ compound division /=

decrement -- compound modulo %=

compound addition += compound bitwise or |=

compound subtraction -= compound bitwise and &=

compound multiplication *=
if { ... }
• if statements test whether a certain condition has been reached, such
as an analog value being above a certain number, and executes any
statements inside the brackets if the statement is true. If false the
program skips over the statement.

if(someVar ?? someValue){
someAction();
}
if { ... } else {...}
• if… else allows for ‘either-or’ decisions to be made. For example, if you
wanted to test a digital input, and do one thing if the input went HIGH
or instead do another thing if the input was LOW

if(someVar ?? someValue){
someAction();
}
else {
someOtherAction();
}
for {...}
• The for statement is used to repeat a block of statements enclosed in curly braces
a specified number of times. An increment counter is often used to increment and
terminate the loop. There are three parts, separated by semicolons (;)

for(init; condition; expression){


someAction();
}
while { ... }
• while loops will loop continuously, and infinitely, until the expression inside the
parenthesis becomes false. Something must change the tested variable, or the while
loop will never exit. This could be in your code, such as an incremented variable, or
an external condition, such as testing a sensor.

while(someVar ?? someValue){
someAction();
}
do { ... } while()
• The do loop is a bottom driven loop that works in the same manner as the while
loop, with the exception that the condition is tested at the end of the loop, so the
do loop will always run at least once.

do
{ someAction
();
}
while(someVar ??
someValue);
• A function is a block of code that has a name and a block of
statements that are executed when the function is called.
THANK YOU!

You might also like