Sunteți pe pagina 1din 9

Arduino programming reference

by ​Luciano Rabassa

STRUCTURE

Sketch
void setup() ​{
//only run once,
//after each powerup or reset
}

void loop() ​{
​//loops consecutively
}

Control Structure

break;​ ​(is used to exit from a for, while or do…​while loop, and to exit from a switch case)
continue;​ ​(skips the rest of the current iteration of a loop (for, while, or do…​while)
do​{​...​}​while​(​x < 5​)​;
for​ ​(​int i = 0​;​ i < 10​;​ i++​){​...​}
goto​ ​label ​(Transfers program flow to a labeled point in the program)
if​(​x < 5​){​...​}​else​{​...​}
return;​ ​(Terminate a function and return a value. eg: return value; )
while​(​x < 5​){​...​}
switch​...​case
switch (var) {
case label1:
// statements
break;
case label2:
// statements
break;
default:
// statements
}

1
With hyperlinks in colour words
Arduino programming reference
by ​Luciano Rabassa

Constants, Data Types, Formatters

Constants
true | false
HIGH | LOW​ ​(5v , 0v)
INPUT | OUTPUT | INPUT_PULLUP
LED_BUILTIN​ ​(digital pin 13)
Floating Point Constants​ ​(10.0 Evaluates to:10
​2.34E5 Evaluates to:2.34 * 10^5 Also to:234000
​67e-12 Evaluates to:67.0 * 10^-12 Also to:0.000000000067)

Integer Constants​ ​(10 (decimal) eg:123;


2 (binary) eg:B1111011;
8 (octal) eg:0173;
16 (hexadecimal) eg:0x7B)

U & L formatters
'u'​ or ​'U'​ (Example: 33u)
'l'​ or ​'L'​ (Example: 100000L)
'ul'​ or ​'UL'​ (Example: 32767ul)

Data Types
void
bool​ ​ (0, 1, true, false)
boolean​ ​(0, 1, true, false)
char​ ​(eg: ‘A’ , -128 to 127)
int​ ​(-32,768 to 32,767)
long​ ​(-2,147,483,648 to 2,147,483,647)
unsigned char​ (0 to 255)
byte​ (0 to 255)
unsigned int​ (0 to 65535)
word​ ​(0 to 65535)
short​ (-32,768 to 32,767)
unsigned long​ (0 to 4,294,967,295)
float​ (-3.4028235E+38 to 3.4028235E+38)
double​ (Same as float. On the Arduino Due, doubles have 8-byte (64 bit) precision)

String
String​ ​ (char Str1[15];
char Str2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'}; without null termination can cause error!
char Str3[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o', '\0'};
char Str4[ ] = "arduino";)

2
With hyperlinks in colour words
Arduino programming reference
by ​Luciano Rabassa

Operators

Arithmetic Operators
%​ ​(remainder)
*​ ​(multiplication)
+​ ​(addition)
-​ ​(subtraction)
/​ ​(division)
=​ ​(assignment operator)

Comparison Operators
!=​ (not equal to)
<​ (less than)
<=​ (less than or equal to)
==​ (equal to)
>​ (greater than)
>=​ (greater than or equal to)

Boolean Operators
!​ (logical not)
&&​ (logical and)
||​ (logical or)

Pointer Access Operators


&​ (reference operator)
*​ (dereference operator)

Bitwise Operators
&​ (bitwise and)
<<​ ​(bitshift left)
>>​ ​ (bitshift right)
^​ (bitwise xor)
|​ (bitwise or)
~​ (bitwise not)

Compound Operators
&=​ (compound bitwise and)
*=​ (compound multiplication)
++​ (increment)
+=​ (compound addition)
--​ (decrement)
-=​ (compound subtraction)
/=​ (compound division)
^=​ (compound bitwise xor)
|=​ (compound bitwise or)

3
With hyperlinks in colour words
Arduino programming reference
by ​Luciano Rabassa

Variables, Array, Utilities

Array
int myInts[6];​ (array of six integers)
int myPins[] = {2, 4, 8, 3, 6};
int mySensVals[6] = {2, 4, -8, 3, 2};
char message[6] = "hello";
int myArray[10]={9,3,2,4,3,2,7,8,9,11};
myArray[9] ​(contains 11)
myArray[10] ​(is invalid and contains random information (other memory address) )

Variable Scope & Qualifiers


const​ ​ ​(making a variable "read-only")
scope
static​ ​ (persist beyond the function call)
volatile​ (load the variable from RAM)

Utilities
PROGMEM​ ​ (Store data in flash)
sizeof()​ (returns the number of bytes in a variable type, or the number of bytes occupied by
an array)

4
With hyperlinks in colour words
Arduino programming reference
by ​Luciano Rabassa

FUNCTIONS

Digital I/O
digitalRead(pin)
digitalWrite(pin, value)
pinMode(pin, mode)​ (INPUT, OUTPUT, or INPUT_PULLUP)

Analog I/O
analogRead(pin)
analogReference(type)
analogWrite(pin, value)

Zero, Due & MKR Family


analogReadResolution(bits)
analogWriteResolution(bits)

Advanced I/O
noTone(pin)
pulseIn(pin, value)​ ​or ​(pin, value, timeout)
pulseInLong(pin, value)​ ​or ​(pin, value, timeout)
shiftIn()​ ​(syntax byte incoming = shiftIn(dataPin, clockPin, bitOrder) )
shiftOut(dataPin, clockPin, bitOrder, value)
tone(pin, frequency)​ or ​(pin, frequency, duration)

Time
delay(ms)
delayMicroseconds(us)
micros()​ ​ (syntax time = micros() )
millis()​ (syntax time = millis() )

Math
abs(x)​ ​Calculates the absolute value of a number.
constrain(x, a, b)​ ​Constrains a number to be within a range.
map(value, fromLow, fromHigh, toLow, toHigh)​ ​Re-maps from one to another.
max(x, y)​ ​Calculates the maximum of two numbers.
min(x, y)​ ​Calculates the minimum of two numbers.
pow(base, exponent)​ ​Calculates the value of a number raised to a power.
sq(x)​ ​Calculates the square of a number: the number multiplied by itself.
sqrt(x)​ Calculates the square root of a number.

Trigonometry
cos(rad)​ ​Calculates the cosine of an angle (in radians). The result will be between -1 and 1.
sin(rad)​ ​Calculates the sine of an angle (in radians). The result will be between -1 and 1.
tan(rad)​ Calculates the tangent of an angle (in radians).The result will be between negative
infinity and infinity.

5
With hyperlinks in colour words
Arduino programming reference
by ​Luciano Rabassa

FUNCTIONS
Characters
isAlpha(thisChar)​ ​Analyse if a char is alpha.
isAlphaNumeric(thisChar)​ ​ Analyse if a char is alphanumeric.
isAscii(thisChar)​ ​ Analyse if a char is Ascii.
isControl(thisChar)​ ​ ​Analyse if a char is a control character.
isDigit(thisChar)​ ​Analyse if a char is a digit (that is a number).
isGraph(thisChar)​ ​Analyse if a char is printable with some content.
isHexadecimalDigit(thisChar)​ Analyse if a char is an hexadecimal digit (A-F, 0-9).
isLowerCase(thisChar)​ ​Analyse if a char is lower case.
isPrintable(thisChar)​ ​Analyse if a char is printable, even a blank space.
isPunct(thisChar)​ ​Analyse if a char is punctuation.
isSpace(thisChar)​ ​Analyse if a char is the space character.
isUpperCase(thisChar)​ ​Analyse if a char is upper case.
isWhitespace(thisChar)​ Analyse if a char is a white space, that is space, formfeed ('\f'),
newline ('\n'), carriage return ('\r'), horizontal tab ('\t'), and vertical tab ('\v')).

Random Numbers
random(max)​ ​The random function generates pseudo-random numbers.
randomSeed()​ Initializes the pseudo-random number generator, causing it to start at an
arbitrary point in its random sequence.This sequence, while very long, and random, is
always the same.

Bits and Bytes


bit(n)​ ​Computes the value of the specified bit.
bitClear(x, n)​ ​Clears (writes a 0 to) a bit of a numeric variable.
bitRead(x, n)​ ​Reads a bit of a number.
bitSet(x, n)​ ​Sets (writes a 1 to) a bit of a numeric variable.
bitWrite(x, n, b)​ ​Writes a bit of a numeric variable.
highByte(x)​ ​Extracts the high-order (leftmost) byte of a word.
lowByte(x)​ Extracts the low-order (rightmost) byte of a variable.

External Interrupts
attachInterrupt()
detachInterrupt()​ Turns off the given interrupt.

Interrupts
interrupts()
noInterrupts()

Communication
Serial
stream
USB
Keyboard
Mouse

6
With hyperlinks in colour words
Arduino programming reference
by ​Luciano Rabassa

Functions

Serial
Serial.begin(speed)
Serial.begin(speed, config)​ Sets the data rate in bits per second.
Serial.end()​ Disables serial communication.
Serial.available()​ Get the number of bytes.
Serial.read()​ Reads incoming serial data.
Serial.peek()​ Returns the next byte
Serial.flush()​ Waits for the transmission of outgoing.
Serial.print(val)​ ​or​ ​(val, format)​ Prints data to the serial
Serial.println(val)​ ​Prints data to the serial
Serial.write(val)​ or ​(str)​ or ​(buf, len)​. Writes binary data to the
serial port.

7
With hyperlinks in colour words
Arduino programming reference
by ​Luciano Rabassa

Libraries
EEPROM
EEPROM.read(address)​ ​Reads a byte from the EEPROM.
EEPROM.write(address)​ Write a byte to the EEPROM.

Servo
servo.attach(pin)​ Attach the Servo variable to a pin.
servo.attach(pin,min,max)
servo.write(angle)​ Writes a value to the servo.
servo.writeMicroseconds(us)​ Writes a value in microseconds.
servo.read()​ Read the current angle.
servo.attached()​ Check whether the Servo variable is attached to a
pin.
servo.detach()​ Detach the Servo variable from its pin.

SoftwareSerial(rxPin, txPin, inverse_logic)


rxPin​ ​(​the pin on which to receive serial data)
txPin​ (the pin on which to transmit serial data)
inverse_logic​ (is used to invert the sense of incoming bits)
SoftwareSerial.begin(speed)​ ​Sets the speed,the baud rate (long)
SoftwareSerial.read()​ ​Return a character that was received on the
RX pin.
SoftwareSerial.print(data)​ Prints data to the transmit pin.
SoftwareSerial.println(data)

Wire
Wire.begin()​ ​Initiate the Wire library and join the I2C bus as a
master or slave.
Wire.begin(address)​ ​address: the 7-bit slave address (optional); if
not specified, join the bus as a master.
Wire.requestFrom()​ Used by the master to request bytes from a
slave device

8
With hyperlinks in colour words
Arduino programming reference
by ​Luciano Rabassa

Wire.requestFrom(address, quantity)
Wire.requestFrom(address, quantity, stop)
Wire.beginTransmission(address)​ Begin a transmission to the
I2C slave.
Wire.endTransmission()​ Ends a transmission to a slave device.
Wire.available()​ Returns the number of bytes.
Wire.onReceive(handler)​ Registers a function to be called when a
slave device receives a transmission from a master.
Wire.onRequest(handler)​ Register a function to be called when a
master requests data from this slave device.

9
With hyperlinks in colour words