Based on the Basic Structure of a Program and Coding Building Blocks pages.
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.
This program does absolutely nothing.
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();
}
void loop() {
}
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:
#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);
}
;
.()
.Before processing data, we have to tell the computer to store it in its memory. This is done using variables.
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:
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).
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.
The value stored in resulting_number
in this case would be 7
.
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:
Comments