Before we start looking at functions you can use with circuit playground, let's quickly overview some features of the Arduino IDE. At this point we are assuming you already installed the IDE and the 216 library described at Circuit Playground.
Before you continue, you should take a look at at the device components description found on page 9 of Components. (under the title "Guided Tour"). The examples below rely on several elements described in the reading (e.g., "ON" LED (green color), pin 13 LED, left Button, right button, slide switch, reset button, neopixels). A neopixel is an RGB LED that can be set to any color of the rainbow. The pin 13 LED does double duty. It pulses nicely when the device is in bootloader mode, and it can also be used as an indicator LED (many programs blink this LED to prove programming worked). The small button in the center of the board is for resetting the board. You can use it to restart or reset the device. Press this button once to reset, double-click to enter the bootloader manually.
Every sketch has two functions: setup() and loop(). The setup() function is called when a sketch starts. Use it to initialize variables, pin modes, start using libraries, etc. The setup() function will only run once, after each powerup or reset of the Arduino board. A frequently used function is delay(). It stops execution for a particular number of milliseconds (e.g., delay(1000) will stop for one second).
In cmsc216, the TwoSixteen.zip library provides support for output (e.g., printf) and for neopixel handling. A template of any sketch used in the class is template_sketch.ino. To download the file, select download template_sketch.ino.
To use printf in your programs, you will need to include stdio.h and serial_interface.h. In addition, you need to call the function init_serial_stdio() in setup(). Clicking on the magnifying glass in the upper right corner of the IDE will open a window displaying the output generated by printf. . The printf.ino sketch illustrates how we can use printf and delay. To download the file select download printf.ino.
The pinMode function configures a pin to behave either as input or output. Notice that pins default to input. The parameters for this function are a pin number and a mode (INPUT, OUTPUT). In example blink_printf.ino, you can see how in the setup function we set pin #13 (red LED) as output. To download the file select download blink_printf.ino.
The digitalWrite function writes a HIGH or LOW value to a digital pin. If the pin has been configured as OUTPUT, its voltage will be set to a high value (e.g., 5V or 3.3V) or 0V for LOW. By writing a HIGH value you can turn on the red LED. In blink_printf.ino, we make the red LED blink by writing a HIGH and LOW to the pin.
The digitalRead function allows you to read the state of a pin returning HIGH or LOW. In the device, pin 4 is associated with the left button. We can use digitalRead to recognize whether this button is pressed (HIGH) or not (LOW). In example digitalRead.ino we first set the pin mode for the left button to INPUT ( pinMode(push_button, INPUT)). We can recognize whether it has been pressed by using digitalRead(push_button). In the example, the value returned by digitalRead is used as input for digitalWrite, allowing the button to turn the red LED on and off. To download the file, select download digitalRead.ino.
We can recognize whether the switch (pin number 21) is on (left position) or off (right position) by using pinMode(21, INPUT) and digitalRead(21). Example switch.ino illustrates how the red LED turns on when the switch is moved to the left and off when moved to the right. To download the file, select download switch.ino.
In order to interact with neopixels (RGB LEDs), we need to include the file neopixel_blit.h and call the function init_neopixel_blit() in setup(). The library defines a type, Pixels, that represents all the neopixels (10) of the device. Pixels is a two-dimensional array in which the pixels are numbered zero through 9. In addition to some initialization, the init_neopixel_blit() function clears (turns off) all the neopixels. Do not use the function outside of setup(). By the way, blit is a computer graphics term that refers to a data operation that combines several bitmaps.
To interact with the neopixels, define a variable of type Pixels (e.g., Pixels pix) and access each pixel by indexing the variable. For example, pix represents the neopixel that is closest to the on (green) LED; pix represents the neopixel to the left of pix, and so on.
To set the colors of a neopixel specify the amount (value between 0 and 255) of red, green and blue that you would like each neopixel to have, and call the function neopixel_blit(). The constants PIX_RED, PIX_GRN, PIX_BLU provide access to each of these color components. For example, to set a neopixel to white:
pix[PIX_RED] = 10; pix[PIX_BLU] = 10; pix[PIX_GRN] = 10; neopixel_blit(pix);
Although you could use a value up to 255, you should use 30 at most. By the way, it takes about 300 ms to send colors to the neopixels. The neopixel.ino example illustrates how to turn on the neopixels of the device, settting pix to red. To download the file, select download neopixel.ino.
Some terms that can help understand your device are defined in the Arduino Glossary. You might want to search for microcontroller, flash memory, bootloader, firmware, toolchain, accelerometer, actuator, baud.