Museum at night

First LEGO toy was produced in 1932. In Denmark there is the LEGO Museum. Imagine that at night the toys become alive in the museum. They can speak and move. Film lovers know such cases – films: Museum at night or Toy Story present similar situations.

The aim of the lesson is to programme our robot to do something you want but only when it is dark around – at night.

Mathematical problem: arithmetic average.

Prerequisite

What students should know before

  • arithmetic average

  • how to work with class LEDs, Display, Sound, Motors see Basics Modules.

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 of the robot (I’ll refer to use the Base Unit), plus a colour sensor directed up (to better see the light).

  • In the classroom, we blind the roller shutters. Lighting the light means the day, turning off – night.

image0

image1

Effects

Mathematics - During this lesson, students have to use an arithmetic average of two or more values.

Computer science - This lesson will show you how to create the programme using colour sensor working in COL- AMBIENT mode - to measure ambient light intensity.

Science – On a sunny day, it is worth taking a moment to allow students to observe the difference between the intensity of sunlight and artificial light.

Exercise

Create a programme that will cause the robot to do something you want but only when it is dark around – at night.

  1. Create a program in which the colour sensor measures the intensity of ambient light. The programme should end after pressing any brick button.

  2. Put down the results of your classmates when it is dark and when the light is on and calculate the arithmetic mean. Create a programme in which the robot asks you about the average and compare the intensity of light with this average and then write the message: light or dark.

  3. Modify your programme such that your robot moves, says something or plays any music file, switches on the light on the brick and displays some eyes when the light is off and does nothing (or only snoring) and has sleeping eyes when it is dark around.

Example solution

Part 1

Science

Students have the possibility to see the difference between sun and artificial light intensity.

Computer science

Step 1:

Check out this text to configure and verify the colour sensor. Put the colour sensor into COL-AMBIENT mode to measure ambient light intensity. In this mode the sensor will return a value between 0 and 100 (0 means dark). In the programme, firstly import the necessary libraries (lines 2-5). Next, create instances of objects (lines 8-9). Subsequently, set the sensor ambient light intensity mode (line 10). Until no button is pressed, make a loop (lines 11-13). Finally, write the value read by the sensor (line 12). The programme uses error handling (try-except) to detect the situation of improperly connected sensor or motors (lines 7 and 14,15).

Program

#!/usr/bin/env python3

1.  #!/usr/bin/env python3

2.  from ev3dev2.button import Button

3.  from ev3dev2.sensor.lego import \*

4.  #or from ev3dev2.sensor.lego import ColorSensor

5.  from time import sleep

6.
7.  try:

8.    cl = ColorSensor()

9.    btn=Button()

10.   cl.mode='COL-AMBIENT'

11.   while not(btn.any()):

12.     print(cl.value())

13.     sleep(0.5)

14. except :

15.   print("Verify sensor connection")

File

Step 2:

On a sunny day additionally students can measure the intensity of sunlight.

Summary

We have just learnt how to configure a colour sensor. We are able to read the value from the colour sensor working in ambient light intensity mode.

Part 2

Mathematics

Students have to compute arithmetic average and use it in the programme.

Computer science

Complete the above programme to allow the robot to distinguish day and night.

Step 1:

Firstly, import the necessary libraries (lines 3-5). Next, create instances of objects (lines 8-9). Set the sensor in ambient light intensity mode (line 11). Get the value of the average (line 12). Until no button is pressed, make a loop (lines 14-18). Depending on the value read by the sensor, write a comment (lines 15-18). As above the programme uses error handling (try-except) (lines 10 and 19,20).

Program

1.  #!/usr/bin/env python3

2.
3.  from ev3dev2.sensor.lego import ColorSensor

4.  from ev3dev2.button import Button

5.  from time import sleep

6.
7.  cl = ColorSensor()

8.  btn = Button()

9.
10. try:

11.   cl.mode='COL-AMBIENT'

12.   avg=int(input("Insert the average please: "))

13.   print(avg)

14.   while not(btn.any()):

15.     if cl.value()>avg:

16.       print("light it is time to sleep")

17.     else:

18.       print("dark")

19. except:

20.   print("Verify sensor connection")

File

Summary

We have just created a programme that allows the robot to distinguish day and night .

Part 3

Computer science

Modify the above programme to allow the robot to move, say something or play any music file, switch on the light on the brick and display some eyes when the light is off and do nothing (or only snoring) and have sleeping (tired) eyes when it is dark around.

Firstly, import the necessary libraries (lines 2-9). Next, create instances of objects (lines 12-18). Set the sensor operating mode (line 20). Get the value of the average set in the previous exercise (line21). Until no button is pressed, make a loop (lines 24-40). If the value read from the sensor is greater than the average, perform the tasks specified in the exercise i.e.display the image (Tired Eyes) (lines 26-27, turn off the LEDs (line 28) and stop the servomotors (lines 29-30). In the opposite case, insert the picture (lines 32-34), turn on the LEDs (lines 36-37) and run servomotors (lines 38-39). Independently executed conditional expression, refresh the screen (line 40). After finishing the loop operation - clean up, i.e. turn off the servomotors (lines 43-44), clean the screen (line 41) and turn off the LEDs (line 42). As above the programme uses error handling (try-except) (lines 11 and 45,46).

1.  #!/usr/bin/env python3

2.  from ev3dev2.sensor.lego import ColorSensor

3.  from ev3dev2.motor import LargeMotor,OUTPUT_B,OUTPUT_C

4.  from ev3dev2.button import Button

5.  from ev3dev2.display import Display

6.  from ev3dev2.sound import Sound

7.  from ev3dev2.led import Leds

8.  from time import sleep

9.  from PIL import Image # Python Image Library (PIL)

10.
11. try:

12.   cl = ColorSensor()

13.   btn = Button()

14.   lcd = Display()

15.   sound = Sound()

16.   mB = LargeMotor(OUTPUT_B)

17.   mC = LargeMotor(OUTPUT_C)

18.   leds = Leds()

19.
20.   cl.mode='COL-AMBIENT'

21.   avg=int(input("Insert the average please: "))

22.   print(avg)

23.
24.   while not(btn.any()):

25.     if cl.value()>avg:

26.       im =Image.open('/home/robot/images/files/Eyes/Tired right.bmp') #file handling

27.       lcd.image.paste(im, (0,0))

28.       leds.all_off()

29.       mB.off()

30.       mC.off()

31.     else:

32.       im =Image.open('/home/robot/images/files/Eyes/Neutral.bmp')

33.       #file handling

34.       lcd.image.paste(im, (0,0))

35.       sound.play('/home/robot/sounds/Blip 1.wav')

36.       leds.set_color('LEFT', 'AMBER')

37.       leds.set_color('RIGHT', 'AMBER')

38.       mB.on(30)

39.       mC.on(30)

40.       lcd.update()

41.       lcd.clear()

42.       leds.all_off()

43.       mB.off()

44.       mC.off()

45. except Exception as e:

46.   print(e)

File

Summary

We have written a programme that allows the robot to perform various activities when it is dark around.

All presented source codes can be found in the file: museum_at_night_ev3dev2 (library ev3dev2). Solutions using the library ev3dev are in the file museum_at_night_ev3dev

Next Section - Point to point