     io_anyway = global_clock;
     io_anyway += MHZ * MAX_BUSY_US;
     tick = ARM_DWT_CYCCNT;
-    runlevel = GOGOGO;
-  }
-  else if (runlevel != ERRDONE && runlevel != ERRSTOP) {
-    error_with_message("Attempt to start running from invalid state.", "");
+    runlevel = RUN_GO;
+  }
+  else if (runlevel != RUN_ERROR && runlevel != RUN_TO_ERROR) {
+    error_with_message("Attempt to start running from invalid state.");
   }
 }
 


#define ANALOG_ZERO 2047
#define ANALOG_AMPL 2047
#define ANALOG_DIVS 16384

#define CHAN (DIG+ANA)


  Run a digital and/or analog stimulus protocol from a Teensy 3.1+ board.
 */

/*
Teensy Stimulus is essentially a set of simple state machines.

The _main loop_ has four persistent states and two transient states; these
are present in the variable `runlevel`.
Persistent states:
  RUN_ERROR - Indicates an error state.
    State entered whenever an invalid input is received.
    State exited only when explicitly reset with `~.` command
  RUN_PROGRAM - State for setting stimuli.
    Teensy starts in this state.  Reset `~.` moves to this state.  Refresh `~"` will also from RUN_COMPLETED.
    State exited when a run starts: `~*` or `~A*` or `~A:blahblah` (or error)
  RUN_COMPLETED - Indicates that a protocol is done running.
    State entered when running state is complete.
    State exited when reset `~.` or refreshed `~"` (to run same program again).
  RUN_GO - Protocol is running.
    State entered from RUN_PROGRAM with a run command `~*` `~A*` `~A:blahblah`
    State exited when stimulus is complete (goes to RUN_COMPLETED state)
Transient states:
  RUN_TO_ERROR - Was running, cooling down stimuli, will turn to error within a second or so.
  RUN_LOCKED - Setting up for something (probably a run).  Transient, very brief (~1 ms).

There is also a state machine that runs every digital channel.
These are stored in the Channel struct.  In addition to a runlevel (state),
there are also three separate timers.
  Global timer `t`: when this is exhausted, the channel is done running.
  Block transition timer yn: when this is exhausted, switch to the next on or off block (as appropriate)
  Pulse transition timer pq: when this is exhausted, turn the stimulus on or off (as appropriate)
There are four runlevels:
  C_ZZZ - Channel is sleeping.  Timers are irrelevant.
  C_WAIT - In an off block.  When yn is exhausted, turn on to C_HI.
  C_LO - In an on block, but stimulus is off.  Turn it on when pq is exhausted, or go back to C_WAIT if pq is exhausted.
  C_HI - Stimulus is on!  If pq exahausted, turn off and go to C_LO.  If yn exhuasted, turn off and go to C_WAIT.
  If t is ever exhausted, turn off stimulus and go to C_ZZZ.

There is a similar state machine for analog channels.  This is still under development, so isn't described.
*/


#include <EEPROM.h>
#include <math.h>

// Note: the Teensy 3.1 and 3.2 can be "overclocked" to 96 MHz, but 72 MHz is their operating speed.
// 72 MHz is plenty for our purposes.
#define MHZ 72
#define HTZ 72000000

// This is the hardware pin attached to the on-chip LED.
#define LED_PIN 13

// The digital channels supported; these correspond to letters 'A' through 'X', with the last being the LED pin.
#define DIG 24
int digi[DIG] = { 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };

// The analog channels supported; support is a fiction currently.
#define ANA 1
#define ANALOG_DIVS 4096d
int16_t wave[ANALOG_DIVS];


/******************************
 * Primary timekeeping struct *
 ******************************
 * 
 * Time is stored as seconds plus ticks, so it's dependent on the clock rate.
 * It's really intended that times will be used going forward, and that you mostly
 * want to check when one duration has passed another.
 *
 * All comparison operators could be defined, but I've only bothered with < right now.
 *
 * Only positive times are supported.
**/

struct Dura {
  int s;  // Seconds
