Obsah

Blocks

This page contains basic info about jBlocks commands in Blocks mode. More about jBlocks can be found on the Environment page.

Group Basic

Start

Block Start is the most important block in every program and it is called immediately after the program starts.

Never-ending loop

The Never-ending loop block is used in the main part of the program. It doesn't allow the program being quit.

Printing values to console

Blocks Print, Print variable and Print without linefeed are used to print string or variable into console. It is useful for debugging and watching program operations.

The parameter is string to be printed. Strings are „quoted“. When we want to print variable or an expression, we have to insert extra quites and join string and expression by +. The following example:

prints content of variable promenna.

Comment

Block Comment is useful to store information and short notes about the program. Comments don't have any influence to the program, the program ignores them.

Delay loop

Delay loop causes pausing the program for some time. Delay is set in milliseconds (1 s = 1000 ms).

Current time in milliseconds

The block stores current time in milliseconds into an integer variable. When used in console mode the time is counted since midnight 1. 1. 1970 (UTC). hen used in jBotBrain II. program the time is counted since the program starts. It is necessary to hanle overflow (after reaching value 2 147 483 647 the time raises from -2 147 483 648).

Group Variables

Variable is used to store some value. We can create variables, write and read them. The jBlocks environment has three date types:

Datetype Java equivalent Example Range
Integer int -5, 0, 42 -2 147 438 648 .. 2 147 438 647
Byte byte -5, 0, 'A' (65) -128 .. 127
Logical value boolean true, false
Floating point number float 0.00f, 42.207f 1,4×10-45 .. 3,4×1038

Operations with numbers

Operations with numbers can be applied to integer and floating point number variables. Floating point number can be converted to integer using so-called casting. Casting float to int trims the part after floating point.

Operation Explanation
+ Addition
- Substraction
* Multiplication
/ Division
% Modulo (division remainder)
++ Increment 1 (Operation a++ adds 1 to variable a)
Decrement 1
(int) Casting floating point expression to int
<< a >> Bit shifts – (a << b) shifts number „a“ b bits left, (a >> b) shifts number „a“ b bits right

Bit shifts examples:

Expression Result Before After
8 >> 1 16 10002 100002
5 << 2 20 1012 101002
6 >> 1 3 1102 112

Advanced operations with numbers - Mathematical functions

The Java Math class contains usful mathematical functions such as sine, square root or logarithm. jBlocks allows calling of standard Java functions, so that we can use them for instance in numerical expressions.

To compute the sqare root of 4, you can use:

There can also be variable or another function in brackets instead of numeric constant (4). The value in brackets is called parameter. If there are more parameters than one, they have to be separred by commas.

Math class function results usually have to be casted to (int) or (float) depending on chosen datetype.

In the Math class there are also two constants: Math.PI and Math.E. The first one contains π value, the second one value of Euler constant e (the base of natural logarithm). These constant also have to be casted to (float) or (int).

Usage of mathematical functions can be found on Examples page.

Function Explanation
abs(a) Returns the absolute value of a.
acos(a) Returns the arc cosine of angle a. Angle in radians can be in range 0-PI.
asin(a) Returns arc sine of angle a. Angle in radians can be in range ←PI/2; PI/2>.
atan(a) Returns arc tangent of angle a. Angle in radians can be in range ←PI/2; PI/2>.
ceil(a) Rounds value a up.
cos(a) Returns cosine of angle a (angle in radians).
exp(a) Returns ea, where e is the base of natural logarithm.
floor(a) Rounds value a down.
IEEEremainder(a, b) Computes remainder using IEEE 754 standard.
log(a) Returns natural logarithm (base e) of a.
max(a, b) Returns the smaller of two values a, b.
min(a, b) Returns the greater of two values a, b.
pow(a, b) Returns a raised to the power of b (ab).
random() Returns random number higher than or equal 0.0 and lesser than 1.0.
round(a) Rounds value a (returns the closest integer value).
sin(a) Returns sine of angle a (angle in radians).
sqrt(a) Vrátí square root of a.
tan(a) Returns tangent of angle a (angle in radians).
toDegrees(a) Converts angle a from radians to degrees.
toRadians(a) Converts angle a from degreees to radians.

Bitwise operations

Bitwise operations can be used with logical expressions or in conditions. Instead of true and false there are 1 and 0 values in tables for better readibility. They can be grouped using brackets as well as operations with numbers.

! Negation:

A !A
1 0
0 1

&& Bitwise AND:

A B A && B
1 1 1
1 0 0
0 1 0
0 0 0

|| Bitwise OR:

A B A || B
1 1 1
1 0 1
0 1 1
0 0 0

^^ Bitwise exclusive OR (XOR):

A B A ^^ B
1 1 0
1 0 1
0 1 1
0 0 0

Set variable

Integer
FLoating point number
Logical variable

Block Set variable stores value into variable. Variable name may contain characters a-z, A-Z and 0-9 and mustn't begin with number. All identifiers (variabe and function names) are unique in a programme so we can not use the same name for logical and integer variable. If we do so, an error mesage appears.

Array

Array is a group of variables with the same datetype (Integer or Byte in jBlocks). The Create array blocks creates new array with n items.

The Set (array) command is used to store value into array on a given index. The index is a number in [brackets]. Indexing starts at 0. To read array value (e. g. in the Print command), use syntax array[0].

Array of bytes are useful when dealing with the Bluetooth interface.

Group Console

This grou is available only in the Console mode (program for PC).

Reads an integer from console and saves it into variable.

Reads float from console and saves it into variable.

Reads logical value from console and saves it into variable. The logical value is true if the number read from console equals to 1.

Clear the console. This function is available only for built-in console in jBlocks environment because the clear screen command is not platform independent.

Group Conditions

If

Commands placed inside the If block are run only id condition in If's parameter equals to true.

In conditions, following comparsion operators can be used:

Operator Explanation
== Equality (equals to)
!= Equality (does not equal to)
< > Relational (lesser than, greater than) - for numerical expressions
<= >= Relational (lesser than ir equals to, greater than or equals to) - for numerical expressions

Else

The Else block must be placed directly after the If block. Commands placed in this block are run if the condition in the If block equals to false.

Cycles

The Repeat n times loop runs commands laced inside this block n-times. It is usable for running repeating operations, such as browsing array's content.

The While loop runs commands placed inside the block until the considion in parameter equals to true. Placing true into the condition results into a never-ending loop.

If you want to „jump“ from the cycle before the condition equals to false use the Break block.

Group Functions

A function is a group of commands, sub-routine, with its unique name. Function can be called from anz part of the programme or called as a background operation. They are useful to manage repeating routines such as turning all LEDs off, printing values etc. and to keep the programme clear.

The jBlocks environment does not use parameters as programming languages do. Instead of them just use variables since all variables defined in the programme are global, so that variable defined in the main loop can be accessed from any function.

Function

The Function block creates new function. For the function name there are same rules as for variable names - characters a-z, A-Z and 0-9 are allowed.

Call function

The Call function block runs selected function and waits until it finishes.

The Call function non-blocking runs selected function as a background operation (thread) so that the main programme does not have to wait until the function finishes. This is useful for LED blinking, reading buttons etc.

Timer

The Timer block calls selected function in given interval (milliseconds). This is useful for periodically repeating actions.

Return

The Return blocks quits the functions and returns to the main programme.

Group LEDs and buttons

This group is avaliable only on the jBotBrain II platform.

Turn on, off and blink LED

Turns one of four LEDS (GREEN1, GREEN2, ORANGE, RED) on.

Turns selected LED off.

Blinks selected LED with interval in milliseconds.

Wait for button press

The Wait for button press pauses the programme and waits until any (or selected) button is pressed.

Save buttons

This block saves all buttons' value nto an integer variable. Every button has unique constant (if multiple buttons are pressed, the values are joined using bitwise OR):

Button Value
S1 1
S2 2
S3 4
S4 8

Save button

The Save button block saves selected button's state into an logical variable. If the button is pressed, the variable esuals to true. Otherwise is the variable equal to false.

If button is pressed

This block extends the If block. Commands placed inside the block are run onl if the selected button is pressed.

Play tone

This non-blocking (programme does not wait until the tone is played) block plays selected tone for period in milliseconds.

Group I/O ports

This group is avaliable only on the jBotBrain II platform.

Read analog

This command reads voltage meassured by one of six analog inputs (0-5) into an integer variable. Voltage is read in millivolts (1000mV = 1V).

Read digital input/port

The Read digital input block reads logical level from one of six inputs (0-5) into a boolean variable. If there is a high level on the pin, true is read.

The Read digital port block reads state of whole digital port into an integer variable. Logical levels are saved into high six bits (1 = high level, 0 = low level).

Set digital otuput/port

The Set dgital output block sets logical level on selected digital output (0-7).

The Set digital port block sets logical levels on whole digital port. Everz digital output has its own bit (1 = hogh level, 0 = low level).

Counter

This command saves counter's frequency in Hertz (1 Hz = 1 s-1). Each digital inputs (0-5) can be used as a counter. The counter is usable with Optocoupler (SD 01).

This command saves counter's edge count into an integer variable.

The Reset counter blocks sets edge count to zero.

Group Motors

This group is avaliable only on the jBotBrain II platform.

Set servo

Sets servo connected to one of eight digital outputs to position represented by integer value in range 1-100% or 500-2500 ms (50% or 1500 ms is center position, 0% and 100% or 500 ms and 2500 ms are limit positions ± 90°).

Set motor speed

Sets speed of motor A or B. Speed is an integer value in range 0-100 where 100 is the maximal speed.

Set motor direction

Sets direction of motor A or B.

Hodnota Otáčení
CLOCKWISE Clockwise
COUNTERCLOSKWISE Anti clockwise
STOP Brake (motor shorted)
STANDBY Motor disconnected

Set motors to standby mode

Sets motors to STANDBY mode (motors disconnected).

Group Sensors

This group is avaliable only on the jBotBrain II platform.

Tactile sensor

Saves state of a tactile sensor (SD 05 module) connected to one of 6 digital inputs into a boolean variable. Variable has true value if the sensor is pressed.

Conditional block extended from the If block. Runs commands placed inside this block if the tactile sensor is pressed.

Waits until the tactile sensor is pressed.

Potentiometer

Reads potentiometer shaft position (SA 01 module) in percent (0-100%) and saves it into an integer variable. The potentiometer is connected to an analog input.

Infrared rangefinder

Saves infrared rangefinder's (SA 03, SA 05 or SA 06 modules) distance in centimeters into a float variable. The rangefinder is connected to an analog input.

Temperature sensor

Saves temperature in °C read from temperature sensor (SA 04 module) connected to an analog input into a float variable.

Pressure sensor

Saves pressure read from pressure senro (SA 02 module) connected to an analog input into a float variable.

Ultrasonic rangefinder

Saves ultrasonic rangefinder's (SI module) distance in centimeters into a float variable. Distance is in range 5-100 cm.

Accelerometer

Reads acceleration on X, Y or Z axis from accelerometer (SI 01 module) and saves it into a float variable.

Gyroscope

Reads angle read on X, Y or Z axis from gyroscope (SI 06 module) and saves it into a float variable.

Resets accumulated angle value on X, Y or Z axis of gyroscope (SI 06 module).

RGB sensor

Saves color (R - red, G - green, B - blue) intensity read from a RGB sensor (SI 04 module) into an integer variable. Intensity is meassured in range 0-255.

Saves intensity of all colors into an array with 3 items:

Index Color
0 Red (R)
1 Green (G)
2 Blue (B)

Calibrates the RGB sensor (SI 04 module).

Turns the LED on a RGB sensor (SI 04 mdule) on (true) or off (false).

Light sensor

Saves value read from a light sensor (SI 02 module) into float variable.

Line sensor

Saves position read from a line sensor (SI 05 module) into integer variable.

Saves values read from a line sensor (SI 05 module) into an array with 5 items (indexed 0-4).

Calibrates black or white color sensed by the line sensor (SI 05 module) or resets to the default value.

Group Modules

This group is avaliable only on the jBotBrain II platform.

LCD display

Prints string onto a LCD display (MI 06 module).

Prints string onto the LCD without linefeed (usable for printing more values on a single row).

Clears LCD and moves carred on first line.

RGB LED

Sets RGB LED (MI 07 module) color (red, green and blue).

Stepper motor driver

Sets behaviour of stepper motor A or B connected to the driver:

Saves state of selected stepper motor into variable. The state is true if the motor has already reached given position.

Group Logic control

This group is avaliable only on the jBotBrain II platform.

Save coutner value

Assigns a falling/rising edge counter to a logical variable and saves edges count into integer variable count. The counter can be configured as incremental or encremental using + or - sign.

Reset counter

Resets value of counter that is assigned to a logical variable.

Timer

Starts timer TON, TOF or TP with delay in milliseconds. Timers are non-blocking, so the programme does not wait until they finished their operation.

TON If there is a rising edge (true) on the input, timer's output is set to logical 1 (true) after the delay. If the input changes to logical 0 (false), the output changes to logical 0 (false) immediately.
TOF If there is a falling edge (false) on the input, timer's output is set to logical 0 (false) after the delay. If the input changes to logical 1 (true), the output changes to logical 1 (true) immediately.
TP If there is an pulse on the input, timer's output is immediately set to logical 1 (true). The output is set back to logical 0 (false) after the delay.

Group Bluetooth

This group is avaliable only on the jBotBrain II platform.

Sets the Bluetooth module power (value true means power on). The module must be on in order to use other functions in this group.

Senfds specified number of bytes (buffer.length for all items) using the Bluetooth interface. The array's date type must be Byte. It is possible to use integers (0, 1, 2, …) or character literals ('A' stands for 65, ' ' equals to 32, …).

Receives given number of bytes (buffer.length for the maximum count) from the Bluetooth communication interface, saves them into specified array of Bytes and saves their count into given Integer variable. The variable's value is 0 if there is no received byte.

Sends string via the Bluetooth communication interface. This string is not terminated with new line character "\n", it has to be specified explicitly (e.g. "Hello, world\n").

Group Dashboard

This group is avaliable only on the jBotBrain II platform. It is used to interact with Dashboard panels.

Initialisation

Starts communication with the Dashboard. The initialisation must be done before first value is sent. The receiver mechanism uses Bluetooth interface and therefore the program should not contain any blocks from the Bluetooth group.

Sending values

Sends value as an integer, floating point number, logical value or a text string.

Sets RGB LED diode panel's color or color of given RGB LED matrix panel's pixel. The color can be also sent as a simple string in RRGGBB format.

Sets Buzzer panel's tone. The value sent is a numerical constant (same as constants in Java class jBotBrain2.hw.Sound).

Receiving values

Event caused by receiving value from given panel. This block's behavior is similar to function: following blocks are eveluated after value was received. The receiver runs as standalone thread, therefore it does not block the main program loop. But the event processing should not be too long (for example it must not contain any never ending loop), because it would block the receiver. The event is ideal for saving received value into a variable.

Saves currently received value into variable with specified date type (integer, floating point number or logical value) in order to use it in program. These commands can be used only in the Received value from panel event.

Saves currently received value into a byte array with one byte per character. This can be used for processing complex messages which does not contain just numerical or logical values (i.e. color format).