The Code:
unsigned long lastStepTime = 0;
int currentStep = 0;
int lastStep = 5;
int tempo = 0;
int ledPinArray[4] = {29, 30, 31, 32};
int ledPitches[4] = {0, 0, 0, 0};
int totalLeds = 4;
int switchPin1 = 34;
int switchPin2 = 33;
int switchPin3 = 27;
void setup() {
Serial.begin(9600);
for(int i = 0; i < totalLeds; i++){
pinMode(ledPinArray[i], OUTPUT);
}
pinMode(switchPin1, INPUT);
pinMode(switchPin2, INPUT);
pinMode(switchPin3, INPUT);
}
void loop() {
tempo = analogRead(A20);
readPitches();
if(digitalRead(switchPin3) == HIGH){
if(digitalRead(switchPin1) == HIGH){
stepBackwards();
}
else{
stepForwards();
}
}
else{
digitalWrite(ledPinArray[lastStep], LOW);
usbMIDI.sendNoteOff(ledPitches[lastStep] + 12, 0, 1);
usbMIDI.sendNoteOff(ledPitches[lastStep], 0, 1);
}
}
void readPitches(){
ledPitches[0] = map(analogRead(A19), 0, 1023, 60, 72);
ledPitches[1] = map(analogRead(A18), 0, 1023, 60, 72);
ledPitches[2] = map(analogRead(A17), 0, 1023, 60, 72);
ledPitches[3] = map(analogRead(A16), 0, 1023, 60, 72);
Serial.println(ledPitches[1]);
}
void stepForwards(){
if(digitalRead(switchPin2) == HIGH){
if(millis() >= tempo + lastStepTime){
digitalWrite(ledPinArray[lastStep], LOW);
usbMIDI.sendNoteOff(ledPitches[lastStep] + 12, 0, 1);
digitalWrite(ledPinArray[currentStep], HIGH);
usbMIDI.sendNoteOn(ledPitches[currentStep] + 12, 127, 1);
lastStepTime = millis();
lastStep = currentStep;
currentStep++;
}
if(currentStep == totalLeds){
currentStep = 0;
}
}
else{
if(millis() >= tempo + lastStepTime){
digitalWrite(ledPinArray[lastStep], LOW);
usbMIDI.sendNoteOff(ledPitches[lastStep], 0, 1);
digitalWrite(ledPinArray[currentStep], HIGH);
usbMIDI.sendNoteOn(ledPitches[currentStep], 127, 1);
lastStepTime = millis();
lastStep = currentStep;
currentStep++;
}
if(currentStep == totalLeds){
currentStep = 0;
}
}
}
void stepBackwards(){
if(digitalRead(switchPin2) == HIGH){
if(millis() >= tempo + lastStepTime){
digitalWrite(ledPinArray[lastStep], LOW);
usbMIDI.sendNoteOff(ledPitches[lastStep] + 12, 0, 1);
digitalWrite(ledPinArray[currentStep], HIGH);
usbMIDI.sendNoteOn(ledPitches[currentStep] + 12, 127, 1);
lastStepTime = millis();
lastStep = currentStep;
currentStep--;
}
if(currentStep < 0){
currentStep = 3;
}
}
else{
if(millis() >= tempo + lastStepTime){
digitalWrite(ledPinArray[lastStep], LOW);
usbMIDI.sendNoteOff(ledPitches[lastStep], 0, 1);
digitalWrite(ledPinArray[currentStep], HIGH);
usbMIDI.sendNoteOn(ledPitches[currentStep], 127, 1);
lastStepTime = millis();
lastStep = currentStep;
currentStep--;
}
if(currentStep < 0){
currentStep = 3;
}
}
}