Arduino Cheat Sheet

ADVERTISEMENT

ATMega168 ATMega328 ATmega1280
Structure
Arduino Cheat Sheet V.02c
Flash (2k for
void setup() void loop()
boobtloader)
16kB
32kB
128kB
Mostly taken from the extended reference:
SRAM
1kB
2kB
8kB
EEPROM
512B
1kB
4kB
Control Structures
Gavin Smith – Robots and Dinosaurs, The Sydney Hackspace
if (x<5){ } else { }
Duemilanove/
Nano/ Pro/
switch (myvar) {
ProMini
Mega
case 1:
14 + 6 analog
External Interrupts
Constants
Qualifiers
break;
# of IO
(Nano has 14+8)
54 + 16 analog
attachInterrupt(interrupt, function,
HIGH | LOW
static // persists between calls
0 - RX1 1 - TX1
case 2:
[LOW,CHANGE,RISING,FALLING])
19 - RX2 18 - TX2
INPUT | OUTPUT
volatile
// use RAM (nice for ISR)
break;
0 - RX
17 - RX3 16 - TX3
detachInterrupt(interrupt)
true | false
const // make read-only
Serial Pins
1 - TX
15 - RX4 14 - TX4
default:
interrupts()
143 // Decimal number
PROGMEM // use flash
2 - (Int 0)
2,3,21,20,19,18
}
noInterrupts()
0173 // Octal number
Ext Interrupts
3 - (Int 1)
(IRQ0- IRQ5)
for (int i=0; i <= 255; i++){ }
5,6 - Timer 0
0b11011111 //Binary
Digital I/O
Libraries:
while (x<5){ }
9,10 - Timer 1
0x7B // Hex number
PWM pins
3,11 - Timer 2
0-13
pinMode(pin, [INPUT,OUTPUT])
do { } while (x<5);
Serial.
10 - SS
53 - SS
7U // Force unsigned
digitalWrite(pin, value)
continue; //Go to next in do/for/while loop
11 - MOSI
51 - MOSI
begin([300, 1200, 2400, 4800, 9600,
10L // Force long
int digitalRead(pin)
12 - MISO
50 - MISO
return x; // Or ‘return;’ for voids.
14400, 19200, 28800, 38400, 57600,
SPI
13 - SCK
52 - SCK
15UL // Force long unsigned
//Write High to inputs to use pull-up res
goto
// considered harmful :-)
Analog4 - SDA
20 - SDA
115200])
10.0 // Forces floating point
I2C
Analog5 - SCK
21 - SCL
end()
2.4e5 // 240000
Analog I/O
int available()
Further Syntax
analogReference([DEFAULT,INTERNA
int read()
// (single line comment)
Data Types
L,EXTERNAL])
flush()
/* (multi-line comment) */
void
int analogRead(pin) //Call twice if
print()
#define DOZEN 12 //Not baker’s!
boolean
(0, 1, false, true)
switching pins from high Z source.
println()
#include <avr/pgmspace.h>
analogWrite(pin, value) // PWM
char (e.g. ‘a’ -128 to 127)
write()
unsigned char (0 to 255)
byte (0 to 255)
EEPROM (#include <EEPROM.h>)
Advanced I/O
General Operators
int
(-32,768 to 32,767)
byte read(intAddr)
tone(pin, freqhz)
= (assignment operator)
unsigned int (0 to 65535)
write(intAddr,myByte)
tone(pin, freqhz ,duration_ms)
+ (addition)
- (subtraction)
word (0 to 65535)
noTone(pin)
Servo (#include <Servo.h>)
* (multiplication) / (division)
long
(-2,147,483,648 to
shiftOut(dataPin, clockPin,
attach(pin , [min_uS, max_uS])
% (modulo)
2,147,483,647)
[MSBFIRST,LSBFIRST], value)
write(angle) // 0-180
== (equal to)
!= (not equal to)
unsigned long (0 to 4,294,967,295)
unsigned long pulseIn(pin, [HIGH,LOW])
writeMicroseconds(uS) //1000-2000,
< (less than)
> (greater than)
(-3.4028235E+38 to
float
1500 is midpoint
<= (less than or equal to)
3.4028235E+38)
Time
read() // 0-180
>= (greater than or equal to)
double (currently same as float)
unsigned long millis() // 50 days overflow.
attached() //Returns boolean
&& (and)
|| (or)
! (not)
sizeof(myint) // returns 2 bytes
unsigned long micros() // 70 min overflow
detach()
delay(ms)
From
Arduino.CC
SoftwareSerial(RxPin,TxPin)
Pointer Access
delayMicroseconds(us)
Strings
& reference operator
// #include<SoftwareSerial.h>
char S1[15];
* dereference operator
begin(longSpeed) // up to 9600
Math
char S2[8]={'a','r','d','u','i','n','o'};
char read() // blocks till data
min(x, y)
max(x, y)
abs(x)
char S3[8]={'a','r','d','u','i','n','o','\0'};
print(myData) or println(myData)
constrain(x, minval, maxval )
//Included \0 null termination
Bitwise Operators
char S4[ ] = "arduino";
map(val, fromL, fromH, toL, toH)
Wire (#include <Wire.h>) // For I2C
& (bitwise and) | (bitwise or)
pow(base, exponent) sqrt(x)
char S5[8] = "arduino";
begin()
// Join as master
^ (bitwise xor) ~ (bitwise not)
char S6[15] = "arduino";
sin(rad)
cos(rad)
tan(rad)
begin(addr) // Join as slave @ addr
<< (bitshift left) >> (bitshift right)
requestFrom(address, count)
Random Numbers
Arrays
beginTransmission(addr) // Step 1
randomSeed(seed) // Long or int
int myInts[6];
send(mybyte)
// Step 2
Compound Operators
long random(max)
int myPins[] = {2, 4, 8, 3, 6};
++ (increment)
-- (decrement)
send(char * mystring)
long random(min, max)
int mySensVals[6] = {2, 4, -8, 3, 2};
+= (compound addition)
send(byte * data, size)
-= (compound subtraction)
endTransmission()
// Step 3
Bits and Bytes
*= (compound multiplication)
byte available() // Num of bytes
Conversion
lowByte()
highByte()
/= (compound division)
byte receive() //Return next byte
char()
byte()
bitRead(x,bitn) bitWrite(x,bitn,bit)
&= (compound bitwise and)
onReceive(handler)
int()
word()
bitSet(x,bitn)
bitClear(x,bitn)
Pics from under C.C. license
|= (compound bitwise or)
onRequest(handler)
long()
float()
bit(bitn) //bitn: 0-LSB 7-MSB

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go