Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
20 views

4.3 Arduino Language Reference

The document provides an overview of the Arduino language and core concepts including: 1) Arduino programs are divided into three main parts - structure, values (variables and constants), and functions. The setup() function is called once and loop() is called repeatedly. 2) The Arduino language is based on C/C++. Programs use basic commands like delay(), digitalWrite(), and analogWrite() to control inputs/outputs. 3) Variables and constants are declared and used within functions. Control structures like if/else and for loops alter program flow. Functions, data types, and operators work similar to C/C++.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

4.3 Arduino Language Reference

The document provides an overview of the Arduino language and core concepts including: 1) Arduino programs are divided into three main parts - structure, values (variables and constants), and functions. The setup() function is called once and loop() is called repeatedly. 2) The Arduino language is based on C/C++. Programs use basic commands like delay(), digitalWrite(), and analogWrite() to control inputs/outputs. 3) Variables and constants are declared and used within functions. Control structures like if/else and for loops alter program flow. Functions, data types, and operators work similar to C/C++.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 77

Arduino Language Reference

Roman Lopez Ph. D.


3/18/2020
9/01/2021
Arduino main parts
Arduino programs can be divided in three main parts: structure, values
(variables and constants), and functions.
The Arduino language is based on C/C++.
Program Skeleton
Arduino Structure
In Arduino, the standard program entry point (main) is defined in the core
and calls into two functions in a sketch.

setup() is called once, then loop() is called repeatedly (until you reset your
board).
An Arduino program run in two parts:
void setup(): Is preparation, and loop() is execution.
In the setup section, always at the top of your program, you would set
pinModes, initialize serial communication, etc.
void loop(): Is the code to be executed -- reading inputs,
triggering outputs, etc.
Variable Declaration
Function Declaration
void
SETUP

void setup() {
port #

pinMode(9, OUTPUT);
Input or Output
}

http://www.arduino.cc/en/Reference/HomePage
LOOP

void loop() { Port # from setup

digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(1000);
Turn the LED on
or off
} Wait for 1 second
or 1000 milliseconds

7
Basic Commands in Arduino IDE
delay( value ) : waits a noted amount of time before moving on to following
commands
value : a number denoting time in milliseconds (1000 milliseconds = 1
second)
digitalWrite( variable , setting ) : sets a component to on ( HIGH ) or off ( LOW )
variable : name of the component
setting : Either HIGH or LOW
digitalRead( variable ) : returns the value of a noted component, either HIGH or
LOW
variable : name of the component
Basic Commands in Arduino IDE
analogWrite( variable , value ) : sets a component to a certain value
from 0-255
variable : name of the component
value : A number 0-255
NOTE: can only be used when a component is plugged into a pin with
~symbol
analogRead( variable ) : returns the value of a noted component, a
number 0-255
variable : name of the component
NOTE: can only be used when a component is plugged into a pin with
~symbol
Variables & Functions

int variable = value creates an integer variable and assigns it a


value
variable : name of the variable written in camelCase
value : A whole number (positive or negative)
DECLARING A VARIABLE

int val = 5;

assignment
Type “becomes”
value
variable name
11
USING VARIABLES
int delayTime = 2000;
int greenLED = 9;
void setup() {
Declare delayTime
pinMode(greenLED, OUTPUT); Variable

void loop() {

digitalWrite(greenLED, HIGH);
delay(delayTime);
Use delayTime
digitalWrite(greenLED, LOW);
Variable
delay(delayTime);

}
12
Using Variables
int delayTime = 2000;
int greenLED = 9;

void setup() {
pinMode(greenLED, OUTPUT);
}
void loop() {
digitalWrite(greenLED, HIGH);
delay(delayTime);
digitalWrite(greenLED, LOW);
delayTime = delayTime - 100;
delay(delayTime);
} subtract 100 from
delayTime to gradually
increase LED’s blinking
speed
13
Variables & Functions
void function (){} : defines a function as the commands found
between the curly braces that will not return a value
function : name of the function, written in camelCase
To call the function, simply write the function name and
a set of parentheses, ie. myFunction();
Parameters can be included in the parentheses
Variables & Functions
int function (){} : defines a function as the commands found
between the curly braces that will return an integer
function : name of the function, written in camelCase
To call the function, simply write the function name and
a set of parentheses, ie. myFunction();
Parameters can be included in the parentheses
Control Structure
if
if...else
IF Condition

if(true)
{
asdfadsf
“perform some action”
}
Sensor Conditions to use in if/else statements
digitalRead( component ) : returns the digital value of a
component as 1 (on) or 0 (off)
component : name of the component to be checked
analogRead( component ) : returns the analog value of a
component
component : name of the component to be checked
IF Condition

if(true)
{
asdfadsf
“perform some action”
}
Control Structures
for
IF - ELSE Condition

if( “answer is true”)


{
“perform some action”
}
asdfadsf
else
{
“perform some other action”
}
IF - ELSE Example
int counter = 0;
void setup() {
Serial.begin(9600);
}
void loop() {

if(counter < 10)


{
Serial.println(“less than 10”);
}
else
{
Serial.println(“greater than or equal to
10”);
Serial.end();
}
counter = counter + 1;
22
}
IF - ELSE IF Condition

if( “answer is true”)


{
“perform some action”
}
asdfadsf
else if( “answer is true”)
{
“perform some other action”
}
IF - ELSE Example
int counter = 0;
void setup() {
Serial.begin(9600);
}
void loop() {

if(counter < 10)


{
Serial.println(“less than 10”);
}
else if (counter == 10)
{
Serial.println(“equal to 10”);
}
else
{
Serial.println(“greater than 10”);
Serial.end();
}
counter = counter + 1;
}
24
Control Structures
switch case
Control Structures
while
Control Structures
do... while
Control Structures
Control Structures
break
Further Syntax
; (semicolon)
{} (curly braces)
General Operation
Arduino Operators
Boolean Operators

&& (and)
|| (or)
! (not)
Pointer Access Operators
* dereference operator
& reference operator
Bitwise Operators
& (bitwise and)
| (bitwise or)
^ (bitwise xor)
~ (bitwise not)
<< (bitshift left)
>> (bitshift right)
Port Manipulation
Compound Operators
++ (increment)
-- (decrement)
+= (compound addition)
-= (compound subtraction)
*= (compound multiplication)
/= (compound division)
&= (compound bitwise and)
|= (compound bitwise or)
Variables and Constants
Variables are expressions that you can use in programs to
store values, such as a sensor reading from an analog pin.
Data Types
Variables can have various types, which are described below.
STRING
Arrays
ADC in Arduino
The Arduino Uno board contains 16 pins for ADC

10-bit analog to digital converter

This means that it will map input voltages between 0 and 5


volts into integer values between 0 and 1023
Reading/Writing Analog Values
analogRead(A0); // used to read the analog value from the pin
A0

analogWrite(2,128);
Quantanization the signal
Converting Analog Value to Digital
Variable Scope & Qualifiers
static
volatile
const
PROGMEM
Serial Communication
Used for communication between the Arduino board and a computer or
other devices.
This communication happens via the Arduino board's serial or USB
connection and on digital pins 0 (RX) and 1 (TX). Thus, if you use these
functions, you cannot also use pins 0 and 1 for digital i/o.

Serial.begin(speed)
int Serial.available()
int Serial.read()
Serial.flush()
Serial.print(data)
Serial.println(data)
Input & Output
• Transferring data from the computer to an Arduino is done using
Serial Transmission
• To setup Serial communication we use the following

void setup() {

Serial.begin(9600);

}
61 61
Writing to the Console

void setup() {

Serial.begin(9600);
Serial.println(“Hello World!”);

void loop() {}

62
Functions
Digital I/O
pinMode(pin, mode)
digitalWrite(pin, value)
int digitalRead(pin)
Analog I/O
analogReference(type)
int analogRead(pin)
analogWrite(pin, value) - PWM
Advanced I/O
shiftOut(dataPin, clockPin, bitOrder, value)
unsigned long pulseIn(pin, value)
Time
unsigned long millis()
delay(ms)
delayMicroseconds(us)
Math
min(x, y)
max(x, y)
abs(x)
constrain(x, a, b)
map(value, fromLow, fromHigh, toLow, toHigh)
pow(base, exponent)
sqrt(x)
Trigonometry
sin(rad)
cos(rad)
tan(rad)
Random Numbers
randomSeed(seed)
long random(max)
long random(min, max)
External Interrupts
attachInterrupt(interrupt, function, mode)
detachInterrupt(interrupt)
Interrupts

interrupts()
noInterrupts()
Utilities

cast (cast operator)


sizeof() (sizeof operator)
Reference
keywords
ASCII chart
Atmega168 pin mapping

You might also like