Simple sequencer: ToneTest.ino

//
// A simple tone sequencer and generator
//

// Author:  P.J. Drongowski
//          http://sandsoftwaresound.net/
// Version: 1.0
// Date:    23 May 2016
// 
// Copyright (c) 2016 Paul J. Drongowski
//               Permission granted to redistribute and modify

// The Arduino tone() function may not play frequencies
// below NOTE_B2 (123 Hz) correctly due to hardware/software
// limitations.

#include "ToneFreq.h"
#include "ToneNote.h"

int tonePin = 5 ;

int noteIndex = 0 ;
int note = 0 ;
int duration = 0 ;

#define TEMPO   (90)
#define REST    -1
#define SEQUENCE 18
int sequence[SEQUENCE][2] = {
  NOTE_As2, EIGHTH, 
  REST,     SIXTEENTH, 
  NOTE_Gs3, SIXTEENTH, 
  REST,     EIGHTH, 
  NOTE_As3, EIGHTH,
  REST,     EIGHTH, 
  NOTE_C3,  EIGHTH, 
  NOTE_Cs3, EIGHTH, 
  NOTE_D3,  EIGHTH,
  NOTE_Ds3, EIGHTH, 
  REST,     SIXTEENTH, 
  NOTE_As3, SIXTEENTH, 
  REST,     EIGHTH, 
  NOTE_Cs4, EIGHTH,
  REST,     EIGHTH, 
  NOTE_G2,  EIGHTH, 
  NOTE_Gs2, EIGHTH, 
  NOTE_A2,  EIGHTH
} ;

void setup() {
  // Start playing the first note in the sequence
  // This note could be a pick-up!
  noteIndex = 15 ;
  note = sequence[noteIndex][0] ;
  duration = sequence[noteIndex][1] ;
  if (note != REST) {
    tone(tonePin, note) ;
  }
}

void loop() {
  delay(1) ;
  duration-- ;
  if (duration <= 0) {
    // Current note has expired.
    noTone(tonePin) ;
    // Find the next note to be played
    if (noteIndex >= (SEQUENCE-1)) {
        noteIndex = 0 ;
    } else {
        noteIndex += 1 ;
    }
    note = sequence[noteIndex][0] ;
    duration = sequence[noteIndex][1] ;
    // Start playing the next note
    if (note != REST) {
      tone(tonePin, note) ;
    }
  }
}