EV3 basics course. Color Sensor. Stop on third line. Improve program (part 4) Pro Preview

The program from part 3 should be refactored and improved to make it easier to understand and support. We extract most of the repeatable behaviours in a loop and this reduces the size of the program three times in terms of the number of blocks used. 

  • #150
  • 26 Oct 2015
  • 2:44

Color sensor program for stopping on third line

EV3-G program you can directly used in your STEM classes or in home


In the previous video we stopped at the third line. We can now count the lines using the EV3 color sensor but the program was not very nice and had a lot of redundancy that now I would like to remove.

This here is the program. As you might remember we just copy and paste these 6 blocks 3 times. We are now counting to the third line. What happens if we want to count to the 5th line or to the 100th line, we must copy these 6 blocks a 100 times. This is not how programming is supposed to be. This whole program is very redundant. As a rule of thumb always when you are developing a software, program if you copy and paste something you are making a mistake. That's the professional rule of thumb, if you are copying and pasting in a software program you are adding redundancy and you are making a bug, that's the rule. We must remove these 12 blocks and what we would like to do is actually execute these 6 blocks 3 times and for this we have a special block called a Loop. We just move this whole program in a loop.

We would like to loop not for unlimited amount of time but for 3 loops. It's the same program, the same blocks but only we removed the redundancy and we added a loop. It's following the same idea, logic. Move forward detect a black line, stop, move forward, detect the table, stop. Then repeat this again and then repeat this one more time.

The robot again stops at a third line only this time the program is restructured and it is 3 times shorter and much more clear.