Spybots Smart Parts  
  The Unofficial Resource Centre for Lego Spybotic  
   
img  
   
   

TUTORIALS

TUTORIALS

BASIC

Lighting Up (Part 1)- nqc

Tutorial Details:
Difficulty Level: Basic
Topics Covered: Controlling ARC, ALERT and VLL lights using NQC.
Assumed Knowledge: THe Basics, My first program, Using Include
Written By: BILL LANE

BACK

In all the supplied missions for the Spybots the arc lights and the alert light are regularly used to provide mission information. They tell us how much time remains, how close we are to achieving the goal, if the enemy is near, etc, etc. In other words they are our robots main communication tool. As such it is important that we can take control of these lights and make them work for us. At the time of writing there is limited documentation about controlling these lights. But the code will work (I'm just not sure there isn't a better way to achieve the same results).

Controlling the LED's involves using SetLED(mode, value) which is defined in spy.nqh. As such you'll need to include this file at the top of your script before defining the main task (if this doesn't make any sense go have a look at Using Include and then come back).

Using SetLED requires that you supply two pieces of information (referred to as parameters); the mode and a value relevant for that mode. The first mode we'll consider is LED_MODE_ON (there are 15 modes in all and you'll find them all defined in spy.nqh). It will turn the red and green arc lights on, if we tell it which ones we want turned on. We do that by sending it a value. For example if we send 0x01 we will turn on the first red light. If we send it two or more values joined by a + sign we can turn on multiple lamps. Here's an example:

#include "spy.nqh"

task main()
{
SetLED(LED_MODE_ON, 0x01+0x02);
Wait (100);
}

Once again we're using Wait so we can actually see a result. When you run that you should see the first two red arc lights come on for 1 second. Below is a list of the required values for each arc llight. Numbers run from the edge to the middle of the Spybot.

Red 1 0x01
Red 2 0x02
Red 3 0x04
Green 1 0x08
Green 2 0x10
Green 3 0x20

Armed with that information let's have a look at another mode. If we want the lights to blink we can use LED_MODE_BLINK. For values we either tell it which lights to blink or send it 0 to stop blinking. Before we look at the code let's look at LED_MODE_DURATION. We use this to tell the spybot how long the blink should last. The value represents the duration in 100ths of a second. So let's see an example:

SetLED(LED_MODE_DURATION,50);
SetLED(LED_MODE_BLINK, 0x01+0x20);
Wait (100);

N.B. The Spybot has a default blink rate. But if you change the duration using this mode it will retain that blink rate until you change it in code or restart the Spybot.

In this example, the two outside lights will go on and off twice before the program ends. With that knowledge under your belt you might want to experiment with creating your own light sequences before we move on.

The next series of modes we'll look at are those involving the ALERT light. They are: LED_MODE_YELLOW, LED_MODE_YELLOW_BLINK, LED_MODE_YELLOW_DURATION and they work in a very similar fashion the first three we discussed. The biggest difference being that with only one alert light we can turn it on using 1 and off using 0. The following example turns tells the alert light to blink for 1 second:

SetLED(LED_MODE_YELLOW_DURATION , 20);
SetLED(LED_MODE_YELLOW_BLINK ,1);
Wait(100);

Add the following code and it will stay on for another second without blinking before turning off completely:

SetLED(LED_MODE_YELLOW_BLINK , 0);
SetLED(LED_MODE_YELLOW,1);
Wait(100);
SetLED(10,0);

We can also use this command to control the VLL ( the laser ) light in a very similar way. The relevant modes are; LED_MODE_VLL, LED_MODE_VLL_BLINK, LED_MODE_VLL_DURATION. There is one big difference here. If you don't expressly turn off the VLL it will remain on after the program ends. The other issue is that it's not enough just to tell it to stop blinking. So if you start it blinking you need to stop the blinking and then turn the light off. Here's the code:

SetLED(LED_MODE_VLL_DURATION,5);
SetLED(LED_MODE_VLL_BLINK,1);
Wait (100);
SetLED(LED_MODE_VLL_BLINK,0);
SetLED(LED_MODE_VLL,0);

So that's it you can now turn all the Spybot's lights on and off as often as you like. There is a different way of using SetLED that offers more control over which ARC lights do what. But it's a bit much for this tutorial. If you're ready for the next step take a look at Lighting Up (Part 2).

 

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