Compiler hints

Be aware that some EV3 Basic programs that run perfectly in 'PC mode' (launched within Small Basic) will not compile correctly to the brick and therefore cannot be run in 'Brick mode' (launched from within the brick's menus). Sometimes the reason is obvious: of course brick mode will not work if the program makes use of Small Basic's graphic window or text window or tries to play sounds on the PC's speaker. Sometimes the reason is less obvious, caused by programming practices that are compatible with Small Basic but not with the compiler that tries to convert the Small Basic files into the RBF format that the brick understands. Here are some tips to minimise the risk of problems occurring when you try to compile your programs so that they can be run in brick mode:

   Sub Move

     Motor.Move("BC",20,angle,"True")

   EndSub

   angle=360

   Move()

So, for compatibility with the compiler, you must assign a value to the variable before you use it, like this:

   angle=360

   Sub Move

     Motor.Move("BC",20,angle,"True")

   EndSub

   Move()

Here's another example of a program that works in Small Basic but which won't compile to the brick (a silly example because you would use For rather than While in this case):

   While n<10

     Speaker.Tone(100,n*200,500)

     Speaker.Wait()

     n=n+1

   EndWhile

By the way, can you guess why the above code will begin by playing a 250Hz tone twice?

The fourth tip above is NOT followed by this program below which is intended to recognise standard colors and then display the corresponding numerical color code and the name of the actual color:

Sensor.SetMode(3,2)

Make an array containing the colors, matched to the array index values

Colors="0=UNKNOWN;1=BLACK;2=BLUE;3=GREEN;4=YELLOW;5=RED;6=WHITE;7=BROWN"

While "True"

  LCD.StopUpdate()

  LCD.Clear()

  code=Sensor.ReadRawValue(3, 0)

  LCD.Text(1,33,40, 2, "Color "+ Code)

  LCD.Text(1,33,75, 2, Colors[Code])

  LCD.Update()

  Program.Delay(200)

EndWhile

The above program runs fine in PC mode but will not compile for download to the brick. Why? Because  the method used to make the array called 'Colors' is not acceptable to the compiler. So the solution, which CAN be compiled to the brick and run in brick mode, is this:

Sensor.SetMode(3,2)

'Make an array containing the colors, matched to the array index values

Colors[0]="UNKNOWN"

Colors[1]="BLACK"

Colors[2]="BLUE"

Colors[3]="GREEN"

Colors[4]="YELLOW"

Colors[5]="RED"

Colors[6]="WHITE"

Colors[7]="BROWN"

While "True"

    LCD.StopUpdate()

    LCD.Clear()

    code=Sensor.ReadRawValue(3, 0)

    LCD.Text(1,33,40, 2, "Color "+ Code)

    LCD.Text(1,33,75, 2, Colors[Code])

    LCD.Update()

    Program.Delay(200)

EndWhile

Further details, as explained by the developer:

The original behaviour of Small Basic has been mimicked in the EV3-compiler as faithfully as possible, but there are some things that simply can not be done with the EV3 byte code interpreter.

Variables are typed

While Small Basic has just one datatype which is a Unicode string into which all possible values (even arrays) are mangled, EV3 Basic has 4 distinct datatypes for variables:

Every variable has one of these types (which is determined by its first use in the program code) and cannot be used to store anything else.

Operators and functions work on compile-type types

Arithmetic or logic operators need to be provided with either number or text parameters and deliver a result of also a defined type (for example, the "<" needs two numbers and delivers a text). One exception is the "=" which will perform either a numerical or a textual comparison, depending on the compile-time type of the operands. Another exception is the "+" that will do a numerical add when provided with two numbers, but will do a text concatenation if one of the operands is text. Similar requirements are there for function parameters, which also have a defined compile-time type. To make things easier, an automatic conversion from number to text is done if a parameter needs a text (but not the other way round).

Limitation for text

Any text variable can only hold up to 251 characters and not the full Unicode range is supported, but only the codes 1 - 255 (they are just plain C-style char strings). The same holds for all elements of text arrays.