built-in functions libraries - e2cre8 · 0x7 b h exa dec ima l - ba s e 1 6 123u f orc e u nsi gne...

1
by Bart Huyskens version: 08/01/2018 Serial - comm. with PC or via RX/TX begin(long speed) // Up to 115200 end() int available() // #bytes available int read() // -1 if none available int peek() // Read w/o removing flush() print(data) println(data) write(byte) write(char * string) write(byte * data, size) SerialEvent() // Called if data rdy EEPROM.h - access non-volatile memory byte read(addr) write(addr, byte) EEPROM[index] // Access as array Servo.h - control servo motors attach(pin, [min_uS, max_uS]) write(angle) // 0 to 180 writeMicroseconds(uS) // 1000-2000; 1500 is midpoint int read() // 0 to 180 bool attached() detach() SoftwareSerial.h - comm. on any pin SoftwareSerial(rxPin, txPin) begin(long speed) // Up to 115200 listen() // Only 1 can listen isListening() // at a time. read, peek, print, println, write // Equivalent to Serial library Wire.h - I²C communication begin() // Join a master begin(addr) // Join a slave @ addr requestFrom(address, count) beginTransmission(addr) // Step 1 send(byte) // Step 2 send(char * string) send(byte * data, size) endTransmission() // Step 3 int available() // #bytes available byte receive() // Get next byte onReceive(handler) onRequest(handler) Libraries - Original: Gavin Smith - www.e2cre8.be >> Brainbox AVR 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 type return; // For void return type Function Definitions <ret. type> <name>(<params>) { ... } e.g. int double(int x) {return x*2;} Basic Program Structure void setup() { // Runs once when sketch starts } void loop() { // Runs repeatedly } 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 Bitwise Operators & bitwise and | bitwise or ^ bitwise xor ~ bitwise not << shift left >> shift right Compound Operators ++ increment -- decrement += compound addition -= compound subtraction *= compound multiplication /= compound division &= compound bitwise and |= compound bitwise or Arduino Programming Cheat Sheet Primary source: Arduino Language Reference https://arduino.cc/en/Reference/ Structure & Flow Operators Type Conversions char(val) byte(val) int(val) word(val) long(val) float(val) Pin Input/Output Digital I/O - pins 0-13 A0-A5 pinMode(pin, [INPUT, OUTPUT, INPUT_PULLUP]) int digitalRead(pin) digitalWrite(pin, [HIGH, LOW]) Analog In - pins A0-A5 int analogRead(pin) analogReference( [DEFAULT, INTERNAL, EXTERNAL]) PWM Out - pins 3 5 6 9 10 11 analogWrite(pin, value) Advanced I/O tone(pin, freq_Hz) tone(pin, freq_Hz, duration_ms) noTone(pin) shiftOut(dataPin, clockPin, [MSBFIRST, LSBFIRST], value) unsigned long pulseIn(pin, [HIGH, LOW]) 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 External Interrupts attachInterrupt(interrupt, func, [LOW, CHANGE, RISING, FALLING]) detachInterrupt(interrupt) interrupts() noInterrupts() Built-in Functions Pointer Access & reference: get a pointer * dereference: follow a pointer Numeric Constants 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 Data Types boolean 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 i.e., no return value Strings 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"; Arrays int 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 Qualifiers static persists between calls volatile in RAM (nice for ISR) const read-only PROGMEM in flash Variables, Arrays, and Data source: h ps://github.com/liton/Arduino-Cheat-Sheet/ BBA WWW.E2CRE8.BE >> BBA L293D H-BRIDGE USB TXLED +5V OK D13 RESET D11 D12 SERVO BUZZER D7 B R A I N B O X A R D U I N O Vba (6Vmax) GND 9-16V DC 9-16V DC USB 5V FROM 5V SOURCE SELECT Vba-0.7V +5V MOSI/ 16 GND SCK/15 RESET MISO/14 ISP D4 GND D8 GND SDA/2 SCL/3 D10 D9 D5 D6 RX/0 TX/1 +5V GND RX/0 TX/1 A5/23 +5V GND A4/22 +5V GND A3/21 +5V GND A2/20 +5V GND A1/19 +5V GND A0/18 +5V GND V-POWER SOURCE SELECT RXLED/17 5V 3V3 GND ADAPTER Place only 1 jumper Place only 1 jumper 5V FROM ADAPTER 7-16V VBa JUMPER1 JUMPER2 +5V +5V +5V +5V ATMEL ATmega32U4 ARDUINO SHIELD CONNECTORS 23/A5 22/A4 21/A3 20/A2 19/A1 18/A0 Vin GND GND 5V 3.3V RESET NC 5V 11 12 13 GND AREF 2/SDA 3/SCL 0/RX 1/TX 2 3 4 5 6 7 8 9 10 23 A5 5V GND 22 A4 5V GND 21 A3 5V GND 20 A2 5V GND 19 A1 5V GND 18 A0 5V GND 5 6 9 10 PWM6 PWM9 PWM3 PWM4 D11 D12 SERVO D13 13 PWM7 11 12 5V 5V GND GND PWM1 PWM GND GND 5V 3V3 GND 8 4 3 2 1 0 SCL SDA TX RX BUZZER 7 +5V MOSI/16 GND SCK/15 RESET MISO/14 ISP 16 5V GND RESET MOSI 14 MISO 15 SCK INT0 INT1 INT2 INT3 +5V GND RX/0 TX/1 0 1 RX TX INT2 INT3 5V GND PF0 ADC0 PF1 ADC1 PF4 ADC4 PF5 ADC5 PF6 ADC6 PF7 ADC7 PC7 PB2 PB3 PB1 PD2 PD3 PD2 PD3 PD1 PD0 PB6 PB5 PD7 PC6 PD4 PB4 ADC8 ADC11 PE6 PB7 PD6 RXLED/17 17 PB0 4 power outputs 600mA/pin Can only be used as output! Voltage selectable with jumper 2 Mulple powering opons Refer to extra datasheet for details ARDUINO IDE ATMEGA32U4 PROCESSOR TXLED PD5 ! Absolute max per pin = 40mA Recommended = 20mA Absolute max 200mA for enre package GND 7-16V 2.1mm

Upload: others

Post on 28-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Built-in Functions Libraries - E2CRE8 · 0x7 B h exa dec ima l - ba s e 1 6 123U f orc e u nsi gne d 123L f orc e l ong 123UL fo rce un sig ned lo n g 123.0 fo rce fl oat ing po i

by Bart Huyskensversion: 08/01/2018

Serial - comm. with PC or via RX/TXbegin(long speed) // Up to 115200end()int available() // #bytes availableint read() // -1 if none availableint peek() // Read w/o removingflush()print(data) println(data)write(byte) write(char * string)write(byte * data, size)SerialEvent() // Called if data rdy

EEPROM.h - access non-volatile memorybyte read(addr)write(addr, byte)EEPROM[index] // Access as array

Servo.h - control servo motorsattach(pin, [min_uS, max_uS])write(angle) // 0 to 180writeMicroseconds(uS) // 1000-2000; 1500 is midpointint read() // 0 to 180bool attached()detach()

SoftwareSerial.h - comm. on any pinSoftwareSerial(rxPin, txPin) begin(long speed) // Up to 115200listen() // Only 1 can listenisListening() // at a time.read, peek, print, println, write // Equivalent to Serial library

Wire.h - I²C communicationbegin() // Join a masterbegin(addr) // Join a slave @ addrrequestFrom(address, count)beginTransmission(addr) // Step 1send(byte) // Step 2send(char * string)send(byte * data, size)endTransmission() // Step 3int available() // #bytes availablebyte receive() // Get next byteonReceive(handler)onRequest(handler)

Libraries

- Original: Gavin Smith - www.e2cre8.be >> Brainbox AVR

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

Function Definitions<ret. type> <name>(<params>) { ... }e.g. int double(int x) {return x*2;}

Basic Program Structurevoid setup() { // Runs once when sketch starts}void loop() { // Runs repeatedly}

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

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

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

Arduino Programming Cheat SheetPrimary source: Arduino Language Reference

https://arduino.cc/en/Reference/

Structure & Flow Operators

Type Conversionschar(val) byte(val)int(val) word(val)long(val) float(val)

Pin Input/OutputDigital I/O - pins 0-13 A0-A5 pinMode(pin, [INPUT, OUTPUT, INPUT_PULLUP]) int digitalRead(pin) digitalWrite(pin, [HIGH, LOW])

Analog In - pins A0-A5 int analogRead(pin) analogReference( [DEFAULT, INTERNAL, EXTERNAL])

PWM Out - pins 3 5 6 9 10 11 analogWrite(pin, value)

Advanced I/Otone(pin, freq_Hz)tone(pin, freq_Hz, duration_ms)noTone(pin)shiftOut(dataPin, clockPin, [MSBFIRST, LSBFIRST], value)unsigned long pulseIn(pin, [HIGH, LOW])

Timeunsigned long millis() // Overflows at 50 daysunsigned long micros() // Overflows at 70 minutesdelay(msec)delayMicroseconds(usec)

Mathmin(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 NumbersrandomSeed(seed) // long or intlong random(max) // 0 to max-1long random(min, max)

Bits and ByteslowByte(x) highByte(x)bitRead(x, bitn)bitWrite(x, bitn, bit)bitSet(x, bitn)bitClear(x, bitn)bit(bitn) // bitn: 0=LSB 7=MSB

External InterruptsattachInterrupt(interrupt, func, [LOW, CHANGE, RISING, FALLING])detachInterrupt(interrupt)interrupts()noInterrupts()

Built-in Functions

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

Numeric Constants123 decimal0b01111011 binary0173 octal - base 80x7B hexadecimal - base 16123U force unsigned123L force long123UL force unsigned long123.0 force floating point1.23e6 1.23*10^6 = 1230000

Data Typesboolean 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 i.e., no return value

Stringschar str1[8] = {'A','r','d','u','i','n','o','\0'}; // Includes \0 null terminationchar str2[8] = {'A','r','d','u','i','n','o'};

// Compiler adds null terminationchar str3[] = "Arduino";char str4[8] = "Arduino";

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

Qualifiersstatic persists between callsvolatile in RAM (nice for ISR)const read-onlyPROGMEM in flash

Variables, Arrays, and Data

source: h�ps://github.com/liffiton/Arduino-Cheat-Sheet/

BBA WWW.E2CRE8.BE >> BBA

L293DH-BRIDGE

USB

TXLED

+5V OK

D13

RESET

D11D12

SERVO

BUZZER

D7

BR

AIN

BO

X AR

DU

INO

Vbatt(6Vmax)

GND 9-16V DC 9-16V DCU

SB5V FRO

M

5V SOU

RCE SELECT

Vbatt-0.7V

+5V

MO

SI/16

GND

SCK/15

RESET

MISO

/14ISP

D4GN

DD8

GND

SDA/2SCL/3

D10D9

D5D6

RX/0TX/1

+5V

GND

RX/0

TX/1

A5/23+5V

GND

A4/22+5V

GND

A3/21+5V

GND

A2/20+5V

GND

A1/19+5V

GND

A0/18+5V

GND

V-POW

ER SOU

RCE SELECT

RXLED/17

5V3V3

GND

ADAPTER

Place only 1 jumper

Place only 1 jumper

5V FROM

ADAPTER7-16VVBatt

JUM

PER1

JUM

PER2

+5V

+5V

+5V

+5V

ATMEL

ATmega32U

4

ARDUINO

SHIELD CON

NECTO

RS

23/A522/A4

21/A320/A219/A118/A0

VinGND

GND5V

3.3VRESET

NC5V

1112

13GNDAREF2/SDA3/SCL

0/RX1/TX

23456

7

8910

23 A5

5V

GND

22 A4

5V

GND

21 A3

5V

GND

20 A2

5V

GND

19 A1

5V

GND

18 A0

5V

GND

5

6

9

10

PWM6

PWM9

PWM3

PWM4

D11 D12

SERVO

D13 13 PWM7

11 125V 5V

GND GND

PWM1

PWM

GND

GND

5V

3V3GND

8

4

3

21

0

SCL

SDATX

RX

BUZZER

7

+5V

MOSI/16

GND

SCK/15

RESET

MISO/14ISP

165V

GNDRESETMOSI

14MISO15SCK

INT0

INT1INT2

INT3

+5V

GND

RX/0

TX/101

RXTX INT2

INT3

5VGND

PF0 ADC0

PF1 ADC1

PF4 ADC4

PF5 ADC5

PF6 ADC6

PF7 ADC7

PC7PB2PB3PB1PD2

PD3

PD2

PD3PD1

PD0

PB6

PB5PD7

PC6

PD4

PB4

ADC8

ADC11

PE6

PB7 PD6

RXLED/1717PB0

4 power outputs 600mA/pin

Can only be used as output!

Voltage selectable with jumper 2

Multiple powering optionsRefer to extra datasheet for details ARDUINO

IDEATMEGA32U4

PROCESSOR

TXLEDPD5

! Absolute max per pin = 40mARecommended = 20mAAbsolute max 200mA for entire package

GND

7-16V 2.1mm