Sample Code for Friendship (Sample Two)

Here is a coding sample of how to use Friendship Coding to do something cool when your Jewelbot sees your red friends for the FIRST TIME (meaning the initial starting connection of being near a red friend). You can change this code to do different things for any and all color friends! Keep in mind that if you were to have code for two different friend colors AND you’re nearby both friend colors, then it will try to do both actions at the same time. This may cause animations to happen in a pattern or the color of the lights to blend.

Animation animation; void setup() { // put your setup code here, to run once } // Declare Jewelbots hardware components to use Buzzer buzz; Timer timer; Animation animation; // Declare booleans which control when you first see a friend of a particular color bool first_time_red = true; void loop() { // Red friend group: if ((see_red_friends()) && (first_time_red)) { buzz.short_buzz(); animation.rainbows(); buzz.short_buzz(); timer.pause(500); first_time_red = false; } else if ((!see_red_friends()) && (!first_time_red)) { first_time_red = true; } }

Note that a boolean is a “true or false” variable and that the && symbol represents “both must be true”.

So in the beginning, you declare a variable named “first_time_red” as true. And so, in the line “if((see_red_friends()) && (first_time_red))” you are saying that the Jewelbot must see red friends and the variable “first_time_red” must both be true. When that is the case, this code will do a buzz and rainbow animation, and at the end, set “first_time_red” to false.

Now, it can’t do a forever buzzing and rainbow loop until the Jewelbot no longer sees red friends. This is declared by the line “else if ((!see_red_friends()) && !(first_time_red))”. The exclamation in front symbolizes “false”. When the Jewelbot doesn’t see the red friends, then the function, “see_red_friends” is false. And because we set “first_time_red” as false in the “if” section, it is also false. These two false statements trigger the “else if” event to declare “first_time_red” as true again. Now, when your Jewelbot sees red friends again, it will do the buzzing and rainbows again.

After pasting the code into Arduino...

1. Make sure your Jewelbots is plugged in to your computer

It is suggested that the original USB that came with your Jewelbots be used.

2. Select the correct Jewelbots Board and Port.

To select a Jewelbots Board: Go to Tools > Boards and scroll down to select "Solo Coding Mode".
To select the port: Go to Tools > Port. Windows should have "COMXX". Mac should have /dev/usberial-xxxx. (The X's representing numbers)

3. Press the magic button for 2 seconds to put your Jewelobts in Uploading Mode

Make sure it is 2 full seconds (1 mississippi...).
You should see a fading of purple lights, which represents the Jewelbots entering Uploade Mode.

4. Press the Upload Button on the Arduino IDE

It is the arrow button in the top left.

5. Unplug your Jewelbots to see the results!

When it says "Done Uploading", you can unplug your Jewelbots to see the code in action.
The act of your Jewelbots running custom code is called "Coding Mode".

Test out more code combinations

Try using different colors! (RED, BLUE, GREEN, WHITE, CYAN, YELLOW, MAGENTA)
Try using different animations too!

Share what you code for your Jewelbots

You can post your code creations on the Jewelbots Forums!