General guidelines

General guidelines

Callbacks:

  • Do not use Delay(); inside callback. Callbacks are meant for quick execution.

  • While script is inside callback - main code is paused until callback is executed.

  • Callback position in the code should be at start before all other functions and main program.

Wrong use of callback example
new var1 = 20; new var2 = 10; forward swap(&x, &y); forward public callback (event); public callback (event) { switch(event) { case RS232: { swap(var1, var2); Delay(100); // <--- Wrong use of delay since this is inside callback } } return 0; } main() { while(1) { Delay(10); } } swap(&x, &y) { new temp; temp = x; // save the x value to temp variable x = y; // put y into x y = temp; // put temp into y Delay(100); <--- Wrong use of delay since function named 'swap' goes into callback }

while(1):

inside while(1) always leave at least 10ms Delay(); at the end.

While(1) delay example
new val; main() { while(1) { val = Get_val(Sensor_SignalIN4); Delay(10); } }

Functions:

  • For creation of new functions use 'forward' for function prototype (check example code line no. 5)

To create new function - it's not needed to specify input variable data type

Creation of a new function example
new num1 = 10; new num2 = 2; new num3; forward newFunction(input1, input2); //first define new function by writing 'forward' first, then goes your function name following with brackets. //Inside brackets define how many variables your function needs //in this case it's only two variables, but it can have more or even no input variables main() { while(1) { if(Get_val(Sensor_SignalIN4) == 0) //if sensor signal is low/0 { num3 = newFunction(div1, div2); //call our previously defined function and set num3 value to return value of a function } Delay(100); } } newFunction(input1, input2) //here we'll define what this function does { new result; //new variable used only in this function result = input1 / input2; //do something with input data return result; //function returns result }

In case you want input variables to change their values - define your function variables using syntax like so (this is called 'call by reference'):

new num1 = 10; new num2 = 2; forward swap(&x, &y); main() { while(1) { if(Get_val(Sensor_SignalIN4) == 0) //if sensor signal is low/0 { swap(num1, num2); //call our previously defined function and set swap input values (since that's how our defined function works) } Delay(100); } } swap(&x, &y) { new temp; temp = x; // save the x value to temp variable x = y; // put y into x y = temp; // put temp into y }

Do not create arguments and functions with same name. This way compiler might return errors during compilation.

new num1 = 10; new num2 = 2; new num3; forward dataTransfer(input1, input2); forward newFunction(newOutput, newInput); main() { newFunction(dataTransfer, 5); // <-- wrong use of function. Same name cannot be defined for multiple things while(1) { Delay(100); } } DataTransfer(input1, input2) { }

Sensors:

in order to get sensor value in floating point (This applies only for non binary sensors), syntax like so must be used: 

Get sensor value in floating point example
new Float:dist; main() { dist = Float:get_val(Sensor_Distance); while(1) { Delay(100); } }

Variables:

  • All new global variables should be declared before functions and callback, all new local variables should be declared at the beginning of a function or callback.
    NOTE: If the variable is declared in the middle of a function or code, the compiler will not show the error and the program may work, but it is likely to affect the stability of the program.

new variable2; // global variables new variable1; forward function(); forward public callback (event); public callback (event) { new variable5; // local variable switch(event) { case RS232: { // do something... } } return 0; } main() { new variable3; // local variable // do something... while(1) { // do something... Delay(100); } } function() { new variable4; // local variable // do something... }

For loop:

 

  • Unlike the c language new variables should not be declared in expression 1 position or more than one variable used.
    NOTE: If the variable is declared or used more than once, the compiler will not show the error and the program may work, but it is likely to affect the stability of the program.