I assume this comment is because I didn't include the entire sketch. Here 'tis, thx.
/* SEQ8 - An analog-style 8 step MIDI note sequencer
NG Elec, 2014
Analog inputs:
A0 = Velocity
A1 = Gate
A2 = Octave
A3 = Note
A4 = Tempo
Digital I/O:
1 (tx) = MIDI output, pin 5
2 = channel select 0 (S0)
4 = channel select 1 (S1)
7 = channel select 2 (S2)
8 = Tempo LED output
9 = Start/Stop button input
12 = Button input
13 = LED channel output
MIDI out connections:
pin 2 = ground
pin 5 = serial out
pin 4 = 220 Ohm to +5V
Delay between 8th notes:
bpm = ((bpm_max-bpm_min)*x/1023) + bpm_min
delay_msec = 30000/bpm;
Gate Time:
x = Gate value: 0 to 1023
max gate time = delay_msec
gateTime = ((delay_msec-1)x/1023) + 1;
MIDI clock out uses 24 pulses per quarter note:
clockTick = 2*delay_msec/24;
*/
#include <MIDI.h>
// pin assignments
int S0 = 2;
int S1 = 4;
int S2 = 7;
int TempoLED = 8;
int StartButton = 9;
int button = 12;
int LEDpin = 13;
// variables
int bpm_max = 500;
int bpm_min = 5;
int Velocity = 0;
int Gate = 0;
int Octave = 0;
int Note = 0;
int Tempo = 0;
long delay_msec = 0; // From the tempo knob, the time between 8th notes
long previousMillis = 0;
long previousTempoMillis = 0;
long previousTriggerMillis = 0;
long previousNoteOnMillis = 0;
long previousClockMillis = 0;
int LEDdelay = 200; // blink time in msec
int clockTick = 0; // time between 1/24th of a quarter note
int gateTime = 0; // time between note ON and OFF
int channel = 0;
void setup ()
{
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(TempoLED, OUTPUT);
pinMode(StartButton, INPUT);
pinMode(button, INPUT);
pinMode(LEDpin, OUTPUT);
Serial.begin(9600);
MIDI.begin();
}
void loop() {
unsigned long currentMillis = millis(); // get the timestamp in msec
// Read the Tempo pot and adjust the time between 8th notes, and clock ticks
Tempo = ((bpm_max-bpm_min)*float(analogRead(A4)/float(1023))) + bpm_min;
delay_msec = 30000/Tempo;
clockTick = 2*delay_msec/24;
// Is it time to turn on the tempo LED (top of the measure)?
if(currentMillis - previousTempoMillis > 8*delay_msec) {
previousTempoMillis = currentMillis;
digitalWrite(TempoLED, LOW);
}
// Is it time to turn the tempo LED off?
if (currentMillis - previousTempoMillis > LEDdelay) {
digitalWrite(TempoLED, HIGH);
}
// Is it time to send a MIDI clock signal?
if (currentMillis - previousClockMillis > clockTick) {
// send MIDI clock
MIDI.sendRealTime(Clock);
}
// Is it time to turn the button light off?
if (currentMillis - previousTriggerMillis > LEDdelay) {
digitalWrite(LEDpin, LOW);
}
// Is it time to turn the MIDI note OFF?
if (currentMillis - previousNoteOnMillis > gateTime) {
MIDI.sendNoteOff(42, 0, 1);
}
// Continue if start/stop is on
if (digitalRead(StartButton) == HIGH){
// Is it time to turn on a note?
if(currentMillis - previousTriggerMillis > delay_msec){
previousTriggerMillis = currentMillis; // reset the last time a trigger happened
// Write the channel selection to the muxs
setMUX(channel);
// Read the voltages from the knobs and switches
Velocity = analogRead(A0);
Gate = analogRead(A1);
gateTime = ((delay_msec-1)*Gate/1023) + 1;
Octave = analogRead(A2);
Note = analogRead(A3);
// Turn on the LED
digitalWrite(LEDpin, HIGH);
// Play the note, if the button is pressed
if (digitalRead(button)) {
MIDI.sendNoteOn(42, 127, 1);
previousNoteOnMillis = currentMillis; // reset the last time a note ON happened
}
// Increment the Channel, or roll over to zero
if (channel < 8){
channel = channel + 1;
}
else {
channel = 0;
}
}
}
else {
channel = 0; // reset the channel to zero, so the sequence starts at channel zero when start/stop is pressed
}
}
void setMUX(int ch){
digitalWrite(S0, (ch >> 0) & 1);
digitalWrite(S1, (ch >> 1) & 1);
digitalWrite(S2, (ch >> 2) & 1);
}