General info "cheat sheet" - BleepLabs/Arduino-Light-And-Sound GitHub Wiki

The Arduino language reference

Documentation on all of the functions, variables, and structural elements of the language.

Functions:

One of the first functions we will be using is digitalWrite.
digitalWrite(pin, value);
We don't need to know all the work that goes in to actually making this happen in the chip. We just have to give the function the pin number that we want to change and what we want to change it to.

value = analogRead(pin);
This function takes the pin number as an input an outputs the reading from that pin. Here it is stored in the variable "value"

current_time = millis();
The millis function returns how many milliseconds have elapsed since the device was turned on. We don't give it any information, it just outputs a number.

Shortcut keys:

Ctrl / Option + t = Format text with nice spaces and tabs
Ctrl / Option + u = Compile and upload to teensy

Bits and Bytes, Reads and Writes

1 bit can represent 2 values, 0 and 1
2 bits can describe twice as many values as 1 bit: 0,1,2,3
Each time you add a bit you can describe twice as many values.
2^2=4
2^3=8
2^4=16
2^8=256
2^32=4,294,967,296

The standard analogWrite “bit depth” is 8 bits (256 values)
The standard analogRead “bit depth” is 10 bits (1024 values)
We can use the following lines in our code setup have both analogRead and Write be 12 bits(4096 values)

analogReadResolution(12); // 0-4095 range
analogWriteResolution(12); // 0-4095 range

While were in setup we should add this line to make our analogReads less noisy analogReadAveraging(64);

Variables

At the beginning of our sketch we have the declarations section Here we set aside spaces in RAM to put numbers in. We give this space a name and select a type of variable.

Use unsigned long or uint32_t for huge numbers like time. 0 - 4,294,967,296
unsigned long = current_time;

Use int aka int16_t for most things where you need just whole numbers -32,768 to 32,767
byte can only hold values from is 0-255 because its just 8 bits
float is for decimal values and can represent numbers in the billions but is slower to use.

There are several ways of setting a variable as 1 or 0:
1 = HIGH = true
0 = LOW = false

If you want something to be a float you need to be sure the variable you're storing the result in is a float. If it's a whole number, put a .0 after it to be explicitly that you want to do floating point multiplication or division, as in the kind that can have examiners.
For example if you say test = 4.0 * 3.1 and test is an int, test will be equal to 12. If test was a float it would be 12.4

You can have an arrays of variables which can give you the power to do operations on lots of numbers at once or keep things conceptually grouped.
Her's an array of all the frequencies for each note of the piano
const float chromatic[88] = {55.00000728, 58.27047791, 61.73542083, 65.40639999, 69.29566692, 73.4162017, 77.78175623, 82.40690014, 87.30706942, 92.49861792, 97.99887197, 103.8261881, 110.0000146, 116.5409558, 123.4708417, 130.8128, 138.5913338, 146.8324034, 155.5635124, 164.8138003, 174.6141388, 184.9972358, 195.9977439, 207.6523763, 220.0000291, 233.0819116, 246.9416833, 261.6255999, 277.1826676, 293.6648067, 311.1270248, 329.6276005, 349.2282776, 369.9944716, 391.9954878, 415.3047525, 440.0000581, 466.1638231, 493.8833665, 523.2511997, 554.3653352, 587.3296134, 622.2540496, 659.2552009, 698.4565551, 739.9889431, 783.9909755, 830.6095048, 880.0001162, 932.3276461, 987.7667329, 1046.502399, 1108.73067, 1174.659227, 1244.508099, 1318.510402, 1396.91311, 1479.977886, 1567.981951, 1661.219009, 1760.000232, 1864.655292, 1975.533466, 2093.004798, 2217.46134, 2349.318453, 2489.016198, 2637.020803, 2793.82622, 2959.955772, 3135.963901, 3322.438019, 3520.000464, 3729.310584, 3951.066931, 4186.009596, 4434.92268, 4698.636906, 4978.032395, 5274.041605, 5587.652439, 5919.911543, 6271.927802, 6644.876037, 7040.000927, 7458.621167, 7902.133861,8372.019192};
First off it's way easier to define with an array father than:
note_A0=55.00000728;
note_A#0=58.27047791;
note_B0=61.73542083;
...
Instead we can say note=chromatic[note_select] and the frequency of the note at the note_select position will be stores in note

Memory

The Teensy 3.2 has 256k Bytes of program space storage. This cannot be changed by the device. It’s where you upload the code to.
It has 64k Bytes of RAM. This is where we store variables. It all goes away when the device resets

An int is 2 Bytes, a float or unsigned long is 4. It can fill up very quickly with large arrays or delay banks.
You can read and write to SD cards for storage but just like on a computer it’s slower than RAM or program space.

There is also 2k Bytes of EEPROM which can be used to store data that will be retained while the device is turned off.