Skip to content

3. Compiling, Loading, and Running Sketches

Janata Lab edited this page Mar 31, 2022 · 3 revisions

A "sketch" is the program that runs on an Arduino. The Arduino .ino sketches must be compiled and loaded onto the Metronome and Tapper Arduinos. The sketch is automatically compiled when issuing an Upload command. Note that various warnings may be thrown during compilation.

NOTE: The setup() function of the sketch runs automatically upon sketch upload and whenever the Arduino is powered on or connected to on the serial (USB) bus.

Uploading the sketch

  1. Under Tools->Port, select the USB port to which the Arduino is connected. As noted below, it is helpful to have only a single Arduino connected to the USB bus at a time.
  2. Sketch->Upload (Command-U)

Tapper sketches

Note: In order for each Tapper Arduino to be identified properly, it is necessary to change the ADDRESS definition in Tapper.ino for each Tapper before uploading the sketch to the Tapper:

#define ADDRESS 4

The constants should range from 1 to 4.

TIP: To reduce the likelihood of error, it is wise to have only one Arduino visible on the USB bus at any given time. If the different tapper "stations" around the GEM box are sequentially numbered, it is advisable to upload the sketches in this order, connecting the corresponding Arduino to the USB bus at each step after having had disconnected the previous Arduino.

The setup() function

The setup() function is run automatically and immediately after:

  1. A sketch has been uploaded
  2. The Tools->Serial Monitor function (Shift-Command-M) is selected for the USB port that the Arduino is connected to
  3. The reset button on the Arduino is pressed

After the setup() function executes, execution of the loop() function commences.

Tapper setup()

The Tapper setup() function performs the following tasks:

  1. Attaches itself to the I2C bus (for receiving communications from the Metronome Arduino).
  2. Registers a callback function, receiveEvent(), for handling messages received from the Metronome Arduino.
  3. Sets the receive pin (for FSR input).
  4. Sets the send pin (for signaling the occurrence of a tap event to the Metronome Arduino).
  5. Initializes the sound card.
  6. Loads the sound

Metronome setup()

The Metronome setup() functions performs the following tasks:

  1. Starts communications on its Serial port
  2. Enables I2C (Wire) communications
  3. Iterates of the designated maximum number of tappers and performs:
    1. a handshake procedure to verify that it receives signals on the interrupt pin associated with that tapper
    2. registers the registerTap interrupt servicing routine (ISR) to the pin
  4. Initializes the sound card
  5. Loads the sound

Order of setting up (or turning on) the Tapper and Metronome Arduinos.

Because sketches execute immediately upon uploading of the sketch, the powering on of the Arduino, or the pressing of the Arduino's reset button, the Tapper Arduinos should be set up prior to the reseting the Metronome Arduino.

In practice, i.e. when controlling the Metronome Arduino from the Python GUI, the opening of the communication (Serial/USB) port will cause the Metronome Arduino to reset and run its setup() function.

NOTE: While each Arduino requires its own source of power via its serial/USB port, the tappers need not be connected to a USB device capable of communication in order for the GEM to function. Only the Metronome Arduino must be connected to an experiment control computer (ECC) that runs the Python code.

Testing and Debugging

Debugging messages are sent to the Serial port if the DEBUG variable is defined in GEMConstants.h. The DEBUG variable should be undefined in production mode. For additional information about debugging, see the dedicated debugging page.

Troubleshooting

Per the Arduino warning regarding global memory and instability, sketches can operate inconsistently if the amount of available memory is low. For reasons we do not completely understand, but probably having to do with how the sketches are stored in memory on any given upload, the sketches may not always function as expected. It seems that uploading a sketch twice can mitigate these intermittent issues.