Embrio lets you create the logic and structure of your programs with it's powerful "agent based" architecture, but also lets you write code when needed. This tutorial shows how to interface with just about any hardware component by writing Arduino code directly into a Custom Arduino Node.

Also check out the Custom Arduino Node documentation page.

And the growing library of pre-written nodes.

View discussion page on forum

In a previous tutorial we saw how to read and write values to pins on the Arduino using Analog and Digital input and output nodes. This covers the basics, but a lot of components you’ll want to use in your projects require more code to work with. This tutorial goes over the Custom Arduino Node, which lets you write Arduino code right into the node.

For just about any component you want to use, there’s a code example for getting it to work in an Arduino program. This is great, but when you have more than one component in your project, your code quickly becomes hard to manage. Embrio solves this problem by keeping the code for each component encapsulated by its own Custom Arduino Node which so it can be easily integrated into a larger program without adding much complexity.

There is a library of built in nodes that is maintained on the web site which you can browse and search. When you open Embrio, it gets the current list of built in nodes from the web site so that they can be added directly in the program. To add a pre-filled node, right click and go to the Input/Output, Custom Arduino Node Library menu and find the component you want. All you have to do now is set your pin values.

Nodes in Embrio serialize to XML, so it’s easy to copy and share them. If you select a node, copy it, and then paste into note pad, you can see that the node is defined by XML which can be easily shared, and is how the library is built up on the web site. If you paste XML that defines a node into the Embrio software, a copy of the node is created.

When there is no pre-built node for what you want to do, the best process is to get the example working in the Arduino IDE so you know your device is wired correctly and the code works. Then copy and paste the code into the corresponding section on the custom Arduino node.

The first 3 code sections are pretty self explanatory. The first box is where you put any Include statements that your code needs. If you need to use code in an external code file that isn’t part of the Arduino library, you need to attach the files to your project using the Include Files form. Get to it from the View menu, or in the right click menu on a Custom Arduino Node. Simply select the files that need to be included, and they will be copied to the compile folder when your program is compiled.

Next is the declaration section, this is where you define any variables. These variables are available to just the node they are entered on, not to other nodes in the project. Next is the setup code section. This is the code that runs in Arduino’s setup function.

You can add inputs and outputs to your node by either right clicking on the node and selecting what to add, or by using the buttons on top of the node. Like all inputs and outputs, you can rename them by middle clicking on them, or selecting Rename from the right click men, and change the order they appear by selecting move up or move down from the right click menu.

Now that your inputs and outputs are added you need to set up where and when your code executes. Add a code block by clicking on the button, a node can have multiple code blocks. There are 4 possible events for the code block to run. First is on every update. In the bottom right of the screen the node is on is a refresh rate that sets how often the nodes on the screen update, by default it’s 32 times per second.

Next is on Input Change. When this option is selected a check box list shows up with all of the inputs on your node that have a change event. Select one or more input, and when any of their values change, the code will fire. Next is the startup event. This code runs just once when the program starts up.

Finally is the trigger event. This one is only available when you have input triggers defined on your node. The second drop down box lets you pick which trigger to attach to. This code block will fire when its input trigger fires.

Once you have a code event set up, you can copy and paste the Arduino code into it. The last step is to interface the values from the Arduino code with the input and outputs defined on the node. To access an input or output in code, you type the word Input or Output, starting with a capital letter, then an underscore, then the name of the input or output without any spaces. So to set a numeric output value named Activation you’d type Output_Activation. To do things like set text and transform numbers and activate triggers, you use the built in functions, which are available on the help page. For a simple example, to convert a long value from an Arduino Analog Read function you have to convert from the range of 0 through 1023 to the range of 0 through 1. To do this you’d use the transform function. The first parameter is the value to transform, the next two define the minimum and maximum values of the input range, and the last two define the minimum and maximum of the output range.

Custom Arduino nodes can be both an input or output node, and like other input and output nodes in Embrio, they require a connection refresh when they are edited. Once you are done with your code, either connect to the Arduino, or refresh the active connection, and you can interact with your hardware components in real time.

View discussion page on forum