0% found this document useful (0 votes)
13 views

Arduino Cheat Sheet 1-En

Uploaded by

shiladey709
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Arduino Cheat Sheet 1-En

Uploaded by

shiladey709
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

ARDUINO PROGRAMMING CHEAT SHEET

SKETCH OPERATORS BUILT-IN FUNCTIONS


Basic Sketch Structure Arithmetic PIN INPUT/OUTPUT Math
void setup() { = assignment Digital I/O min(x, y); max(x, y);
// runs once after each powerup or reset + addition pinMode(pin, mode); abs(x); - Absolute value
} - subtraction mode - INPUT, OUTPUT, INPUT_PULLUP sin(rad); cos(rad); tan(rad);
* multiply / divide int digitalRead(pin); sqrt(x); pow(base, exponent);
void loop() { % modulo digitalWrite(pin, state); constrain(x, min, max);
// runs continuously state - HIGH, LOW map(val, fromL, fromH, toL, toH);
} Comparison
== equal to Analog I/O External Interrupts
Function Definitions != not equal to int analogRead(pin); attachInterrupt(interrupt, ISR,
<ret. type> <func. name>(<param. type> <param. name>){ … } i.e.: < less than analogReference(source); mode);
float circleCircumference(int radius) { return 3.14 * 2 * radius; } > greater than source - DEFAULT, INTERNAL, EXTERNAL mode - LOW, CHANGE, RISING,
void printGreeting(string name) { Serial.println(name); } <= less than or equal analogWrite(pin, value); //PWM FALLING
>= greater than or equal detachInterrupt(interrupt);
Advanced I/O interrupts();
Boolean tone(pin, freq_hz); noTone(pin); noInterrupts();
VARIABLES, ARRAYS, DATA TYPES && and tone(pin, freq_hz, duration_ms);
|| or byte shiftIn(dataPin, clkPin, order); Type Conversions
Data Types Strings
! Not shiftOut(dataPin, clkPin, order, val); char(val); byte(val);
bool/boolean true false char ex1[3] = {`H’,’i’,’\0’};
bitOrder - MSBFIRST, LSBFIRST int(val); word(val);
char -128 - 127 char ex2[3] = {H’,’i’};
Compound Operators unsigned long pulseIn(pin, state, long(val); float(val);
unsigned char 0 - 255 char ex3[] = „Hi”;
++ increment -— decrement timeout); //timeout parameter optional
byte 0 - 255 char ex4[3] = „Hi”;
+= addition -= subtraction pulseInLong //same as pulseIn Random Numbers
int -32768 - 32767 *= multiplicat. /= division randomSeed(seed);
unsigned int 0 - 65535 Qualifiers
Bits and Bytes long random(max); //min = 0
word 0 - 65535 static //persists between func. calls
Bitwise operators byte lowByte(x); byte highByte(x); long random(min, max);
long -2147483648 - 2147483647 volatile //in RAM (good for ISR)
& and byte bitRead(x, bitnumber);
unsigned long 0 - 4294967295 const //read-only
| or bitWrite(x, bitnumber, bit); Time
float -3.4028e+38 - 3.4028e+38 PROGMEM //stored in flash
^ xor bitSet(x, bitnumber); unsigned long millis(); //<50 days
double - same as float except Due ~ not bitClear(x, bitnumber); unsigned long micros(); //<70 mins
void - indicates no return value Numeric Constants
<< shift left bit(bitnumber); delay(miliseconds);
123 decimal
>> shift right delayMicroseconds(useconds);
Arrays 0b01111110 binary
int even[] = {2, 4, 6, 8}; 0123 octal - base 8
Compound bitwise operators
int pins[6]; 0xA2 hexadecimal
123U force unsigned
&= compound bitwise and ARDUINO LIBRARIES
pins[0] = 10; //indexing from 0 |= compound bitwise or
pins[6] = 7; //Common mistake - 123L force long Serial - communication via UART Wire.h - I2C communication
indexing from 0 to size - 1 !!! 123UL force unsigned long begin(long speed); begin(); //join a master
Pointer Access
123.0 force float end(); begin(addr); //join a slave
& reference: get a pointer
1.23e6 1.23*10^6 int available() //num. of bytes requestFrom(address, count);
* dereference: get a value
available setClock(clkFreq);
int read(); //-1 if none available beginTransmission(addr);
int peek(); //read without removing write(byte)
CONTROL STATEMENTS flush(); write(char* str);
if (x > 0) { … } else { … } print(data); println(data); write(byte* data, length);
switch (x) { write(byte); write(char* str); byte endTransmission();
write(byte* data, size); int available(); //no. of bytes
case 1: Author: Michał Zięba serialEvent(); byte read(); //get next byte
… Adapted from: Mark Liffiton
break; onReceive(handler);
case 2: EEPROM.h - non-volatile memory onRequest(handler);
www.przygodyzkodem.pl byte read(address);

break; GITHUB: przygodyzkodem write(address, byte); Servo.h - control servo motor
default: IG: przygodyzkodem put(addr, data); get(addr, data); attach(pin, min_us, max_us);
… FB: przygodyzkodem EEPROM[index]; //access as array write(angle); //0 to 180
break; writeMicroseconds(useconds);
} SoftwareSerial.h - UART on any pin //1000 - 2000; 1500 is midpoint
while (x < 10) { … } SoftwareSerial(rxPin, txPin); int read(); //0 to 180 angle
for (int i = 0; i < 10; i++) { … } bool listen(); //only 1 can listen bool attached();
do { … } while (x < 10); Attribution-ShareAlike 4.0 bool isListening(); detach();
break; //Exit loop/switch immediately International (CC BY-SA 4.0) begin, read, peek, print, println,
continue; //Go to next iteration start write, available //As in Serial lib.

You might also like