Jewelbots Tutorials Lesson 3: Hello World explained, part 2

Now, let’s dig into the two lines of code you wrote in the loop function.

void loop() {
// put your main code here, to run repeatedly:
LED led;
led.turn_on_single(NE, BLUE);
}

In many programming languages, there are things called “objects”. Objects are a way of grouping together data and functions that are somehow related to each other. For example, a cat object holding information about the cat named Whiskers might contain data representing Whiskers’s name and age along with functions representing how Whiskers would meow or chase a mouse.

Objects are specified using something called a “class”.

You can think of a class as a blueprint. When a house is being built, a blueprint specifies exactly how the house will be designed; however, a blueprint is not an actual house. In a similar way, classes specify which data and functions an object will have. Everything that is in an object is specified by its class. In our cat example, the cat object Whiskers is an instance of the class called Cat.

You won’t be writing any new classes when you program your Jewelbot, but you will be using objects made from classes the Jewelbots creators wrote. One such class is the LED class. This class contains data and functions related to the LEDs on the Jewelbots. Specifically, all of the functions in the LED class turn on and/or turn off one or more LEDs on a Jewelbot. In order for us to use these functions, we must make an LED object.

The following code from our Hello World program does this for us:

LED led;

The first LED (written in all caps) tells the program that our new object will be coming from the LED class. The second led (written in lowercase) is what we’ll call the “object name” — it’s what we want to call the specific LED object we’re making.

To help better explain the class name and object name relationship, let’s go back to the “Cat” class example. If we wanted to make an object representing the cat Whiskers, we would write

Cat whiskers;

We could make another Cat object with another name (say, “Pumpkin”) by writing:

Cat pumpkin;

Now we have two unique objects, with the same specifications given by the Cat class, but with different names.

We have just seen that we can make multiple objects of the same class with different names. It follows from this example that it doesn’t matter what an object’s name is, as long as it has some name that can be used to identify it. We can verify this by going back to our original Hello World program and changing the two places where it says led (in lower case) to any other word we want. Let’s try calling it light:

void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
LED light;
light.turn_on_single(NE, BLUE);
}

After making this change and loading the new program onto our Jewelbots, everything should work the same way as it did before!

The object name is what we will use to access all of the functions specified in that object’s class. Since the turn_on_single function is defined as a part of the LED object, if we want to use the function and make an LED light up, we write the name of the object, and then a dot, and then turn_on_single:

led.turn_on_single

But that’s not all! Now Jewelbot knows it has to turn on an LED. But which LED should it turn on, and what color should it be? We write these as arguments to the function — they are put between parentheses at the end of turn_on_single.

led.turn_on_single(POSITION_OF_LED, COLOR_OF_LED);

You don’t actually write POSITION_OF_LED and COLOR_OF_LED — you’ll replace that with which LED you want to turn on, and which color you want it to be.

The four LEDs on the Jewelbot are named by their positions, expressed as cardinal directions when the Jewelbot is held with the micro-USB port facing down. So if you turn your Jewelbot so the micro-USB port is down, the one in the upper right is the northeast (NE) one, the bottom right is the southeast (SE), bottom left is the southwest (SW), and upper left is the northwest (NW). In the diagram below, the Jewelbot is shown with the USB port (the thick black line) on the right side — as it might be when you are wearing the Jewelbot on your wrist.

The Jewelbot LEDs can light up in seven colors: red, green, blue, yellow, magenta, cyan, and white. When you specify the color in the turn_on_single function, you write its name in all caps. That’s the way the Jewelbot will understand the name of the color.

Try turning on different LEDs, with different colors!

For example:

led.turn_on_single(NW, GREEN);
led.turn_on_single(SE, MAGENTA);

You can just put one of these statements at a time in the loop() function, or you can put more than one in to make multiple LEDs light up at the same time.

In the next lesson we’ll talk about how to turn off an LED after a certain period of time.