docs:blox:examples-logic

6.2 BLOX: Events and Logic

Using BLOX to build firmware is like piecing together a puzzle where each piece represents a different action or decision your device needs to make. Here’s a simple way to understand it:

6.2.1 Everything Happens Sequentially

  • Imagine you're giving instructions to a robot. You tell it to move forward, then turn left, then pick up an object. The robot will follow these steps one after another, in the exact order you gave them.
  • In BLOX, each block you place in the workspace is like an instruction for your device, and it will follow these steps in the order they appear.

6.2.2 Logic Flow Control

  • Inside this sequence, you may sometimes want your robot to repeat a task, like picking up 10 objects one by one. Instead of giving the same instruction 10 times, you can use a Repeat Block.
  • A “repeat” block tells the robot, “Do this task a certain number of times,” while a “while” block tells it, “Keep doing this task as long as a certain condition is true.”
  • Sometimes, you want your robot to make decisions. For example, “If there's an object in front of you, pick it up; if not, keep moving.”
  • In BLOX, you can use logic blocks like “if” to control what your robot does based on different conditions. This way, you can make your device smart enough to handle different situations automatically.
  • If you'd like the device to wait, you can use a Delay block
  • There are many more examples

By arranging these blocks in the workspace, you're essentially building a set of instructions that the firmware will follow, making your device do exactly what you want it to do.

6.2.3 Sequential Instructions

Let's start with a very basic example.

Your BLOX has two onboard RGB LEDs. Let's setup a simple program to control these LEDs. No inputs, no change to the flow of the program. Just a set of instructions executed sequentially.

Once it starts to run the firmware we want to: If we build this out in BLOX it would look like this:

- Set both LEDs to RED
- Wait for 1 second
- Set both LEDs to GREEN
- wait for 1 second
- Set both LEDs to BLUE
- wait for 1 second
- Then repeat (the pattern will run over and over)

It's really that easy! You can substitute the LEDs for any other output and already build out a couple interesting projects

6.2.4 Control flow of Instructions using a Logic block

Let's expand upon our basic example

Lets add a little logic to the program. Suppose we only want the LED pattern to run, if a certain condition is being met. For the sake of simplicity, lets use the onboard MODE button as an input. We only want the program to run if the MODE button is pressed, otherwise, just turn the LEDs Purple.

Once it starts to run the firmware we want to: If we build this out in BLOX it would look like this:

IF the button is pressed:
- Set both LEDs to RED
- Wait for 1 second
- Set both LEDs to GREEN
- wait for 1 second
- Set both LEDs to BLUE
- wait for 1 second
ELSE
- set the LEDs to Purple

You can apply this exact concept to a lot of other examples

  • Waiting for a motion sensor before using the Beeper to make on an alarm sound
  • Waiting for a button press before raising a TV lift actuator
  • Read the status of a garage door sensor and display the status on an LCD
  • Use it as a thermostat, if the temperature drop below a certain value, turn on a heating device
docs/blox/examples-logic.txt · Last modified: 2024/08/19 20:45 by admin