0% found this document useful (0 votes)
1 views

Programming Language Reference 1

The document provides a reference for the Arduino programming language, detailing constants, data types, variable scope, and qualifiers. It includes examples of syntax for defining constants, using data types like int, float, and char, and explains the usage of keywords such as const, static, and volatile. The information is aimed at assisting programmers in understanding and utilizing the Arduino language effectively.

Uploaded by

pallavitp7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Programming Language Reference 1

The document provides a reference for the Arduino programming language, detailing constants, data types, variable scope, and qualifiers. It includes examples of syntax for defining constants, using data types like int, float, and char, and explains the usage of keywords such as const, static, and volatile. The information is aimed at assisting programmers in understanding and utilizing the Arduino language effectively.

Uploaded by

pallavitp7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Arduino programming language reference

Construct Description
A0 – A5 Constant value for analog pins
0 - 13 Constant value for digital pins
; (semicolon) Used to end a statement
// Single line comment
/* */ Multiline comment
#define Allows the programmer to give a name to a constant value before
the program is compiled. Syntax is
#define constant_name value
#include Used to include an external library in the sketch. Syntax is
#include <LibraryFile.h>
Swarnalatha G.L, Assistant Professor, CSE dept, SSIT 42
Arduino programming language reference (contd.)
Constants
• Integer constants: Integer constants are numbers such as 123.
• Floating-point constants: Floating-point constants are numbers such as 10.0, 2.34E5
(2.34*10^5), 67e-12(67.0*10^-12).
• HIGH/LOW: When reading or writing to a digital pin there are only two possible
values a pin can take/be-set-to: HIGH and LOW. These are the same as 1 and 0.
• true/false: These are the two constants used to represent truth and falsity in the
Arduino language. true is defined as 1 and false is defined as 0. These constants are
typed in lowercase unlike HIGH and LOW.
• LED_BUILTIN: Most Arduino boards have a pin connected to an on-board LED in
series with a resistor. The constant LED_BUILTIN is the number of the pin to which
the on-board LED is connected. Most boards have this LED connected to digital pin
13.
• INPUT/OUTPUT: Pins can be configured as INPUT or OUTPUT. Pin configured as an
INPUT can only be read. Pin configured as an OUTPUT can only be set.
Swarnalatha G.L, Assistant Professor, CSE dept, SSIT 43
Arduino programming language reference (contd.)
Data types
• int: On the Arduino UNO, an int stores a 16-bit (2-byte) value. This yields a range
of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of ((2^15) –
1)). Syntax is
int variable_name = value;
• float: A float stores 32-bits (4-byte) of information. These can be as large as
3.4028235E+38 and as low as -3.4028235E+38. Syntax is
float variable_name = value;
• char: A data type used to store a character value. Character literals are written in
single quotes, like ‘A’. Multiple characters (strings) are written in double quotes,
like "ABC". Syntax is
char variable_name = value;
• double: Double precision floating point number. On the Arduino UNO boards, this
occupies 4 bytes. That is, the double implementation is exactly the same as the
float, with no gain in precision. Syntax is
double variable_name = value;
Swarnalatha G.L, Assistant Professor, CSE dept, SSIT 44
Arduino programming language reference (contd.)
• long: Long variables are extended size variables for number storage, and store 32
bits (4 bytes) value from -2,147,483,648 to 2,147,483,647. Syntax is
long variable_name = value;
• short: A short is a 16-bit data type. On all Arduino boards a short stores a 16-bit
(2-byte) value. This yields a range of -32,768 to 32,767 (minimum value of -2^15
and a maximum value of (2^15) - 1). Syntax is
short variable_name = value;
• unsigned int: On the Arduino UNO boards, unsigned integers are the same as
integers in that they store a 2 byte value. Instead of storing negative numbers
however they only store positive values, yielding a useful range of 0 to 65,535
((2^16) - 1). Syntax is
unsigned int variable_name = value;
• byte: A byte stores an 8-bit unsigned number, from 0 to 255. Syntax is
byte variable_name = value;
Swarnalatha G.L, Assistant Professor, CSE dept, SSIT 45
Arduino programming language reference (contd.)
• unsigned long: Unsigned long variables are extended size variables for number
storage, and store 32 bits (4 bytes). Unlike standard longs unsigned longs won’t
store negative numbers, making their range from 0 to 4,294,967,295 (2^32 - 1).
Syntax is
unsigned long variable_name = value;

Swarnalatha G.L, Assistant Professor, CSE dept, SSIT 46


Arduino programming language reference (contd.)
Variable Scope & Qualifiers
• const: The const keyword stands for constant. It is a variable qualifier that
modifies the behavior of the variable, making a variable "read-only". This means
that the variable can be used just as any other variable of its type, but its value
cannot be changed. You will get a compiler error if you try to assign a value to a
const variable. You can use either const or #define for creating numeric or string
constants. In general const is preferred over #define for defining constants.
Examples are
const int led = 5; or #define led 5
const float pi = 3.14;
• scope: A global variable is one that can be seen by every function in a program.
Local variables are only visible to the function in which they are declared. In the
Arduino environment, any variable declared outside of a function (e.g. setup(),
loop(), etc.), is a global variable. Example code is

Swarnalatha G.L, Assistant Professor, CSE dept, SSIT 47


Arduino programming language reference (contd.)
int PWMval; // any function will see this variable
void setup() {
//…
}
void loop() {
int i; // i is only visible inside of loop
float f; // f is only visible inside of loop
//…

for (int j = 0; j < 100; j++) {


// variable j can only be accessed inside the for-loop brackets
}
}

Swarnalatha G.L, Assistant Professor, CSE dept, SSIT 48


Arduino programming language reference (contd.)
• static: The static keyword is used to create variables that are visible to only one function. However
unlike local variables that get created and destroyed every time a function is called, static variables
persist beyond the function call, preserving their data between function calls. Variables declared as
static will only be created and initialized the first time a function is called.
Example code
void setup() {
Serial.begin(9600);
}
void loop() {
aFunction();
delay(1000);
}
void aFunction() {
static int counter = 0; /* static variable retains value in between function calls, but no other functions can
change its value*/
Counter++;
Serial.print(“Counter: “);
Serial.println(counter);
}
Swarnalatha G.L, Assistant Professor, CSE dept, SSIT 49
Arduino programming language reference (contd.)
In the example, the counter variable is declared as static within the aFunction() function. Each time
aFunction() is called, the counter value increments, and the incremented value is printed to the serial
monitor. This demonstrates that counter variable retains its value between function calls, unlike a
regular local variable.
volatile: volatile is a keyword known as a variable qualifier, it is usually used before the datatype of a
variable. Declaring a variable volatile is a directive to the compiler that the variable’s value may be
changed by something outside the normal program execution flow, such as an interrupt or external
hardware. Specifically, it directs the compiler to load the variable from RAM and not from a storage
register, which is a temporary memory location where program variables are stored and manipulated.
Example code: Toggling an LED based on an interrupt pin change
#define led 13
volatile byte state = LOW;
void setup() {
pinMode(led, OUTPUT);
attachInterrupt(digitalPinToInterrupt(2), blink, CHANGE);
} Swarnalatha G.L, Assistant Professor, CSE dept, SSIT 50
Arduino programming language reference (contd.)
void loop() {
digitalWrite(led, state);
delay(100); // delay for visual effect
}
void blink() {
state = !state; // toggle the LED state
}
In this example, state is declared volatile because it is modified within the blink interrupt service
routine (ISR), which executes whenever the interrupt pin changes state. The setup function attaches
an interrupt to digital pin 2, and when this pin changes, the blink function is called. Inside blink, the
value of state is toggled (flipped between LOW and HIGH). The loop function reads the value of
state (from memory) and uses it to control the LED's state. Without the volatile keyword, the
compiler might optimize the code by loading state into a register and using the cached value, which
could lead to incorrect behavior.
Swarnalatha G.L, Assistant Professor, CSE dept, SSIT 51

You might also like