CODE

Hello World!

Before diving into reprogramming, let's get started with a simple CODE Hello World program.

We will be reprogramming the Flashlight Cubelet, so first, connect a Flashlight Cubelet to your Bluetooth Cubelet and remember to connect a Battery Cubelet. Next, (if you haven't already) launch the CODE programming environment in your browser on your PC or mobile device and follow the steps below.

1. Select a Cubelet

In the Network Graph select the Flashlight Cubelet.


2. Load the default code

In the Cubelet Information Area, select 'View Default Code'. The Code Window will appear, showing the Flashlight Cubelet's factory-installed, or 'default', program. Remember, it has a setup() and a loop() function. Notice that the loop() function has two subroutines: think(), and act (). The Flashlight Cubelet's program doesn't have a sense() subroutine because it is an Action Cubelet and cannot sense anything.


3. Modify default code

Now we'll make a tiny change to the Flashlight Cubelet's default code. We will cause the flashlight to turn on at full brightness once it has been flashed. This isn't a very exciting program; in fact, you'd probably never want to write this program, but it's the simplest example we could think of to show you how to use CODE.

First, find the line of code that sets the Flashlight's value to its block value. This line is in the act() routine, Modify this line to set the Flashlight's block value to a number. In the case below, we are setting it to it's maximum value (255).


4. Check the program

Before the CODE programming environment will flash your program to the Cubelet, it must first check it for syntax errors. To do this, press the Check button and wait for CODE to check your program. If CODE reports any errors, fix the problems and try again.


5. Flash the program

The next step is to send your program to your Cubelet. Make sure your Flashlight Cubelet is still selected in the Network Graph, and then press the Flash button. A progress bar will appear below the code window. When flashing is finished CODE will report success or failure. If flashing succeeded, your program is installed in the Cubelet and you can go on to the next step and test it. If flashing failed, your program did not get installed and you'll need to figure out why, fix the problem, and try again. The most likely problem is that your Battery Cubelet has low power and its batteries need to be charged.


6. Verify

Check the Flashlight Cubelet's behavior: make that it is functioning the way you wanted it to. In this simple example, the flashlight should now always be illuminated, regardless of what Sense Cubelets are attached.


Next

Now that you know something about CODE and written your first program, let's explore the syntax so we can move on to more complex programming.

(Before you put your Cubelets away, you might want to reprogram your Flashlight block to its original behavior; otherwise the next time you use it you might wonder why it stays on all the time!)


Continue to Basic Syntax.