Writing Code

Built In Functions

double transform(double input, double inputMin, double inputMax, double outputMin, double outputMax);

transform(int input, int inputMin, int inputMax, int outputMin, int outputMax);

transformLong(long input, long inputMin, long inputMax, long outputMin, long outputMax);

double clamp(double value, double min, double max);

long clamp(long value, long min, long max);

int clamp(int value, int min, int max);

embrioDebug(string input); // Writes the text to the message log in the Embrio UI.

double abs(double input);

int abs(int input);

double constrain(double value, double min, double max);

int constrain(int value, int min, int max);

double map(double value, double fromLow, double fromHigh, double toLow, double toHigh);

int map(int value, int fromLow, int fromHigh, int toLow, int toHigh);

double max(double x, double y);

int max(int x, int y);

double min(double x, double y);

int min(int x, int y);

double pow(double baseValue, double exponent);

int pow(int baseValue, int exponent);

double sq(double input);

int sq(int input)

double sqrt(double input);

int sqrt(int input);

double cos(double input);

double sin(double input);

double tan(double input);

bool isAlpha(char character);

bool isAlpha(string character);

bool isAlphaNumeric(char character);

bool isAlphaNumeric(string character);

bool isAscii(char input);

bool isDigit(char character);

bool isHexadecimalDigit(char character);

bool isLowerCase(char character);

bool isPrintable(char character);

bool isPunct(char character);

bool isSpace(char character);

bool isUpperCase(char character);

bool isWhitespace(char character);

int random(int max);

int random(int min, int max);

void randomSeed(int seed);

void delay(int milliseconds); // Does nothing in C#, but won't break compile

void delay(long milliseconds); // Does nothing in C#, but won't break compile

void delayMicroseconds(int microseconds); // Does nothing in C#, but won't break compile

long micros(); // In C# returns the number of ticks since the program started

long millis(); // In C# returns the number of milliseconds since the program started

Video Transcript

The code you write into a node gets used in two ways. First it is compiled as a C# class and executed at runtime in the Embrio UI or in an exported Embrio PC project. Second it is compiled in a generated INO file for upload to an Arduino. In both cases the code written in a node gets processed and transformed in ways needed for either context.

Most nodes have the red arrow icon activated which means that you write code once and that code is used both in the C# program and the Arduino program. Therefore the code your write needs to be valid for C++ and C#. This means that in general you must avoid using code that is specific to C# or Arduino and not available to the other.

To make this easier a number of Arduino functions have been implemented in the base C# node class so you can use a lot of Arduino specific functions in your nodes and they will work in the Embrio UI. For example Arduino has a function called abs() that gets an absolute value of a number. C# does not have this function by default, but you are able to use it in your nodes. Also some other useful shared functions have been added, like functions to transform a number from one range to another. A full list of such functions is available on the Embrio documentation page about writing code, which you should see under this video.

To access the value of an Input you write Input underscore and the inputs full name with no spaces. Same with an output except you write Output underscore and the full output name with no spaces. For input and output types that have a minimum and maximum value you type the accessor to the input and output then .Min or .Max. To fire an output trigger you write the accessor for the output trigger then add .Trigger(). Use the Declarations section for variables and functions that can be accessed anywhere in the node. Note that there are no global or static variables or functions, they are all scoped to an instance of a node. Use just the variable type or function return type, but do not use the C# scope decorators like “private” or “public”. For example to make an integer variable that can be used from any update code in the node write “int counter = 0;” To write a function that has no return value write void MyFunction();

Functions here do have access to the node’s inputs and outputs, but variable declarations outside of a function do not. For example if you want to have a variable record an Input value when the node starts you must declare the variable in the declarations section, then assign the value in the startup code block.