Programming the SpinWheel

Based on the Basic Structure of a Program and Coding Building Blocks pages.

What does a computer program look like?

Approach this page the way you would approach the first few lines of a foreign language you want to learn. Try to pick out words that make sense, without worrying about the overall structure, correct syntax, or proper grammar. As time passes and you have learned new things, come back to this page and see whether you can understand a bit more of it.

The Skeleton of an Arduino Program

void setup() {

}

void loop() {

}

This program does absolutely nothing.

When you turn on the SpinWheel, setup() is run once and then the loop() block is run repeatedly until the SpinWheel is turned off.

Extra Elements of a SpinWheel Program

// Include extra resources and commands
// specific to the SpinWheel.
#include "SpinWearables.h"
using namespace SpinWearables;

void setup() {
  // Instruct the specific SpinWheel hardware
  // to be ready to receive instructions.
  SpinWheel.begin();
}

void loop() {

}

Make it light up:

#include "SpinWearables.h"
using namespace SpinWearables;

void setup() {
  SpinWheel.begin();
}

void loop() {
  // Set the large LED number 0 to
  // have red=255, green=0, and blue=0 color.
  SpinWheel.setLargeLED(0, 255, 0, 0);
  // Update the currently shining LEDs
  // based on the instructions provided up to here.
  SpinWheel.drawFrame();
}
SpinWheel LED numbering.
void loop() {
}

Creating changing patterns

To begin writing more complex code, we need to introduce the idea of variables. Variables allow us to store information in the program and change that information as needed.

To define a new variable we can add the following line to the loop block:

int which_LED = 1;
#include "SpinWearables.h"
using namespace SpinWearables;

void setup() {
  SpinWheel.begin();
}

void loop() {
  int which_LED = 1;
  SpinWheel.setLargeLED(which_LED, 255, 0, 0);
  SpinWheel.drawFrame();
}
#include "SpinWearables.h"
using namespace SpinWearables;

void setup() {
  SpinWheel.begin();
}

// We have to declare which_LED outside of the loop to change it every loop
int which_LED = 0; 

void loop() {

  // clear all LEDs
  SpinWheel.clearAllLEDs();
  
  // Turn on 1 LED
  SpinWheel.setLargeLED(which_LED, 255, 0, 0);
  SpinWheel.drawFrame();
  
  // change the LED to use
  which_LED = which_LED + 1;
  
  // pause the code to delay running the next loop
  delay(500);
  
}

Additional Programming Notes

  • Only one “command” per line. This makes the code more readable.
  • Command are followed by a semicolon ;.
  • Commands tend to be some name followed by parentheses ().
  • Inside these parentheses we put some extra information: this information can control how a command performs.

Building Blocks for your Program

Variables

Before processing data, we have to tell the computer to store it in its memory. This is done using variables.

“Variables” are like labeled shelves for information. When you need to save a number for later use, you put it in a variable of your choice. Above we have number 3 stored in the variable a, number 4 stored in variable b and the number 7 is about to be stored in a variable named e. We can pick the names for the variables ourselves (any sequence of letters that is not interrupted by whitespaces). image credit Mariya Krastanova

We will only discuss two types of variables: integers and decimals. Other types do exist, but we won’t cover them here.

To define a new integer variable you need the following line in your code:

int my_special_integer = 6;

If we want to work with decimals, we use the variable type float instead of int. The name comes from the early history of computers and is unimportant for our purposes (how the decimal point can “float” between the digits).

float pi = 3.1415;

Functions

In computer programming, functions are commands that take a few variables and do something useful with them. Functions are reusable pieces of code.

Here is some code that uses an example function called max that takes two numbers as input and returns the larger number. The input values are also called the arguments of the function.

int number_a = 5;
int number_b = 7;
int resulting_number = max(number_a, number_b);
int number_a = 5;
int number_b = 7;
int resulting_number = max(number_a, number_b);

The value stored in resulting_number in this case would be 7.

Functions are tools provided in a given programming language that are capable of ingesting a number of parameters and producting (a.k.a “returning”) some new value that depended on the input parameters. image credit Mariya Krastanova
int number_a = 5;
int number_b = 7;
int result = max(sqrt(number_a-1) * 2, min(number_b, 2));

Creating your own functions

Now we have the pieces to begin to create our own functions. In the beginning, this may be like creating a poem using a limited set of magnetic word tiles. With practice, your bank of pieces will increase in size, enabling you to write more complicated code. image credit Mariya Krastanova
float x = 3.5;
float y = 2.5;
float resulting_number = avg(x, y);

In this code example, resulting_number will have the value of 3.0.

To define this new function, we need to write down its name, together with the type of data it will be producing, followed by a set of computational instructions:

float avg(float first_argument, float second_argument) {
  return 0.5*(first_argument+second_argument);
}

Functions that do not return values

a = calculation1();
delay(1000);
b = calculation2();

Comments

// I am writing this comment to remind me
// that the next line stores the number 5
// in the variable named my_test_variable.
int my_test_variable = 5;
// reveal.js plugins