Nguồn: https://www.truongcongly.com/2024/01/arduino-cheat-sheet.html
void setup() { // Code that runs once at the beginning } void loop() { // Code that runs repeatedly }
// Single-line comment /* Multi-line comment */
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
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
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.
analogWrite(pin, value) //Writes an analog value (PWM) to a pin (0-255)
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])
unsigned long millis() // Overflows at 50 days unsigned long micros() // Overflows at 70 minutes delay(msec) delayMicroseconds(usec)
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)
randomSeed(seed) // long or int long random(max) // 0 to max-1 long random(min, max)
lowByte(x) highByte(x) bitRead(x, bitn) bitWrite(x, bitn, bit) bitSet(x, bitn) bitClear(x, bitn)bit(bitn) // bitn: 0=LSB 7=MSB
char(val) byte(val) int(val) word(val) long(val) float(val)
attachInterrupt(interrupt, func, {LOW|CHANGE|RISING|FALLING}) detachInterrupt(interrupt) interrupts() noInterrupts()
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
bool
true | falsechar
-128 - 127, 'a' '$' etc.unsigned char
0 - 255byte
0 - 255int
-32768 - 32767unsigned int
0 - 65535word
0 - 65535long
-2147483648 - 2147483647unsigned long
0 - 4294967295float
-3.4028e+38 - 3.4028e+38double
currently same as floatvoid
return type: no return valuechar 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";
123
decimal0b01111011
binary0173
octal - base 80x7B
hexadecimal - base 16123U
force unsigned123L
force long123UL
force unsigned long123.0
force floating point1.23e6
1.23*10^6 = 1230000byte 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
= 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
++ increment -- decrement += compound addition -= compound subtraction *= compound multiplication /= compound division &= compound bitwise and |= compound bitwise or
& bitwise and | bitwise or ^ bitwise xor ~ bitwise not << shift left >> shift right
& reference: get a pointer * dereference: follow a pointer