UB90 Reference: the Arduino C and C++ the simulator runs
The Uno Breadboard Simulator (UB90, version 0.9.0) runs a subset of the Arduino language: C and C++ (classes and templates) with the Arduino functions, in one file or several. This page lists every command it runs, grouped as in the Arduino language reference. A sketch that uses only these commands builds and runs in the simulator and on a real Uno. See the UB90 Help page for how to use the simulator. Back to the simulator.
Reference - Table of Contents
Sketch
void setup() { } | runs once when the Uno starts |
void loop() { } | runs again and again after setup() |
int add(int a, int b) { } | your own functions, with return |
#define LED 13 | a name for a value (no parameters) |
#include <Arduino.h> | the Arduino core, built in (also "Arduino.h", <avr/io.h>, <stdint.h>, <stdbool.h>, <math.h>); any other library name is an error |
#include "ace.h" | one of the sketch's own files (see Files and the preprocessor) |
// and /* */ | comments |
Control
if (x) { } else { } | choose |
for (int i = 0; i < 5; i++) { } | count |
while (x) { } | repeat while true |
do { } while (x); | repeat at least once |
switch (x) { case 1: break; default: } | choose by value |
break; continue; return x; | leave a loop, skip, return |
Types
bool boolean | true or false |
char byte | 8 bits |
int short | 16 bits, as on the Uno |
long | 32 bits |
unsigned int, unsigned long | no negative values |
uint8_t int8_t uint16_t int16_t | fixed sizes |
uint32_t int32_t word size_t | fixed sizes |
float double | decimal numbers |
const static volatile | qualifiers |
int a[5] = {1, 2}; | arrays |
Constants
HIGH LOW | pin levels, 1 and 0 |
INPUT OUTPUT INPUT_PULLUP | pin modes |
LED_BUILTIN | pin 13 |
A0 to A5 | analog pins (14 to 19) |
true false | bool values |
DEC HEX BIN OCT | number formats for Serial.print |
PI HALF_PI TWO_PI | circle constants |
DEG_TO_RAD RAD_TO_DEG | angle conversion |
Operators
= + - * / % | assign and arithmetic |
== != < > <= >= | compare |
&& || ! | logic |
& | ^ ~ << >> | bits |
++ -- += -= *= /= %= | compound |
&= |= ^= <<= >>= | compound bits |
x ? a : b | choose a value (numbers only) |
Digital I/O
pinMode(pin, mode) | INPUT, OUTPUT, or INPUT_PULLUP |
digitalWrite(pin, value) | HIGH or LOW |
digitalRead(pin) | returns HIGH or LOW |
Analog I/O
analogRead(pin) | A0 to A5, returns 0 to 1023 |
analogWrite(pin, value) | PWM 0 to 255 (pins 3, 5, 6, 9, 10, 11) |
Time
delay(ms) | wait in milliseconds |
delayMicroseconds(us) | wait in microseconds |
millis() | milliseconds since the start |
micros() | microseconds since the start |
Serial
Serial.begin(9600) | start the Serial Monitor |
Serial.print(x) Serial.print(x, HEX) | print a value or text |
Serial.println(x) | print, then a new line |
Serial.write(b) | send one byte |
Serial.available() Serial.read() | return 0 and -1 (no input) |
Serial.println("a:" ...) | name:value pairs draw the Plotter |
Math
map(x, inLow, inHigh, outLow, outHigh) | rescale a value |
constrain(x, low, high) | keep x in a range |
min(a, b) max(a, b) abs(x) | smaller, larger, size |
sq(x) sqrt(x) pow(x, y) | square, square root, power |
sin(x) cos(x) tan(x) | angles in radians |
Random
random(max) random(min, max) | a whole number below max |
randomSeed(n) | start a new random sequence |
Bits and bytes
bit(n) _BV(n) | the value of bit n |
bitRead(x, n) | bit n of x |
lowByte(x) highByte(x) | the low or high 8 bits |
Sound
tone(pin, frequency) | a square wave on the buzzer |
tone(pin, frequency, ms) | for a time |
noTone(pin) | stop the sound |
Files and the preprocessor
Name.ino, x.h, x.cpp | one sketch, up to 16 files in one folder (the file tabs above the editor) |
#include "x.h" | reads the file x.h of the sketch; "lib/x.h" finds x.h too |
#ifndef X_H #define X_H #endif | include guards; also #ifdef, #else, #undef |
#pragma once | a file is read once |
\ at the end of a line | the line goes on (long strings and defines) |
| Limits | 16 files, 8 include levels, 200,000 characters in all |
C++: classes, templates, namespaces
struct P { int x = 1; int sum() const; }; | structs and classes: fields with starting values, methods, const methods |
public: private: protected: | access labels |
Counter(int s) : n(s) { } | constructors with an initializer list; Counter c(41); |
static int sq(int v); | static members; call as M::sq(7) |
this->k, obj.field, obj.method() | members of an object |
template <uint8_t P, uint16_t T = 45> | class and function templates with type (typename T) and number parameters and defaults |
Pin<10>::high(); times<3>(5) | template arguments in a call |
namespace ns { } using namespace ns; ns::f() | namespaces (names must differ between namespaces) |
enum, enum class, typedef, using T = ...; | named values and type names |
constexpr, inline, static_assert(c, "text"); | a false static_assert stops the sketch with its text |
void two(int &a) | reference parameters; default values int n = 3 |
F("text"), while (!Serial) | text in flash; the port is always ready |
| Limits | 64 class instances, 256 objects |
Registers
DDRB PORTB PINB | pins 8 to 13 |
DDRC PORTC PINC | pins A0 to A5 |
DDRD PORTD PIND | pins 0 to 7 |
PORTB |= _BV(PB5); | bit names PB5, DDB5, PINB5 are 0 to 7 |
TCCR0A TCCR1A TCCR2A (and B) | timers: a COM bit (COM0A1, COM0B1, ...) puts its pin on PWM, as analogWrite() does |
OCR0A OCR0B OCR1A OCR1B OCR2A OCR2B | the PWM duty of pins 6, 5, 9, 10, 11, 3 |
ADMUX = _BV(REFS0) | ch; | chooses the analog input A0 to A5 |
ADCSRA |= _BV(ADSC); then ADC | one conversion, read as ADC (or ADCL and ADCH), as analogRead() does |
Not supported
String, char arrays of text | text only inside Serial.print |
inheritance, virtual, operators, pointers | a plain message names each; use a reference (int &x) for a pointer |
new, delete, the STL | not in this subset |
libraries (#include) | Servo, Wire, SPI, and others |
attachInterrupt(), Serial input | no interrupts or typing in |
About this reference
- An int is 16 bits, as on the Uno: it holds −32768 to 32767, and a larger value wraps around when it is stored. Use long for larger numbers.
- Text is allowed only inside Serial.print, Serial.println, and Serial.write.
- Build checks the spelling of the code (syntax). Upload runs the compiler checks: setup() and loop() exist, every name is declared, and each function gets the right number of values. An error names its file and line.
- Timing is close to the Uno but not exact.
- The full Arduino language reference is at docs.arduino.cc.