Use for arrays and color sensors¶
Prerequisite¶
Students should be familiar with:
Concept of one dimensional arrays
How to store data in arrays
Random generalization of items
Time constraints:
starting from 45 min - single lesson
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.
Fig.1
Basic configuration plus color sensor
Effects¶
Computer science (Python) - This lesson will help students to understand the use of one-dimensional array, from abstract concepts to the point where they actually understand how it works, storing colors in random order. Students will get familiar with random functions, which are used while generating colors, thus getting a sense of gamification. They will stay more engaged during classes if trying to understand how the program works. Also they will use basic structures as if..else and loops, which are needed during programming in Python.
Exercise¶
The text of the exercise - step-by-step instruction - what students should do is in the Arrays_Worksheet_students . Depending on the student’s ability, they perform steps and report the program’s results.
It is suitable to make groups of 4-5 students to have collaborative work during this task. They compete with other groups, which gives extra motivation to stay focused during the task.
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.
Example solution¶
Part 1¶
Computer science (Programming in Python)
Step 1:
Test out this code to configure color sensors
Program
#!/usr/bin/env python3
cs= ColorSensor()
assert cs.connected
cs.mode = "COL-REFLECT"
Step 2:
Create a program to make the robot generate three random colors (red, green, blue) and store them in a one-dimensional array (designed for 10 elements). Also, the robot repeats the colors as generated.
Program
#!/usr/bin/env python3
colors=[]**
for i in range(0,10):
colors.append(random.randint(1,3))
for i in range(0, 10):
if colors[i]==1 :
Sound.speak('RED')
time.sleep(2)
elif colors[i]==2 :
Sound.speak('GREEN')
time.sleep(2)
elif colors[i]==3 :
Sound.speak('BLUE')
time.sleep(2)
time.sleep(1)
Additional info:
The color card should be shown close to the sensor (approx. 1 cm). Please look at the picture below.
Step 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 passed.
Program
#!/usr/bin/env python3
Sound.beep()
Sound.speak('Show the first color')
for i in range(0,10):
if i!=0:
Sound.speak('Show the next color')
time.sleep(3)
if colors[i]==1 and cs.color==cs.COLOR_RED:
Sound.speak('CORRECT')
time.sleep(2)
elif colors[i]==2 and cs.color==cs.COLOR_GREEN:
Sound.speak('CORRECT')
time.sleep(2)
elif colors[i]==3 and cs.color==cs.COLOR_BLUE:
Sound.speak('CORRECT')
time.sleep(2)
else:
Sound.speak("WRONG")
time.sleep(1)
Sound.speak("Game Over")
raise SystemExit
Sound.speak("You won")
time.sleep(1)
Sound.speak("Game Over")The program in Python:
Solution of step4, step5:
Make a program which works with more than three colors.
Make a program where the user also input a number of attempts to guess the numbers.
#!/usr/bin/env python3
from ev3dev.ev3 import \*
import time
import random
import sys
cs= ColorSensor()
assert cs.connected
cs.mode = "COL-REFLECT"
colors=[]
Sound.speak('Input the number of attempts')
NumofAtt =int(input("Number of attempts:"))
Sound.speak('Input the number of colors')
NumofClr=int(input("Number of colors to play with(1-5):"))
for i in range(0,NumofAtt):
colors.append(random.randint(1,NumofClr))
time.sleep(3)
for i in range(0, NumofAtt):
if colors[i]==1 :
Sound.speak('RED')
time.sleep(2)
elif colors[i]==2 :
Sound.speak('GREEN')
time.sleep(2)
elif colors[i]==3 :
Sound.speak('BLUE')
time.sleep(2)
elif colors[i]==4 :
Sound.speak('YELLOW')
time.sleep(2)
elif colors[i]==5 :
Sound.speak('BLACK')
time.sleep(2)
time.sleep(5)
Sound.beep()
Sound.speak('Show the first color')
time.sleep(2)
for i in range(0,NumofAtt):
if i!=0:
Sound.speak('Show the next color')
time.sleep(3)
if colors[i]==1 and cs.color==cs.COLOR_RED:
Sound.speak('CORRECT')
time.sleep(2)
elif colors[i]==2 and cs.color==cs.COLOR_GREEN:
Sound.speak('CORRECT')
time.sleep(2)
elif colors[i]==3 and cs.color==cs.COLOR_BLUE:
Sound.speak('CORRECT')
time.sleep(2)
elif colors[i]==4 and cs.color==cs.COLOR_YELLOW:
Sound.speak('CORRECT')
time.sleep(2)
elif colors[i]==5 and cs.color==cs.COLOR_BLACK:
Sound.speak('CORRECT')
time.sleep(2)
else:
Sound.speak("WRONG")
time.sleep(1)
Sound.speak("Game Over")
raise SystemExit
Sound.speak("You won")
time.sleep(1)
Sound.speak("Game Over")
raise SystemExit
Summary¶
Students have just learned how to use a color sensor. They are able to store colors in arrays and check if generated colors are equal as shown in front of the sensor. Also they can make different versions of this program example: counting score at several attempts, think of other type of data to store, etc.