- It includes the basic syntax and built in functions for Arduino programmers
- Structure of the program
void setup(){
//Runs only once and initiates the variables
}
Void loop{
// Runs repeatedly and controls the board
}
- Functions
<return type> <name>(<params>) { ... } //syntax
int sum(int x,int y) {return x+y;} //example
//Return types
return x; // x must match return type
return; // For void return type
//Conditional statement
if<condition>{ <statement> }else{ <statement> }
//Loops
while (<condiiton) { <statement> }
do{ <statement> }(<condiiton)// condition is tested at the end of loop
for (initialization expression; condition for expression; update expression){
// body of the loop
}
//Loop control statement
break; // Exit a loop immediately
continue; // Go to next iteration
switch(var){
case 1:
<statement>
break;
case 2:
<statement>
break;
default:
<statement>
}
- Arithmetic
= //assignment
+ //add
- //subtract
* //multiply
/ //divide
% //modulo
- Comparison
== //equal to
!= //not equal to
< //less than
> //greater than
<= //less than or equal to
>= //greater than or equal to
- Boolean
&& // and
|| // or
! // not
- Bitwise
& //bitwise and
| //bitwise or
^ //bitwise xor
~ //bitwise not
<< //shift left
>> //shift right
- Compound
++ //increment
-- //decrement
+= //compound addition
-= //compound subtraction
*= //compound multiplication
/= //compound division
&= //compound bitwise and
|= //compound bitwise or
- Pointer access
& //reference: get a pointer
* //dereference: follow a pointer
sizeof() //unary operator used to compute the size of its operand (i.e) returns the amount of memory allocated
- Numeric Constant
HIGH | LOW
INPUT | OUTPUT
156 // decimal
0b00110011 // binary
0267 // octal
0xC8 // hexadecimal
10U // force unsigned
10L // force long
10UL // force unsigned long
10.0 // force floating point
10.2e6 // 10.2*10^6 = 1230000
- Data types
bool // true or false
char // -128 to 127, 's','@'
unsigned char // 0 to 255
byte // 0 to 255
int // -32768 to 32767
unsigned int // 0 to 65535
word // 0 to 65535
long // -2147483648 to 2147483647
unsigned long // 0 to 4294967295
float // -3.4028e+38 to 3.4028e+38
double // -3.4028e+38 to 3.4028e+38
void // no return value
- Array
int a[]={10,20,30};
int a[5]={1,2,3,4,5};
int a[5];
- Strings
char a[6] = {'H','e','l','l','o','\0'};
char a[6] = {'H','e','l','l','o'};
char a[6] = {"Hello"};
char a[] = {"Hello"};
- Qualifiers
static // preserves the value even when they are out of their scope
volatile // in RAM (nice for ISR)--> to tell the compiler, that the value may change at any time.
const //read-only
PROGMEM //in flash memory
- Digital I/O
//Digital I/O pins: 0-13
pinMode(pin,[INPUT, OUTPUT, INPUT_PULLUP])
int digitalRead(pin)
digitalWrite(pin, [HIGH, LOW])
- Analog I/O
//Analog I/O pins: A0-A5
pinMode(pin,[INPUT, OUTPUT, INPUT_PULLUP])
int analogRead(pin)
analogWrite(pin, value) // PWM Out pins: 3 5 6 9 10 11
analogReference([DEFAULT, INTERNAL, EXTERNAL])
- Advanced I/O
tone(pin, freq_Hz)
tone(pin, freq_Hz, duration_ms)
noTone(pin)
shiftOut(dataPin, clockPin,[MSBFIRST, LSBFIRST], value)
unsigned long pulseIn(pin,[HIGH, LOW])
- Time
unsigned long millis() // Overflows at 50 days
unsigned long micros() // Overflows at 70 minutes
delay(msec)
delayMicroseconds(usec)
- Math
min(x, y)
max(x, y)
abs(x)
sqrt(x)
pow(base, exponent)
constrain(x, minval, maxval)
map(val, fromL, fromH, toL, toH)
// Trignometric functions
sin(rad)
cos(rad)
tan(rad)
- Random number
randomSeed(seed) // long or int
long random(max) // 0 to max-1
long random(min, max)
- Bits and Bytes
lowByte(x)
highByte(x)
bitRead(x, bitn)
bitWrite(x, bitn, bit)
bitSet(x, bitn)
bitClear(x, bitn)
bit(bitn) // bitn: 0=LSB 7=MSB
- Type conversions
char(<value>)
byte(<value>)
int(<value>)
word(<value>)
long(<value>)
float(<value>)
- External Interrupts
attachInterrupt(interrupt, function,[LOW, CHANGE, RISING, FALLING])
detachInterrupt(interrupt)
interrupts()
noInterrupts()
- Serial.
//communication with PC or via RX/TX
begin([300, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 115200])
end()
int available() // #bytes available
int read() // -1 if none available
int peek() // Read w/o removing
flush()
print(data)
println(data)
write(byte)
write(char * string)
write(byte * data, size)
SerialEvent() // Called if data is ready
- EEPROM.h
// Access non-volatile memory
byte read(addr)
write(addr, byte)
EEPROM[index] // Access as array
- Servo.h
// Control servo motors
attach(pin, [min_uS, max_uS])
write(angle) // 0 to 180
writeMicroseconds(uS) // 1000-2000; 1500 is midpoint
int read() // 0 to 180
bool attached()
detach()
- SoftwareSerial.h
//Communication on any pin
SoftwareSerial(rxPin, txPin)
begin(long speed) // Up to 115200
listen() // Only 1 can listen
isListening() // at a time.
read, peek, print, println, write // Equivalent to Serial library
- Wire.h
//I²C communication
begin() // Join a master
begin(addr) // Join a slave @ addr
requestFrom(address, count)
beginTransmission(addr) // Step 1
send(byte) // Step 2
send(char * string)
send(byte * data, size)
endTransmission() // Step 3
int available() // Number of bytes available
byte receive() // Return next byte
onReceive(handler)
onRequest(handler)