Arduino Cheat Sheet

Structure & flow:

Basic Structure:

void setup() {
  // Code that runs once at the beginning
}

void loop() {
  // Code that runs repeatedly
}

Comments:

// Single-line comment
/* Multi-line comment */

Control Structures:

if (x < 5) { ... } else { ... }
while (x < 5) { ... }
for (int i = 0; i < 10; i++) { ... }
break;     // Exit a loop immediately
continue;  // Go to next iteration
switch (var) {
  case 1:
    ...
    break;
  case 2:
    ...
    break;
  default:
    ...
}
return x;  // x must match
return typereturn;    // For void return type

Built-in Functions:

Digital I/O - pins 0-13 A0-A5:

pinMode(pin, OUTPUT) //Sets pin as output
pinMode(pin, INPUT) //Sets pin as input
pinMode(pin, INPUT_PULLUP) //Sets pin as input with res
digitalWrite(pin, HIGH) //Sets pin to HIGH (5V)
digitalWrite(pin, LOW) //Sets pin to LOW (0V)
digitalRead(pin)// Reads the value of a digital pin

Analog I/O - pins A0-A5:

analogRead(pin) //Reads the analog value from a pin (0-1023)
analogWrite(pin, value) //Writes an analog value (PWM) to a pin (0-255)
analogReference({DEFAULT|INTERNAL|EXTERNAL}) //Specifies the reference voltage to use
       //DEFAULT: Default reference, usually 5V on 5V boards or 3.3V on 3.3V boards.
       //INTERNAL: Built-in reference, typically 1.1V or 2.56V depending on the microcontroller model.
       //EXTERNAL: Uses a voltage applied to the AREF pin as the reference.

PWM Out - pins 3 5 6 9 10 11:

  • analogWrite(pin, value) Writes an analog value (PWM) to a pin (0-255)

Advanced I/O:

tone(pin, freq_Hz, [duration_msec])
noTone(pin)
shiftOut(dataPin, clockPin,  {MSBFIRST|LSBFIRST}, value)
shiftIn(dataPin, clockPin,  {MSBFIRST|LSBFIRST})
unsigned long pulseIn(pin,  {HIGH|LOW}, [timeout_usec])

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)
sin(rad)    cos(rad)    tan(rad)
sqrt(x)     pow(base, exponent)
constrain(x, minval, maxval)
map(val, fromL, fromH, toL, toH)

Random Numbers:

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(val)       byte(val)
int(val)        word(val)
long(val)       float(val)

External Interrupts:

attachInterrupt(interrupt, func, {LOW|CHANGE|RISING|FALLING})
detachInterrupt(interrupt)
interrupts()
noInterrupts()

Common Functions:

delay(milliseconds) //Pauses for a specified time
millis() //Returns the number of milliseconds since the program started
Serial.begin(speed) //Starts serial communication
Serial.print(data) //Prints data to the serial monitor
Serial.println(data) //Prints data and a newline to the serial monitor

Variables, Arrays, and Data:

Data Types (Kiểu sữ liệu):

  • bool true | false
  • char -128 - 127, 'a' '$' etc.
  • unsigned char 0 - 255
  • byte 0 - 255
  • int -32768 - 32767
  • unsigned int 0 - 65535
  • word 0 - 65535
  • long -2147483648 - 2147483647
  • unsigned long 0 - 4294967295
  • float -3.4028e+38 - 3.4028e+38
  • double currently same as float
  • void return type: no return value

Strings (Chuỗi):

char str1[8] =  {'A','r','d','u','i','n','o','\0'};  // Includes \0 null termination
char str2[8] =  {'A','r','d','u','i','n','o'};  // Compiler adds null termination
char str3[] = "Arduino";
char str4[8] = "Arduino";

Numeric Constants (Hằng số):

  • 123 decimal
  • 0b01111011 binary
  • 0173 octal - base 8
  • 0x7B hexadecimal - base 16
  • 123U force unsigned
  • 123L force long
  • 123UL force unsigned long
  • 123.0 force floating point
  • 1.23e6 1.23*10^6 = 1230000

Qualifiers:

  • static: persists between calls
  • volatile: in RAM (nice for ISR)
  • const: read-only
  • PROGMEM: in flash

Arrays (Mảng):

byte myPins[] = {2, 4, 8, 3, 6};
int myInts[6];      // Array of 6 ints
myInts[0] = 42;  // Assigning first                
                          // index of myInts
myInts[6] = 12;  // ERROR! Indexes
                          // are 0 though 5

Operators (Toán tử):

General Operators:

=   assignment
+   add         -   subtract
*   multiply    /   divide
%   modulo
==  equal to    !=  not equal to
<   less than   >   greater than
<=  less than or equal to
>=  greater than or equal to
&&  and         ||  or
!   not

Compound Operators:

++  increment
--  decrement
+=  compound addition
-=  compound subtraction
*=  compound multiplication
/=  compound division
&=  compound bitwise and
|=  compound bitwise or

Bitwise Operators:

&   bitwise and    |   bitwise or
^   bitwise xor    ~   bitwise not
<<  shift left     >>  shift right

Pointer Access:

&  reference: get a pointer
*  dereference: follow a pointer

Nhận xét

Mới hơn Cũ hơn