UB32 Reference: the Arduino C the simulator runs

The Uno Breadboard Simulator (UB32, version 0.3.2) runs a subset of the Arduino language: C with the Arduino functions. 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 UB32 Help page for how to use the simulator. Back to the simulator.

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 13a name for a value (no parameters)
#include <...>ignored; no libraries are loaded
// 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 booleantrue or false
char byte8 bits
int short16 bits, as on the Uno
long32 bits
unsigned int, unsigned longno negative values
uint8_t int8_t uint16_t int16_tfixed sizes
uint32_t int32_t word size_tfixed sizes
float doubledecimal numbers
const static volatilequalifiers
int a[5] = {1, 2};arrays

Constants

HIGH LOWpin levels, 1 and 0
INPUT OUTPUT INPUT_PULLUPpin modes
LED_BUILTINpin 13
A0 to A5analog pins (14 to 19)
true falsebool values
DEC HEX BIN OCTnumber formats for Serial.print
PI HALF_PI TWO_PIcircle constants
DEG_TO_RAD RAD_TO_DEGangle conversion

Operators

= + - * / %assign and arithmetic
== != < > <= >=compare
&& || !logic
& | ^ ~ << >>bits
++ -- += -= *= /= %=compound
&= |= ^= <<= >>=compound bits
x ? a : bchoose 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

Port registers

DDRB PORTB PINBpins 8 to 13
DDRC PORTC PINCpins A0 to A5
DDRD PORTD PINDpins 0 to 7
PORTB |= _BV(PB5);bit names PB5, DDB5, PINB5 are 0 to 7

Not supported

String, char arrays of texttext only inside Serial.print
classes, structs, templates, pointersnot in this subset
libraries (#include)Servo, Wire, SPI, and others
attachInterrupt(), Serial inputno 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.
  • Timing is close to the Uno but not exact.
  • The full Arduino language reference is at docs.arduino.cc.