Here is some sample maze following code for your reference. Remember, every robot is slightly different from every other, and line following is pretty picky. This code is a guideline, but if you use it you might have to adjust some of the settings to work the best with your particular robot and line. The sample code assumes that the robot is setup astride the line, with its color sensor facing the center of the black stripe.
This code is the main task:
This is the function which initializes variables and creates constants:
This function gets and returns the current color value from the color sensor:
Our first example code uses a simple if-then approach which will yield a robot that can follow most lines pretty well, especially if the turns are not too abrupt. The "gold standard" for this kind of programming is to use a PID-based algorithm, which we did not recommend for this unit because it is challenging to understand and can be more difficult to program. If you are interested in using PID with VEX IQ, you might try this code. We tested it and it works pretty well, but it could be refined if you have a very advanced student. There are lots of online resources about understanding PID programming.
This is most appropriate for older students (grade levels 6-8). Younger students will need additional time and help to understand this.
Line following is a classic robot activity. Using a light sensor to pick out a black line on a white background and then guiding the robot based on that line teaches students about feedback and incorporating that real-time input in their program.
Each student team will need a VEX IQ robot kit and access to Robot Mesh Studio either online or installed on the school's computers.
The only thing you need for this exercise is a light surface with black lines on it:
Our test track -- the tiles are 12" square and the line is 3/4" black friction tape
An example of a wide white stripe (about 2-1/2 inches in this example) with a black line down the center.
15 minutes: This activity uses the clawbot with sensors. There is one small structural change that should take less than 15 minutes. The change is detailed in the Activity Guide.
10 minutes: configure motors and controller. Review the basics of a robotics control system including the VEX IQ Brain, radios, controller, and wiring. Refer back to the VEX IQ Robotics Education Guide for details on VEX IQ parts, and share that programming is what makes the robot useful.
10 minutes: download code and test robot.
100- 200 minutes: This is a pretty big project to do right. We suggest at least three and preferably four class sessions for this, as programmers can and do tinker with their code for hours.
Follows the I.5 activity in the VEX IQ Robotics Education Guide.
Grades | Activity | Identifier | Interim CSTA K–12 CS Standard | Framework Concept | Framework Practice |
3–5 | Work with a partner or partners | 1B-A-2-1 | Apply collaboration strategies to support problem solving within the design cycle of a program. | Algorithms and Programs | Collaborating |
3–5 |
Using the color sensor as a line detector Incorporate feedback from sensor to control movement No sample code provided -- all student-written |
1B-A-5-3 | Create a plan as part of the iterative design process, both independently and with diverse collaborative teams (e.g., storyboard, flowchart, pseudo-code, story map). | Algorithms and Programs | Creating Computational Artifacts |
3–5 |
Using the color sensor as a line detector Incorporate feedback from sensor to control movement No sample code provided -- all student-written |
1B-A-5-4 | Construct programs, in order to solve a problem or for creative expression, that include sequencing, events, loops, conditionals, parallelism, and variables, using a block-based visual programming language or text-based language, both independently and collaboratively (e.g., pair programming). | Algorithms and Programs | Creating Computational Artifacts |
3–5 |
Using the color sensor as a line detector Incorporate feedback from sensor to control movement No sample code provided -- all student-written |
1B-A-5-5 | Use mathematical operations to change a value stored in a variable. | Algorithms and Programs | Creating Computational Artifacts |
3–5 |
Using the color sensor as a line detector Incorporate feedback from sensor to control movement No sample code provided -- all student-written |
1B-A-3-6 | Decompose (break down) a larger problem into smaller sub-problems, independently or in a collaborative group. | Algorithms and Programs | Recognizing and Defining Computational Problems |
3–5 |
Using the color sensor as a line detector Incorporate feedback from sensor to control movement No sample code provided -- all student-written |
1B-A-3-7 | Construct and execute an algorithm (set of step-by-step instructions) that includes sequencing, loops, and conditionals to accomplish a task, both independently and collaboratively, with or without a computing device. | Algorithms and Programs | Recognizing and Defining Computational Problems |
3–5 |
Using the color sensor as a line detector Incorporate feedback from sensor to control movement No sample code provided -- all student-written |
1B-A-6-8 | Analyze and debug (fix) an algorithm that includes sequencing, events, loops, conditionals, parallelism, and variables. | Algorithms and Programs | Testing and Refining |
3–5 |
Using the color sensor as a line detector Incorporate feedback from sensor to control movement No sample code provided -- all student-written |
1B-C-6-11 | Identify, using accurate terminology, simple hardware and software problems that may occur during use, and apply strategies for solving problems (e.g., reboot device, check for power, check network availability, close and reopen app). | Computing Systems | Testing and Refining |
6–8 |
Using the color sensor as a line detector Incorporate feedback from sensor to control movement No sample code provided -- all student-written |
2-A-2-1 | Solicit and integrate peer feedback as appropriate to develop or refine a program. | Algorithms and Programming | Collaborating |
6–8 |
Using the color sensor as a line detector Incorporate feedback from sensor to control movement No sample code provided -- all student-written |
2-A-7-2 | Compare different algorithms that may be used to solve the same problem in terms of their speed, clarity, and size (e.g., different algorithms solve the same problem, but one might be faster than the other). [Clarification: Students are not expected to quantify these differences.] | Algorithms and Programming | Communicating about Computing |
6–8 |
Using the color sensor as a line detector Incorporate feedback from sensor to control movement No sample code provided -- all student-written |
2-A-7-4 | Interpret the flow of execution of algorithms and predict their outcomes. [Clarification: Algorithms can be expressed using natural language, flow and control diagrams, comments within code, and pseudocode.] | Algorithms and Programming | Communicating about Computing |
6–8 |
Using the color sensor as a line detector Incorporate feedback from sensor to control movement No sample code provided -- all student-written |
2-A-5-6 | Develop programs, both independently and collaboratively, that include sequences with nested loops and multiple branches. | Algorithms and Programming | Creating Computational Artifacts |
6–8 |
Using the color sensor as a line detector Incorporate feedback from sensor to control movement No sample code provided -- all student-written |
2-A-5-7 | Create variables that represent different types of data and manipulate their values. | Algorithms and Programming | Creating Computational Artifacts |
6–8 |
Using the color sensor as a line detector Incorporate feedback from sensor to control movement No sample code provided -- all student-written |
2-A-4-8 | Define and use procedures that hide the complexity of a task and can be reused to solve similar tasks. [Clarification: Students use and modify, but do not necessarily create, procedures with parameters.] | Algorithms and Programming | Developing and Using Abstractions |
6–8 |
Using the color sensor as a line detector Incorporate feedback from sensor to control movement No sample code provided -- all student-written |
2-A-3-9 | Decompose a problem into parts and create solutions for each part. | Algorithms and Programming | Recognizing and Defining Computational Problems |
6–8 |
Using the color sensor as a line detector Incorporate feedback from sensor to control movement No sample code provided -- all student-written |
2-A-6-10 | Use an iterative design process (e.g., define the problem, generate ideas, build, test, and improve solutions) to solve problems, both independently and collaboratively. | Algorithms and Programming | Testing and Refining |
Level | Activity | Standard |
KS-2 |
Using the color sensor as a line detector Incorporate feedback from sensor to control movement No code provided -- all student-written |
Design, write and debug programs that accomplish specific goals, including controlling or simulating physical systems; solve problems by decomposing them into smaller parts |
KS-2 |
Using the color sensor as a line detector Incorporate feedback from sensor to control movement No code provided -- all student-written |
Use sequence, selection, and repetition in programs; work with variables and various forms of input and output |
KS-2 |
Using the color sensor as a line detector Incorporate feedback from sensor to control movement No code provided -- all student-written |
Use logical reasoning to explain how some simple algorithms work and to detect and correct errors in algorithms and programs |
KS-2 |
Using the color sensor as a line detector Incorporate feedback from sensor to control movement No code provided -- all student-written |
Select, use and combine a variety of software (including internet services) on a range of digital devices to design and create a range of programs, systems and content that accomplish given goals, including collecting, analysing, evaluating and presenting data and information |
KS-3 |
Using the color sensor as a line detector Incorporate feedback from sensor to control movement No code provided -- all student-written |
Design, use and evaluate computational abstractions that model the state and behaviour of real-world problems and physical systems |
KS-3 |
Using the color sensor as a line detector Incorporate feedback from sensor to control movement No code provided -- all student-written |
Understand several key algorithms that reflect computational thinking; use logical reasoning to compare the utility of alternative algorithms for the same problem |
KS-3 |
Using the color sensor as a line detector Incorporate feedback from sensor to control movement No code provided -- all student-written |
Understand the hardware and software components that make up computer systems, and how they communicate with one another and with other systems |
KS-3 |
Using the color sensor as a line detector Incorporate feedback from sensor to control movement No code provided -- all student-written |
Undertake creative projects that involve selecting, using, and combining multiple applications, preferably across a range of devices, to achieve challenging goals, including collecting and analysing data and meeting the needs of known users |
KS-3 |
Using the color sensor as a line detector Incorporate feedback from sensor to control movement No code provided -- all student-written |
Create, re-use, revise and re-purpose digital artefacts for a given audience, with attention to trustworthiness, design and usability |