LEGO MindStorms EV3 Model Analysis – Color Sorter Part 3

In my previous post, I analyzed the code of Color Sorter model and mentioned that the mounting device code could be improved to make the sorting process faster without changing the mechanical structure.

In the initial code, each time the mounting device drops out one color brick, it will move back to the initial position (to the touch sensor) before going on to drop the next color brick.

The code segment to change has been marked in blue in the above diagram. Let’s see how to do it now!

Initialize and Add a Variable to Store Previous Brick’s Color

At the beginning of this segment, the smart hub beeps and clears the screen. The program adds a variable called “Previous Color” and set its initial value to 2, which represents Blue color.

Then, the program turns on the big motor till the mounting device touches the touch sensor and stops the big motor. That is the same as the initial code design.

Compare Current and Previous Blocks’ Color

At this point, the program enters into the loop of “SRT”. It retrieves the item value of list “A” and then minuses the variable “Previous_Color” from it. Please note the each color has a color value representing it. Blue, Green, Yellow and Red is represented as 2, 3, 4 and 5 respectively. if current item value of list “A” is 3, which means the current brick color is Green. If variable “Previous_Color” stores 2, which means the previous brick color is Blue. Their difference is 3 – 2 = 1.

So what is the usage of this value difference? I will soon introduce it.

Move Mounting Device to Proper Position

This sub segment contains two switch block. The first switch block uses the current item value of list “A” as data input and makes smart hub speak out the color name.

The second switch block uses the color difference value mentioned above as input, which will decide the degrees and direction that big motor should turn. Since the four colors value are 2, 3, 4 and 5, their difference could range from -3 (2 – 5 = -3) to 3 (5 – 2 = 3). There is also possibility that previous and current bricks have the same color, so the value difference could be 0, which means that the big motor does not need to move at all.

Dropping Out the Color Brick

Once the big motor has moved to proper position, the medium motor could begin to work, dropping the unfixed component and then push it up to drop out the color brick. Which is totally the same as the initial code version.

What is different from original version is that the program will set current brick color value to “Previous_Color” variable, so the program is getting ready for next iteration.

The left part of the code is the same as the original version. The program compares the length of list “A” with the iteration number. The loop will end when it iterates all of items in the list.

Data Wire Relationship

The data wire relationship has become a bit more complex. From the below diagram, you could see that the current item value of list “A” has been wired as input to several other blocks. The difference between current block value and previous block color is also wired as input to the switch block of turning on big motor.

The final effect for Colour Sorter model is shown in the video below. Enjoy your Coding and Have Fun!

P.S. If you would like to check the code by yourself, here is the project file:

Note: All the analysis articles are copyright products of Anyone re-posting them should credit author and original source. Anyone using them for commercial purposes or translating them into other languages should notify TheCodingFun and get confirmation first. All Rights Reserved.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.