Structure 0b10110110 Binary number (leading zero b)
#include <library.h> Arduino Programming 0100 Octal number (leading zero)
void setup() 0x8C Hex number (leading zero x)
void loop() Crib Sheet V1 100U forced unsigned int
Control structures 100L forces long int
if (a>10) { } else { } 100UL forces unsigned long int
switch (var) { 100.0 forces floating point
case 1: 1.1e2 equivalent to 110
break; Data Types
case 2; #define CONST_NAME 42 // define constants throughout code
break; bool 0,1,false,true
default: char, byte, int, unsigned int, long, unsigned long, float
} char S1[15];
for (int a=0; a<=100;a++){ } char S2[8]={’a’,’r’,’d’,’u’,’i’,’n’,’o’,’\0’};
while (a<10){ } Arrays
do { } while (a<10); int a[6] = {1,2,3,4,5,6}; // 1D array
continue; jump to next iteration in do/for/while loop int b[2];
break; exit a do/for/while loop b[0]=1; b[1]=2;
goto avoid using, program flow is hard to follow int c[2][6] = { {1,2,3, 4, 5, 6},
Operators {7,8,9,10,11,12} }; // 2D array
= + - * / > >= < <= Qualifiers
a%b modulo, returns remainder when int a divided by int b static // persist between function calls
== equal to != not equal to volatile // use with variables that change in ISR
&& and || or ! not const // constant (read-only)
Bitwise Operators Functions variables defined in a function are local to
& bitwise and | bitwise or the function and do not retain their value on
^ bitwise xor ~ bitwise not subsequent function calls unless they are
<< bitshift left >> bitshift right declared as static
Compound Operators eg. if define the following variables in setup():
a++; increment, a=a+1; int e[4]={1,2,3,4}; int total;
a--; decrement, a=a-1; total = add_elements(e,4);
a+=b; compound addition, a=a+b; // eg. “add_elements” function that returns an integer
a-=b; compound subtraction, a=a-b; int add_elements(int num[COLS],int col)
a*=b; compound multiplication, a=a*b; {
a/=b; compound division, a=a/b; int sum=0;
a&=b; compound bitwise and, a=a&b; // your code goes here
a|=b; compound bitwise or, a=a|b; return sum; // returns the sum of the 1D array elements
Pointers }
int *ptr; int myvar; int myval; void used in function declarations that do not return data
myvar = 100; Digital I/O
ptr = &myvar; // take the address of myvar pinMode(pin,[INPUT,OUTPUT,INPUT_PULLUP]);
myval = *ptr; // take value available at ptr digitalWrite(pin,value);
Constants int digitalRead(pin)
HIGH, LOW Analog I/O
true, false int analogRead(pin)
100 Decimal number analogWrite(pin,value); // PWM, value=duty cycle (0-255)
Buzzer within the ISR:
tone(pin,freq_Hz,duration_ms); current_time = millis();
Port Manipulation (fast) if(current_time-lastButtonDetected > debounce_interval) {
DDRy = Bxxxxxxxx; // Data Direction Register lastButtonDetected = current_time;
// y=D: D0-D7, y=B: D8-D13 // your code goes here
// x=1: OUTPUT, x=0: INPUT }
// LSB is lowest pin number (D0 or D8) Timer/Counter where n=0,1,2 on Arduino UNO,
DDRD |= B00111100; // set D2 to D5 as OUTPUT, leaving Timer0 used by millis(), Timer2 is 8-bit, Timer1 is 16-bit
// other pins alone OC0A=D6, OC0B=D5, OC1A=D9, OC1B=D10, OC2A=D11, OC2B=D3
PORTy = Bxxxxxxxx; // set pin values for register y=D or B TCNTn, OCRnA, OCRnB
// x=1: HIGH, x=0: LOW TCCRnA and TCCRnB contain:
// LSB is lowest pin number (D0 or D8) WGMnx, CSnx, x=0,1,2
Time COMnAx, COMnBx, FOCnA, FOCnB, x=0,1
unsigned long millis() For Timer2:
unsigned long micros() TCCR2A: WGM20=0 WGM21=1 COM2B0=4 COM2B1=5 COM2A0=6
delay(ms) COM2A1=7
delayMicroseconds(us) TCCR2B: CS20=0 CS21=1 CS22=2 WGM22=3 FOC2B=6
FOC2A=7
Math
min(a,b) max(a,b) abs(a) constrain(a,minval,maxval)
TIMSKn contains: TOIEn, OCIEnA, OCIEnB
map(val,fromL,fromH,toL,toH)
pow(base,exponent) sq(a) sqrt(a) For Timer2:
sin(rad) cos(rad) tan(rad) TIMSK2: TOIE2=0 OCIE2A=1 OCIE2B=2
ISR([TIMERn_COMPA_vect,TIMERn_COMPB_vect,TIMERn_OVF_vect]) {
Random Numbers
// your code goes here
randomSeed(seed) // long or int
}
eg. randomSeed(analogRead(A0));
enable interrupts with sei();
long random(max) // number from 0 to max-1
disable interrupts with cli();
long random(min,max) // number from min to max-1
Serial (built-in library)
Bits and Bytes
Serial.begin([300,1200,2400,4800,9600,
lowByte() highByte()
14400,19200,28800,38400,57600,115200]);
bitRead(a,bitn) bitWrite(a,bitn,bit)
Serial.end();
bitSet(a,bitn) bitClear(a,bitn)
int Serial.available()
bit(bitn) // 0 = LSB, 7 = MSB
int Serial.read()
Serial.flush();
Interrupts Serial.print(“Hello World!”);
attachInterrupt(digitalPinToInterrupt(interrupt_Pin), Serial.print(a,[DEC,BIN,HEX,OCT]);
function,[LOW,CHANGE,RISING,FALLING]); // external Serial.println();
// interrupt_Pin is D2 or D3 on Serial.write();
// Arduino UNO set as INPUT Servo (built-in library)
noInterrupts(); // disable interrupts, can use: cli(); Servo myservo;
Interrupts(); // enable interrupts, can use: sei(); myservo.attach(pin,[min_uS,max_uS]);
Debouncing myservo.write(angle); // 0-180
// eg. with the following declared global variables: myservo.writeMicroseconds(uS); // 1000-2000, 1500 midpoint
volatile unsigned long current_time; myservo.read();
volatile unsigned long lastButtonDetected; myservo.attached(); // boolean
const int debounce_interval; myservo.detach();