Skip to content

Peripherals: Arduino IO functions

Stefan Lenz edited this page Mar 25, 2023 · 3 revisions

Currently implemented functions and commads

AREAD(PIN): analog read from PIN (see the sections about pins and constants)

AWRITE PIN: analog write to PIN

DREAD(PIN): digital read from PIN

DWRITE PIN: digital write to PIN

PINM PIN, MODE: change pin mode

DELAY M: delay the program for M milliseconds.

MILLIS(DIV): return the milliseconds from program start on divided by DIV

PLAY PIN, FREQUENCY, DURATION: play a tone on a pin (not available on DUE and ESP32)

PUSLE(PIN, SIGNAL, TIMEOUT): reads a pulse from PIN and returns the duration in 10 microsecond units.

As a command PULSE PIN, DURATION, VALUE, REPETITIONS, INTERVALL writes pulses to a digital pin.

MAP(VALUE, LOW1, HIGH2, LOW2, HIGH2): Arduino map, convert an integer VALUE from the rage LOW1-HIGH1 to the range LOW2-HIGH2 using 32 bit arithmetic. Useful for 16 bit integer systems.

PINM, pins and constants

PINM is a direct copy of the Arduino function pinMode(). The constants D0 to Dn, A0 to An and the modes OUTPUT, INPUT, INPUT_PULLUP are not implemented in BASIC to save keyword space. The respective integer values have to be used in BASIC programs. AZERO is a predefined constant for the lowest analog pin A0. You would use AREZO + n to find the analog pin An.

Digital pins are numbered from 0-n followed by the analog pins which start at at n+1. For an Arduino UNO with 13 digital pins the first analog pin A0 is 14. For a Mega with 50 digital pins the first analog pin is 54.

The constants for the pin modes are INPUT=0, OUTPUT=1, INPUT_PULLUP=2.

LOW=0 and HIGH=1 which is not surprising.

DELAY and MILLIS

Delay is using the Arduino delay functions directly handing through the BASIC variable value. This restricts delays to a maximum of 32 seconds. MILLIS allows specifying a divisor adjusting the time base. MILLIS(1) is the time in milliseconds since program start. MILLIS(1000) is the time in seconds. Any integer can be used as a divisor.

PULSEIN

PULSEIN is a wrapper of the pulseIn() function of the Arduino libraries. The time parameter for the timeout is specified in milliseconds (unlike the Arduino function which uses microseconds). The duration is returned in units of 10 microseconds, limiting the resolution of ultrasonic pulses to 0,3cm. The maximum pulse length that can be measured with PULSEIN is 3 seconds. Longer pulses need to be implemented using MILLIS.

MAP

The MAP function is mainly useful on Integer BASIC variants.

U=MAP(V, A, B, C, D)

converts the value V in the range between A and B linearly to a value U in the range C and D. Long arithmetic is used for this to overcome the restriction of 16 bit integers. Also on floating point systems all numbers are cast to long.

PLAY

PLAY is the Arduino tone() and noTone() function. The first argument is the pin the tone is played from, the second the frequency and the third is optionally the duration. If the third argument is 0, the noTone() command is executed.

On ESP32 FabGL systems, PLAY triggers the build in sound generator.