In this post we will cover the Compare programming block used within the Lego MINDSTORMS EV3 programming software. We will cover all the available options and cover it’s uses within a step by step example program.
What is the Compare Block?
The Compare block compares 2 numbers to find out if they are equal, not equal, or greater. It has 6 modes which offer 6 types of comparisons (covered in the next section). It’s available from the red data operations palette:
What are the Compare Block Modes?
The compare block has 6 modes (or 6 types of compare operations). These include:
- Equal To: True if the A input equals B input (A = B)
- Not Equal To: True if A input does not equal B input (A != B)
- Greater Than: True if A input is greater than (larger) than the B input (A > B)
- Less Than: True if A input is less than the B input (A < B)
- Greater Than or Equal To: True if A input is greater than or equal to B input (A => B)
- Less Than or Equal To: True if A input is less than or equal to B input (A <= B)
How to Use the Compare Block
For the compare block to work both A and B inputs need to be either wired or contain a hard-coded value. From there it’s just a matter of selecting the appropriate mode and wiring the output into the block of your choice.
The example below shows how to use the compare block to check if the center button has been pressed. The center button will output the number 2, and the compare button will check if the output is equal to 2. If it is, “Center!” will be displayed on the screen, if any other button has been pressed it will display “Not Center”.
EV3 Compare Block Example Program: Random Back and forward movements
This program will move a Lego MINDSTORM robot randomly back and forth. In terms of a robot build, we recommend the Explor3r. It’s a quick, simple build and best of all it’s compatible with both the Lego MINDSTORMS Education and Retail kits. For build instructions please click here. Note: all you need is the base for this program.
Program Logic:
- Generate a random number between 1 and 10
- If the random number is less than or equal to 5, move the robot forward 1 rotation
- If the random number is more than 5, move the robot backwards 1 rotation
- Loop this process for 20 seconds
Lets get started:
- Loop the program for 20 seconds
- Drag and drop an orange Loop block next to the start block
- Set its mode to Timer
- Set the seconds input to 20
- Drag and drop an orange Loop block next to the start block
- Generate a random number between 1 and 10 and check to see if it’s less than or equal to 5
- Drag and drop a red Random block inside of the Loop block
- Ensure the lower bound value is 1 and the upper bound value to 10
- Drag and drop a red Compare block next to the random block
- Set its mode to Less Than or Equal To
- Set the A input to 5
- Wire the output from the Random block to input B
- Drag and drop a red Random block inside of the Loop block
- Depending on the output move the robot backwards or forwards
- Drag and drop an orange Switch block next to the compare block
- Set its mode to Logic
- Wire the output from the Compare block into the Switch
- Drag and drop a green Move Steering block inside of the True case of the switch block
- Set its Power to 75
- Set the number of rotations to 1
- Drag and drop a green Move Steering block inside of the False case of the switch block
- Set its Power to -75
- Set the number of rotations to 1
- Drag and drop an orange Switch block next to the compare block
Hit to download and play to test your program.
Example Program 2: Follow a Black Line With a Lego EV3 Robot Until it Stops
This program will move a Lego MINDSTORMS robot forwards to follow a straight black line. If the black line stops, the EV3 robot will stop. In terms of a robot build, we recommend the Explor3r. It’s a quick, simple build and best of all it’s compatible with both the Lego MINDSTORMS Education and Retail kits. For build instructions please click here. Be sure to follow the base instructions as well as the color sensor attachment instructions.
Apart from the robot all you need is a thick black line for the robot to follow. We’ve put a PDF together for you to download and print if you need one. It can be downloaded here.
Program logic:
- Compare the output from the color sensor:
- If it’s equal to 1 (black) then keep the EV3 robot motors On
- If it’s not equal to 1 then stop the robot
- Loop the program
If you would like to learn more about the Color Sensor, see our detailed post here for more detail.
Lets get started:
- Loop the program for 20 seconds
- Drag and drop an orange Loop block
- Check the color sensor output and check to see if its equal to 1
- Drag and drop an yellow Color Sensor block inside of the Loop block
- Set its mode to Measure | Color
- Drag and drop a red Compare block next to the color block
- Set the mode to Equal To
- Wire the output of the Color block into input A
- Set input B to 1
- Drag and drop an yellow Color Sensor block inside of the Loop block
- Move the EV3 Robot forwards if the color is detecting Black (1)
- Drag and drop an orange Switch block next to the Compare block
- Set its mode to Logic
- Drag and drop a green Move Steering block into the True case
- Set its mode to On
- Set the Power to 50
- Drag and drop a green Move Steering block into the False case
- Set its mode to Off
- Drag and drop an orange Switch block next to the Compare block
Hit to download and play to test your program.
We hope you enjoyed this deep dive post into the Lego MINDSTORMS EV3 Compare programming block. Feel free to leave a comment if you have any questions or have a suggestion for a future post.