Use for arrays and color sensors¶
The aim of the lesson in Programming –One Dimensional Arrays, also students get the idea of simulating games with the robot.
Preparing For This Tutorial:¶
The LEGO Mindstorm EV3 Robot that coincides with this tutorial comes from building specific sections found in the LEGO Mindstorm Education Core Set building instructions. You will need to build the main body for the robot (I’ll refer to as the Base Unit), plus color sensor.
Time constraints:
starting from 45 min - single lesson.
Exercise¶
1: Type the following code in order to generate three random numbers
#!/usr/bin/env python3
import random
v1 = random.randint(0,99)
v2 = random.randint(1, 100)
v3 = random.randint(1985,2014)
print(v1)
print(v2)
print(v3)
2: Make a Python program, which generates 10 random numbers (0, 1, 2) and store them in an array (each number refers to one color). Then, print the numbers on the screen.
3: Create a program where the robot recognizes colors shown in front of the sensor in order as given, and gives a sign of approval if you pass.
Note: Use the command system(“CLS”),to clear the screen, before the user starts to guess the numbers .
4: Make a program which works with more than three colors.
5: Make a program where the user also input a number of attempts to guess the numbers.
6: Add to your program the ability to generate random numbers of trials and a random range of numbers (colors), earn points and announce the winner.
Theory¶
Color Sensor
Color detected by the sensor, represented by an integer:
0: No color 1: Black 2: Blue 3: Green 4: Yellow 5: Red 6: White 7: Brown
Color detection works best if the colors are close to the official Lego colors (the colors of the Lego bricks) and if the object is about 8mm away from the sensor.
color_name
Returns a string: ‘NoColor’, ‘Black’, ‘Blue’, etc
Python One Dimensional Array (List)¶
A one-dimensional array is a group of elements having the same name. Individual elements are referred to using common name and unique index of the elements. The simplest form of an array is one-dimensional-array. The array itself is given name and its elements are referred to by their subscripts.
array_name = [] or array_name = list()
Declare One Dimensional Array in Python
Here is the general form to declare one dimensional array in Python
array_name = []
Here, arr is the name of the array. Here is an example, declaring an array named arr of int type, having maximum element size of 10 elements
Something specific about Python lists is that their data type doesn’t have to be unique and the number of elements expands as needed and does not have to be specified upfront.
Here is an example, declaring and initializing values to the array with name arr, containing 10 elements
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Python One Dimensional Array Example¶
Here are some example program, demonstrating one dimensional array in Python
Python One Dimensional Array
#!/usr/bin/env python3
arr = [1, 2, 3, 4, 5]
for i in range(len(arr)):
print("arr[{}] = {}".format(i, arr[i]))
Here is the sample output of this Python program:
Python One Dimensional Array
#!/usr/bin/env python3
print("Enter 10 array elements")
arr = []
for _ in range(10):
arr.append(int(input()))
print("The elements of the array are")
print(*arr)
print("Sum of all elements is:", sum(arr))
print("Average of all elements is:", sum(arr) / len(arr))
Here is the sample run of the above Python program:
Short help on programming¶
Commands/functions needed for the exercise
Configure color sensors |
cs= ColorSensor() cs.mode = “COL-REFLECT” |
---|---|
Your robot can speak. |
Sound.speak(‘RED’) |
To obtain random numbers |
import random v1 = random.randint(0,99) print(v1) |
Class __________
Student(s)___________________________________________
Use of Arrays in programming & Color Sensors
Previous understanding:
Concept of one dimensional array
Storing data in an array
Generating random numbers from a range
Basic commands in Python if, elif, else; for; input, print
Step 1: Type the following code in order to generate three random numbers
#!/usr/bin/env python3
import random
v1 = random.randint(0,99)
v2 = random.randint(1, 100)
v3 = random.randint(1985,2014)
print(v1)
print(v2)
print(v3)
Step 2: Make a Python program, which generates 10 random numbers (0, 1, 2) and store them in an array (each number refers to one color). Then, print the numbers on the screen.
Step 3: Student enters the number in order as shown, and the program responds if the student made a mistake or not.
Note: Use the command system(“CLS”),to clear the screen, before the user starts to guess the numbers .
Step 4: Make a program which works with more than three colors.
Step 5: Make a program where the user also enters a number of attempts to guess the numbers.
Step 6: Compete with the computer, which generates random numbers of attempts and random range of numbers (colors), score the points and announce the winner.