In this challenge you will practice the following computer science concepts in a team or small group environment:
- Creating Objects
- Using multiple conditional statements and looping statements
- Creating and Using variables
Computer Science Principles Curriculum
- Big Idea: Creativity: EU 1.2, LO 1.2.3, EK 1.2.3A, EU 1.3, LO 1.3.1, EK 1.3.1A
- Big Idea: Algorithms: EU 4.1, LO 4.1.1, EK 4.1.1B
- Big Idea: Programming: EU 5.1, LO 5.1.1, EK 5.1.1B, EU 5.2, LO 5.2.1, EK 5.2.1C, EK 5.2.1D
Common Core Standards
- English Language Arts Standards » Science & Technical Subjects: CCSS.ELA-Literacy.RST.9-10.2, CCSS.ELA-Literacy.RST.9-10.3, CCSS.ELA-Literacy.RST.9-10.4, CCSS.ELA-Literacy.RST.9-10.5, CCSS.ELA-Literacy.RST.9-10.6, CCSS.ELA-Literacy.RST.11-12.2, CCSS.ELA-Literacy.RST.11-12.3, CCSS.ELA-Literacy.RST.9-10.1, CCSS.ELA-Literacy.RST.9-10.2, CCSS.ELA-Literacy.RST.9-10.7, CCSS.ELA-Literacy.RST.9-10.8, CCSS.ELA-Literacy.RST.9-10.9
- Standards For Mathmatical Practice: CCSS.Math.Practice.MP1, CCSS.Math.Practice.MP2, CCSS.Math.Practice.MP5, CCSS.Math.Practice.MP6, CCSS.Math.Practice.MP7, CCSS.Math.Practice.MP8, CCSS.Math.Content.HSF.IF.A.1
- Control Structures
- Conditional Statements
- If Statement
- Color Sensor
This challenge will use several libraries such as the Motor, ColorSensor, Utility, Screen and Sound library. You will use different conditions with the Color Sensor for the purpose of following the line. There are multiple ways to approach this task, so feel free to try some other ways once you finish this lab!
This project requires you to download this program template, which will be completed in this assignment.
Goal 1: Creating objects
First you will need to unzip your template and open the project in Sodbeans. Open the main.quorum file. In the top of the file you should have the appropriate code for using the Quorum ColorSensor and the Motor libraries inside the Lego library.
Activity : Instantiate the Motor and the ColorSensor objects
Call the Motor object motor and the ColorSensor object colorSensor.
Now you must set the port to work with. This will be the port that the Color Sensor is connected to on the robot. You will do that using the SetPort statement.
Example : Tell the port your Color Sensor is connected
//This line of code will set the port to work with in parenthesis
You can change the port at any time. If you add other sensors you may want to rearrange to get the best fit.
Just remember to double check that the ports you are connected to are the ones you have set in your code!
Goal 2: Using conditional and looping statements
Your code should work like this: the robot will go forward if the color sensor detects the colored line. If the sensor does not detect the colored line, it will first turn to the right in search of that color. If it does not detect that color after turning a certain number of degrees to the right, it will turn to the left. You will have to consider how much the robot turned to the right, so when it turns back left it will check far enough in that direction.
Let’s start with the conditions now. In order to get the color of the line, you will use the GetColor() command. You may have to experiment to find the setting that the color sensor will respond to best.
Activity : Write the proper code to set the condition of the motors A and D to move forward
//This line is the looping condition; once the condition doesn’t match anymore the program stops
Try experimenting with the Color Sensor. Let's move the Lego Robot forward when it finds the color blue (we suggest using blue tape). Then, if the Lego Robot finds another color (it goes off the tape), it will stop immediatly.
Don't forget to set the speed of the motors. Look back at earlier robot lessons if you don't remember how to do this.
Example : Conditions in case the color line isn’t forward
//Before the conditions, to make it easier, let's call 'color' the values the sensor will get
color = colorSensor:GetColor()
if //the color is equal to anything besides blue
//The Lego Robot will turn to the right in search of blue
//The right wheel will rotate a certain number of degrees backward
//The left wheel will rotate the same number of degrees forward (creating a turn)
//After the time you decided, the Lego Robot will change direction and turn to the left side
//The Lego Robot will turn to the left to find the color line
If the robot finds the blue line again, you will want it to go back to moving forward, starting the whole process again. A condition should move your program back to the beginning again in this case.
Activity : Creating and changing variables in many loops
To achieve this, you will have four loops. The robot will take each step in an effort to find the blue line and then start moving forward again.
You will need to add one more loop between the two in the previoius example. You will also need one before the end.
Example : Conditions in case the color line isn’t forward
//The car will try to find the line to the RIGHT
if condition = condition1 //turn to the right
condition = condition2
//After a certain amount of time, the car will change its direction to the LEFT
elseif condition = condition2
condition = condition3
//The car will then try to find the line to the LEFT
elseif condition = condition3
condition = condition4
//After a certain amount of time, the car changes its direction back to the RIGHT
elseif condition = condition4
condition = condition1
Don't forget to write the conditions after the objects have been created.
Activity : Call the actions
Your program should work just fine now, but to make it better let’s add this line after the end in order to tell the program to wait 250 milliseconds before it stops.
End of Lesson
You have reached the end of the lesson for Actions. To view more tutorials, press the button below or you can go back to the previous lesson pages.