Spybots Smart Parts  
  The Unofficial Resource Centre for Lego Spybotic  
   
img  
   
   

TUTORIALS

TUTORIALS

intermediate

Measuring Light - mindscript

Tutorial Details:
Difficulty Level: Intermediate
Topics Covered: Using events, watchers and conditionals with the light sensor to measure light.
Assumed Knowledge: The Basics, My First Program, Get Moving, Lighting Up (parts 1 & 2), Conditional (If), Events,Watchers & the Bumper.
Written By: BILL LANE

Print Version

As we saw in Events, Watchers and the Bumper it is possible to use events to detect changes in the Spybots sensors. In that tutorial we used sensor 1 (the touch sensor) to react to an obstacle. Many of the Spybot missions use light as an important component. Which makes it handy to be able to use the light sensor. In this tutorial we'll use multiple events and watchers to turn on the arc lights to indicate light intensity.

The first step is to declare the sensor:

sensor opto on 2

This is almost identical to declaring the touch sensor. Except that we've given it a different, more logical name, and it's number 2 (rather than 1). Next we need to declare the events. But before we do we need to work some things out.

In this exercise I want more ARC lights to come on as the light intensity increases. To do this I'll break the available light readings into six ranges (1 for each ARC light). By default the light sensor returns a percentage value (i.e 100 for lots of light, 0 for no light). Therefore, each range will cover around 16 - 17%.

In the bumper tutorial we wanted events to occur when the pressed state changed. But that won't work here. Instead we're going to define a condition under which the event will be triggered. You'll know from Conditional (If) that we can test for a range of values and that's exactly what we're going to do here:

event opto1 when opto is 0..17
event opto2 when opto is 18..34
event opto3 when opto is 35..51
event opto4 when opto is 52..68
event opto5 when opto is 69..85
event opto6 when opto is 86..100

What you can see is that I've actually defined six events each occuring when the light sensor is in a specific range. For example, opto1 will occur when the light sensor returns a value between 0 and 17. These six events cover the full range of light readings so each time the light intensity changes the relevant event will be triggered.

The next step is to create the watchers. Once again this is very similar to what we did in Events, Watchers and the Bumper. Except this time we want one watcher for each event and we're not planning to make the Spybot change direction. Instead we'll use LED [ iDisplay ] to reset the ARC lights ( this command was covered in Lighting Up Part 1 and the table of light configurations is in Lighting Up Part 2 ). For simplicity I'll just show one watcher here. The rest are identical except for the light configuration we send to LED. You'll also note that I haven't included the constant iDisplay I've replaced it with it's actual value (27). That saves me from including spybot.h or declaring the constant myself:

watcher light1 monitor opto1
{
LED[27] = 1
}

The only other step required to make it work is to start the watcher in the main task by writing start light1 etc inside the main command block. There is only one thing wrong with this. It requires a change in light intensity before the ARC lights will actually display anything. We can fix this by using a series of conditional if statements to initialise the ARC lights. It looks like this:

if opto is 0..17 {LED[27]=1}
if opto is 18..34 {LED[27]=3}
if opto is 35..51 {LED[27]=7}
if opto is 52..68 {LED[27]=39}
if opto is 69..85 {LED[27]=55}
if opto is 86..100 {LED[27]=63}

Each if simply checks to see if the light is within a specified range (the same range as the event declaration) and if it is turns on the appropriate arc lights. Only one of the if statements can possibly return TRUE so there will be no problem about turning on the wrong lights. Here's the completed code:

program lightTest{

sensor opto on 2

event opto1 when opto is 0..18
event opto2 when opto is 18..34
event opto3 when opto is 35..51
event opto4 when opto is 52..68
event opto5 when opto is 69..85
event opto6 when opto is 86..100

main{

if opto is 0..18 {LED[27]=1}
if opto is 18..34 {LED[27]=3}
if opto is 35..51 {LED[27]=7}
if opto is 52..68 {LED[27]=39}
if opto is 69..85 {LED[27]=55}
if opto is 86..100 {LED[27]=63}

start light1
start light2
start light3
start light4
start light5
start light6
}

watcher light1 monitor opto1
{
LED[27] = 1
}

watcher light2 monitor opto2
{
LED[27] = 3
}

watcher light3 monitor opto3
{
LED[27] = 7
}

watcher light4 monitor opto4
{
LED[27] = 39
}

watcher light5 monitor opto5
{
LED[27] = 55
}

watcher light6 monitor opto6
{
LED[27] = 63
}
}

Now you can run around the house sticking your Spybot in all your homes darkest corners and brightest spots to see what sorts of readings you get.

 

This tutorial is protected by International Intellectual Property Rights laws and may not be reproduced or redistributed in full or part, without the prior written consent of the author. Unauthorized reproduction of this tutorial or its contents may result in prosecution.

 

 
 
DISCLAIMER: All content is provided as is, with no warranty stated or implied regarding the quality or accuracy of any content on or off this site. All trademarks, service marks, and copyrights are property of their respective owners. This site is not sponsored, authorized or sanctioned by the LEGO Group nor representative of their opinions in any way.PRIVACY POLICY