const int MIDI_CH = 1;
const int NUM_STEPS = 8;
unsigned long lastStepMs = 0;
int currentStep = 0;
int currentNote = -1; int readTempoBpm(){
int bpm = map(Element, 0, 1023, 40, 240);
return bpm;
} unsigned long bpmToStepMs(int bpm){
if (bpm < 20) bpm = 20;
if (bpm > 300) bpm = 300;
return 60000UL / bpm;
} void Sequencer(){
int bpm = readTempoBpm();
unsigned long stepMs = bpmToStepMs(bpm);
if (millis() - lastStepMs >= stepMs){
lastStepMs += stepMs;
sendStep(currentStep);
currentStep = (currentStep + 1) % NUM_STEPS;
}
} void sendStep(int step) {
int note = stepNotes[step];
MIDInoteRecieved = true;
RecievedMIDINote = note;
if (currentNote >= 0) {
usbMIDI.sendNoteOff(currentNote, 0, MIDI_CH);
}
usbMIDI.sendNoteOn(note, 100, MIDI_CH);
currentNote = note;
} case 3:
Sequencer();
break;