This project exemplifies a Bela workflow that encompasses running Arduino-like code alongside a Bela program.
The Arduino code runs on a dedicated thread, which can exchange messages with Bela's audio thread. The example shows running a Pure Data patch alongside the Arduino program and how the two can communicate.
render.cpp
makes the basic API calls to get everything setup. In ino.cpp
you can write Arduino-like code (a lot of code can just be copy/pasted in from your .ino
sketches). You can run a Pd patch at the same time and use pdSendMessage()
and pdReceiveMessage()
to communicate between the two. This allows to easily intermix procedural and dataflow languages, using each where it's best suited. This example also shows a prototype of the control panel that will eventually be integrated in the IDE: this is a p5 GUI that allows to monitor inputs and outputs and even override them (e.g.: to simulate connected potentiometers on the input, or to generate test signals on the outputs). There is no requirement to use all three components (Arduino, Pd and control panel) at once, e.g.: you can use Arduino with C++ code or the control panel without Arduino.
- Support for Arduino libraries is limited, but will be expanded. In principle, most existing Arduino libraries based on Wire or or Adafruit_I2CDevice should work. Let us know specific things that don't work and we can look into adding them.
- timing of anything running in
loop()
is less accurate than it would be on Arduino, for two reasons:- it runs on a core shared with other processes (especially on the first generation Belas).
- I/O is actually managed by the Bela core, so that writes will be flushed to the pin when the next
render()
callback runs and any value you read refers to what was read when the lastrender()
callback ran. In most cases this is not a big deal, but in a couple of cases you'll need special approaches:- write-then-read (e.g.: set a pin high, then read an input that relies on that pin actually being high) operations require to add a delay after the write to make sure the output value is actually written to the pin before we attempt to read back
- writing to the same output multiple times may result in some outputs being dropped unless you add an appropriate delay after each write. In the common case of bitbanging a clock on one pin while shifting data on another, consider using
shiftOut()
instead.
- as on some Bela boards
analogWrite()
actually meansanalogWrite()
, we add apwmWrite()
that bitbangs a PWM on a Bela digital channel (and is somewhat closer to theanalogWrite()
behaviour of the Arduino Uno, though with a lower bitclock) - API and whatnot is subject to change
This also runs on older Belas and requires Bela's dev
branch with the addition of these two libraries: