Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save EvanBurnette/a18ee65a395a471d146ecc6b30168acc to your computer and use it in GitHub Desktop.
Save EvanBurnette/a18ee65a395a471d146ecc6b30168acc to your computer and use it in GitHub Desktop.
#define BOUNCE_WITH_PROMPT_DETECTION
#include <SoftwareSerial.h>
#include <Bounce2.h>
#define midiIn 4
#define midiOut 5
#define backPin 14
#define playPin 15
#define forwardPin 16
#define undoPin 17
#define tapPin 18
#define ledPin 13
unsigned long lastTapFallTime;
unsigned long tapFallTime;
unsigned long tapElapsedTime;
unsigned long beatTime;
unsigned long pulseTime;
unsigned long firstBeat;
int pulseCount;
unsigned long nextPulse;
byte bankAndPatternByte;
byte bankBit;
byte patternByte;
#define debounceInterval 25
Bounce tap = Bounce();
Bounce play = Bounce();
Bounce forward = Bounce();
Bounce back = Bounce();
Bounce undo = Bounce();
bool playing;
SoftwareSerial midiSerial(midiIn, midiOut);
void setup() {
midiSerial.begin(31250);
Serial.begin(57600);
tap.attach(tapPin, INPUT_PULLUP);
tap.interval(debounceInterval);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
beatTime = 600000;
pulseTime = beatTime/24;
pulseCount = 0;
nextPulse = 0;
playing = false;
play.attach(playPin, INPUT_PULLUP);
play.interval(debounceInterval);
bankAndPatternByte = 128;
bankBit = 1;
patternByte = 127;
forward.attach(forwardPin, INPUT_PULLUP);
forward.interval(debounceInterval);
back.attach(backPin, INPUT_PULLUP);
back.interval(debounceInterval);
undo.attach(undoPin, INPUT_PULLUP);
undo.interval(debounceInterval);
}
void loop() {
tap.update();
if ( tap.fell() ) {
lastTapFallTime = tapFallTime;
tapFallTime = micros();
tapElapsedTime = tapFallTime - lastTapFallTime;
if (tapElapsedTime < 3000000){
if (beatTime > 0){
beatTime = (tapElapsedTime+beatTime)/2;
pulseTime = beatTime/24;
firstBeat = micros() + beatTime;
}
else {
beatTime = tapElapsedTime;
}
}
else {
beatTime = 0;
}
}
if (micros() >= nextPulse){
nextPulse = pulseTime + micros();
if (pulseCount == 0){
digitalWrite(ledPin, HIGH);
}
else if (pulseCount == 1){
digitalWrite(ledPin, LOW);
}
else if (pulseCount == 23){
pulseCount = -1;
}
else{}
pulseCount++;
if (playing){midiSerial.write(0xF8);}
else{}
}
else{}
play.update();
if ( play.fell() ){
if(playing){
midiSerial.write(0xFC);//send stop
}
else{
pulseCount = 0;
midiSerial.write(0xFA);//send start
}
playing = !playing;
}
else{}
forward.update();
if ( forward.fell() ){
bankAndPatternByte++;
bankBit = bankAndPatternByte>>7;
patternByte = bankAndPatternByte & 127;
write3(0xB9, 0, 0);
write3(0xB9, 0x20, bankBit);
write2(0xC9, patternByte);
}
else {}
back.update();
if ( back.fell() ){
bankAndPatternByte--;
bankBit = bankAndPatternByte>>7;
patternByte = bankAndPatternByte & 127;
write3(0xB9, 0, 0);
write3(0xB9, 0x20, bankBit);
write2(0xC9, patternByte);
}
undo.update();
if ( undo.fell() ){
write3(0xBF, 79, 127);
}
else {}
}
void write3(byte byte1, byte byte2, byte byte3){
midiSerial.write(byte1);
midiSerial.write(byte2);
midiSerial.write(byte3);
}
void write2(byte byte1, byte byte2){
midiSerial.write(byte1);
midiSerial.write(byte2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment