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

English

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.

Courses and lessons with this Tutorial

This Tutorial is used in the following courses and lessons

Image for EV3 Basic Course. Introduction to robot programming, construction and sensor use
  • 38
  • 193:26
  • 30
Image for Color Sensor
  • 8
  • 0
  • 3
  • 3d_rotation 0
Image for Instructors Remote Training
  • 136
  • 280:11
  • 156
Image for Color Sensor Theory and Practice
  • 5
  • 0
  • 0
  • 3d_rotation 0
Image for Instructors Remote Training
  • 136
  • 280:11
  • 156
Image for Color Sensor
  • 9
  • 0
  • 4
  • 3d_rotation 0