Examples

These examples shows some basic skills needed to work in the jBlocks environment. They can help you with first programmes and exploring the jBotBrain II board.

Blocks

Simple LED blinking

First example simply prints information to console (Print command), blinks all four LEDs placed on jBotBrain II and runs never-ending loop. Never-ending loop causes that the programme does not stop after configuring LEDs, but runs infinitely.

Comment (white block) is helpful to store some information about the programme, it does not have any influence to its behaviour.

Example JBL file: blink1.jbl.

Another LED blinking

The second example blinks orange LED with 200 ms interval using Turn LED on and Turn LED off commands in a never-ending loop. These commands are called depending on a led variable.

If the led variable is equal to true, LED is turned on and variable's value is changed to false. In the other case is LED turned off and variable led is set back to true. Every iteration of the cycle also contains Wait 300 ms command which causes a little delay.

This example can be simplified by inverting the led variable value instead of setting it to true or false in if condition:

Example JBL file: blink2.jbl.

Printing potentiometer values

This programme prints potentiometer's shaft position in percent into console in a never-ending loop with 200 ms delay. Reading potentiometer and printing value is placed into a separate function potenciometer in order to have the programme clear.

Example JBL file: potentiometer.jbl.

Mathematical functions

This example prints sine values for 0°, 15°, 45°, 60°, 75° and 90°. The result is stored in a result variable. It uses functions sin and toRadians functions from the Math class.

Because sine values are in interval < -1; 1 >, we need to declare the result variable as a floating-point variable and cast the sin function's result using (float).

The programme is dedicated for console output sicne it does not need jBotBrain II hardware.

Example JBL file: sine.jbl.

Array sorting

This example creates an array with given length (variable length), fills it with random values, sorts using the Bubble Sort method and prints both unsorted and sorted arrays.

Bubble Sort is one of the simplest sorting algorithms - walks trough the array, compares and eventually swaps two neighbouring items until it is not sorted.

You can also try this algorithm with bigger array by raising the length variable.

Example JBL file: bubblesort.jbl.

Ladder diagrams

LED blinking

  1. The TP timer with 300 ms delay is started if its output is low. This is done on start at first because the output is low when program starts. The PULSE variable holds its value only during current cycle iteration.
  2. Switching LED in case variable PULSE is high. Green LED is on if the pulse is not present and variable LED is high.

Example JBL file: ld_blink.jbl.

Counter

  1. Pulse generation using TP timer as in LED blinking, the pulse increments counter and blinks orange LED.
  2. Printing the counter's state (using $counter[2]]) and turning green LED on. The green LED is always on because there is no input on 2. rung (this could be used as run indicator).

Example JBL file: ld_pulse.jbl.

Conveyor belt

This example shows program for simple conveyro belt with two motors which are started after specified delay on START button press. For safety reasons, it is important to stop motors when the STOP button is pressed.

  1. Variable RUN is high if the STOP button is not pressed and the RUN button is pressed or the RUN variable was high in previous iteration. This preserves its value unless the STOP button is pressed.
  2. TON timer with 1000 ms delay is started if the RUN variable is high.
  3. Motor A is started if the RUN variable is high.
  4. Motor B is started if timer's output is high. The TON timer's output is high after specified delay (1000 ms).
  5. The last one rung lights red LED if the conveyor belt is stopped.

Example JBL file: ld_conveyor_belt.jbl.

Dashboard

This example is simple Dashboard for controling peripherals (red LED diode and servo connected to digital output OUT1).

The Dashboard contains these panels:

  • Button with variable name button - turns the LED on and off
  • LED diode with variable name led
  • LED display with variable name angle - displays servo position in degrees
  • Sliding potentiometer with variable name pot - controls servo position
  • Servo with variable name servo

Program for the jBotBrain II computer just receives values and sends responses:

Example files:

Nahoru