100% found this document useful (1 vote)
52 views54 pages

Arduino Syntax Reference Guide

The document provides an overview of key concepts in the Arduino programming environment including comments, operators, variables, data types, functions, conditional statements, and loops. It explains that comments are lines of text ignored by the compiler, the equals and comparison operators, and common variable types like boolean, integer, and character. It also outlines how to declare and assign variables as well as variable scope. The document describes the setup and loop functions that define the main structure of Arduino sketches and how to use if/else statements and for/while loops for conditional execution and repetition.

Uploaded by

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

Arduino Syntax Reference Guide

The document provides an overview of key concepts in the Arduino programming environment including comments, operators, variables, data types, functions, conditional statements, and loops. It explains that comments are lines of text ignored by the compiler, the equals and comparison operators, and common variable types like boolean, integer, and character. It also outlines how to declare and assign variables as well as variable scope. The document describes the setup and loop functions that define the main structure of Arduino sketches and how to use if/else statements and for/while loops for conditional execution and repetition.

Uploaded by

Yumvuhore bosco
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Code

[Link]
The Arduino Environment
Board Type
Serial Port / COM Port
The Environment
Parts of the Sketch
Comments

• Comments can be anywhere


Comments

• Comments can be anywhere


• Comments created with // or /*
and */
Comments

• Comments can be anywhere


• Comments created with // or /*
and */
• Comments do not affect code
Comments

• Comments can be anywhere


• Comments created with // or /*
and */
• Comments do not affect code
• You may not need comments,
but think about the community!
Operators

The equals sign

= is used to assign a value

== is used to compare values


Operators

And & Or

&& is “and”

|| is “or”
Variables

Basic variable types:

Boolean
Integer
Character
Declaring Variables

Boolean: boolean variableName;


Declaring Variables

Boolean: boolean variableName;

Integer: int variableName;


Declaring Variables

Boolean: boolean variableName;

Integer: int variableName;

Character: char variableName;


Declaring Variables

Boolean: boolean variableName;

Integer: int variableName;

Character: char variableName;


String: stringName [ ];
Assigning Variables

Boolean: variableName = true;


or variableName = false;
Assigning Variables

Boolean: variableName = true;


or variableName = false;
Integer: variableName = 32767;
or variableName = -32768;
Assigning Variables

Boolean: variableName = true;


or variableName = false;
Integer: variableName = 32767;
or variableName = -32768;
Character: variableName = ‘A’;
or stringName = “SparkFun”;
Variable Scope
Where you declare your variables matters
Setup
void setup ( ) { }

The setup function comes before


the loop function and is necessary
for all Arduino sketches
Setup
void setup ( ) { }

The setup header will never change,


everything else that occurs in setup
happens inside the curly brackets
Setup
void setup ( ) {
pinMode (13, OUTPUT); }

Outputs are declare in setup, this is


done by using the pinMode function
This particular example declares digital pin # 13 as an
output, remember to use CAPS
Setup
void setup ( ) { [Link];}

Serial communication also begins in


setup
This particular example declares Serial communication
at a baud rate of 9600. More on Serial later...
Setup, Internal Pullup Resistors
void setup ( ) {
digitalWrite (12, HIGH); }

You can also create internal pullup resistors in setup, to


do so digitalWrite the pin HIGH
This takes the place of the pullup resistors currently on
your circuit 7 buttons
Setup, Interrupts
void setup ( ) {
attachInterrupt (interrupt, function,
mode) }
You can designate an interrupt
function to Arduino pins # 2 and 3

This is a way around the linear


processing of Arduino
Setup, Interrupts
void setup ( ) {
attachInterrupt (interrupt, function,
mode) }
Interrupt: the number of the interrupt, 0
or 1, corresponding to Arduino pins # 2
and 3 respectively

Function: the function to call when the


interrupt occurs

Mode: defines when the interrupt should


be triggered
Setup, Interrupts
void setup ( ) {
attachInterrupt (interrupt, function,
mode) }
•LOW whenever pin state is low
•CHANGE whenever pin changes value
•RISING whenever pin goes from low to
high
•FALLING whenever pin goes from low to
high

Don’t forget to CAPITALIZE


If Statements
if ( this is true ) { do this; }
If
if ( this is true ) { do this; }
Conditional
if ( this is true ) { do this; }
Action
if ( this is true ) { do this; }
Else
else { do this; }
Basic Repetition

• loop

• For

• while
Basic Repetition

void loop ( ) { }
Basic Repetition

void loop ( ) { }
Basic Repetition

void loop ( ) { }

The “void” in the header is what


the function will return (or spit out)
when it happens, in this case it
returns nothing so it is void
Basic Repetition

void loop ( ) { }

The “loop” in the header is what the


function is called, sometimes you make
the name up, sometimes (like loop) the
function already has a name
Basic Repetition

void loop ( ) { }

The “( )” in the header is where you


declare any variables that you are
“passing” (or sending) the function, the
loop function is never “passed” any
variables
Basic Repetition

void loop ( ) { }
Basic Repetition

for (int count = 0; count<10; count++)


{
//for action code goes here
//this could be anything
}
Basic Repetition

for (int count = 0; count<10; count++)


{
//for action code goes here
}
Basic Repetition

for (int count = 0; count<10; count++)


{
//for action code goes here
}
Basic Repetition

for (int count = 0; count<10; count++)


{
//for action code goes here
}
Basic Repetition

for (int count = 0; count<10; count++)


{
//for action code goes here
}
Basic Repetition

for (int count = 0; count<10; count++)


{
//for action code goes here
}
Basic Repetition

for (int count = 0; count<10; count++)


{
//for action code goes here
}
Basic Repetition

while ( count<10 )
{
//while action code goes here
}
Basic Repetition

while ( count<10 )
{
//while action code goes here
//should include a way to change count
//variable so the computer is not stuck
//inside the while loop forever
}
Basic Repetition

while ( count<10 )
{
//looks basically like a “for” loop
//except the variable is declared before
//and incremented inside the while
//loop
}
Basic Repetition
Or maybe:

while ( digitalRead(buttonPin)==1 )
{
//instead of changing a variable
//you just read a pin so the computer
//exits when you press a button
//or a sensor is tripped
}
Questions?
[Link]
6175 Longbow Drive, Suite 200
Boulder, Colorado 80301

Common questions

Powered by AI

Interrupts allow an Arduino program to respond immediately to specific events, bypassing the default linear execution order. The attachInterrupt function configures these by linking a hardware event to a function. It requires parameters for interrupt number (0 for pin 2, 1 for pin 3), the function to execute, and the triggering mode (LOW, CHANGE, RISING, or FALLING).

Variable scope in Arduino determines the parts of a program where a variable can be accessed. Declaring variables globally makes them accessible throughout the entire sketch, whereas local declarations within functions like setup or loop limit accessibility to those specific blocks, affecting how variables retain or lose state across function calls .

Comments in Arduino sketches do not affect the execution of the code as they are ignored by the compiler. Despite this, comments are useful for providing explanations and clarifications for the benefit of other people who might read or use the code, fostering a better understanding and collaborative development practices .

Internal pull-up resistors in Arduino are created by writing a digital pin HIGH in the setup function (digitalWrite(12, HIGH)). This technique is used to avoid the need for external pull-up resistors, reducing circuit complexity and ensuring reliable pin state definition in switch or button input scenarios .

Serial communication in Arduino facilitates data transmission over a communication port by initializing with Serial.begin in the setup function, often with a baud rate of 9600. This establishes the communication speed, allowing the program to send and receive data smoothly to connected devices like computers or other microcontrollers .

In Arduino, assigning values to a variable is done using the equals sign '=', whereas comparing values uses '=='. Assignment changes the value stored in a variable, while comparison is used in conditional statements to evaluate if two values are equal, affecting the program's flow based on this evaluation .

The loop function continuously executes the code within it after the setup function has completed. Unlike setup, which runs once at the start, loop runs repeatedly, enabling the Arduino to perform repetitive tasks, like checking sensor statuses or updating outputs .

For loops initialize a variable, define a condition, and increment automatically within a single line, making them suited for fixed iteration counts. While loops repeat the code as long as a condition is true and require separate initialization and incrementation, allowing for more dynamic exit conditions like user input .

If-else statements in Arduino guide the program flow by executing a block of code if a specified condition is true, and an alternate block if the condition is false. The syntax requires the condition in parentheses, followed by the action in curly braces, e.g., if (this is true) { do this; } else { do this; } .

Setting up a digital pin as an output is done in the setup function using the pinMode function, e.g., pinMode(13, OUTPUT). This configuration is essential as it determines how the pin operates, enabling it to control an external device such as an LED or relay by sending signals .

You might also like