Back

Some things you need to know before coding your device.

Programming is case sensitive.

For instance, the variable type int has to be in all lowercase, otherwise you will get an error.

Also, you must end every line of code with a semicolon. (unless it has parentheses or brackets) For instance,

void setup(){ int x = 2; //there's a semicolon to end the line declaring the variable x. }

Variable Names

Variables can be named whatever you want them to be.

For instance, in all examples in Jewelbot tutorials, the LED object has always been declared as led. However, you can totally declare it as whatever you want!

Comments

Programmers will often put comments next to their code to help other users understand what their code does. All comments start with "//", and are ignored by the compiler, meaning they do not affect your code.

The Setup() function

The setup() function is called when a sketch starts. Use it to initialize variables, pin modes, etc. The setup function will only run once, after each power up or reset of the Arduino board. For example,

void setup() { LED led; // declaring an LED variable to be named "led" }

Conditional Statements

These are logical arithmetic statements you can implement into your program in order to control different functions or loops.

For instance, you can use these conditional statements to turn your Jewelbot into a calculator! Let's say you have variables called x and y.

You can set these variables to equal your math homework problems!

void setup(){ // declare the name of your LED object here. LED led; // declare the values of your variables x and y int x = (4*5+3)^2; int y = 20x - (8^2); }

Now you can use the conditional statements to test which variable is bigger!

If...then...

This is a conditional statement that performs an activity only if something specific happens.

if (x > y ) { led.turn_on_all(GREEN); } if (y < x ){ led.turn_on_all(BLUE); }

This block of code states that if the variable x is greater than y then your Jewelbot will light up purple on LED pin 1. The second conditional states that if the variable y is greater than x then your Jewelbot will light up red on LED pin 2.

If...else...

This is a conditional statement that performs an activity if something specific happens, but in any situation where that specific thing doesn't happen, then it will do something else.

if (x < y) { led.turn_on_all(RED); } else { led.turn_on_all(WHITE); }

This block of code programs your Jewelbot to turn the LED on pin 1 to light up purple if variable x is greater than y. Otherwise, your Jewelbot will turn the LED on pin 2 to light up red for all other conditions, without having to set up a conditional for if y is greater than x.

Loops

A loop is a sequence of instructions that continually repeats.

Loop()

The loop() function does precisely what its name suggests, and loops consecutively whatever you coded your program to do over and over again!

void setup() { // put your setup code here, to run once: LED led; } void loop() { // put your main code here, to run repeatedly: led.flash_single(SE, BLUE, 65000); // it will loop to have one LED be the color blue for 65000 milleseconds }

For loop

This is different from the if-then or if-else conditionals, as this will set a conditional that causes your program to loop itself a specific amount of times.

The for loop has a set of 3 parameters. A declaration of an int, a condition for running the loop, and an increment. For instance, a for loop could start as for(int z = 0, z < 10, z++). This means that there is a variable of z with the value of 0, that will increase by 1 for as long as it is less than 10. However, the value of z will only increment when the for loop completes one cycle of the steps you programmed within it.

for(int a = 2, a <=20, a++){ led.flash_single(NW, YELLOW, 1000); led.flash_single(NE, MAGENTA, 1000); }