int buttonPin[4] = {33, 34, 35, 36};
int ledPin[4] = {7, 8, 9, 10};
int totalLedPins = 4;
int totalButtonPins = 4;
int tempoPot = A13;
int tempoValue = 0;
int slideSwitch = 31;
unsigned long lastStep = 0;
int currentStep = 0;
int totalSteps = 4;
bool lastButtonState[4] = {LOW, LOW, LOW, LOW};
bool buttonState[4] = {LOW, LOW, LOW, LOW};
bool switchedOn[4] = {false, false, false, false};
void setup() {
for (int i = 0; i < totalLedPins; i ++) {
pinMode(ledPin[i], OUTPUT);
}
for (int i = 0; i < totalButtonPins; i ++) {
pinMode(buttonPin[i], INPUT);
}
pinMode(slideSwitch, INPUT);
}
void loop() {
if (digitalRead(slideSwitch) == LOW) { //stepForwards
checkButtons();
updateLeds();
stepForwards();
}
if (digitalRead(slideSwitch) == HIGH) { //stepBackwards
checkButtons();
updateLeds();
stepBackwards();
}
}
void checkButtons() { //check the status of the button
for (int i = 0; i < totalButtonPins; i++) {
lastButtonState[i] = buttonState[i];
buttonState[i] = digitalRead(buttonPin[i]);
if (lastButtonState[i] == LOW && buttonState[i] == HIGH) {
switchedOn[i] = !switchedOn[i]; // this means: if switchedOn is true then set it to false, if switchedOn is false then set it to true
delay(5);
} else if (lastButtonState[i] == HIGH && buttonState[i] == LOW) {
delay(5);
}
}
}
void updateLeds() { //update the LEDs based on the status of the button
for (int i = 0; i < totalLedPins; i++) {
if (switchedOn[i] == true) {
digitalWrite(ledPin[i], HIGH);
} else {
digitalWrite(ledPin[i], LOW);
}
}
digitalWrite(ledPin[currentStep], HIGH); //ensure that the LED of the current step is always HIGH (results in consistent blinking as the steps are raised or lowered)
}
void stepForwards() { //step forwards, and send a MIDI note for each engaged step
tempoValue = analogRead(A13);
if (millis() > lastStep + tempoValue) {
lastStep = millis();
usbMIDI.sendNoteOff(44, 127, 1);
countUp();
if (switchedOn[currentStep] == HIGH) {
usbMIDI.sendNoteOn(44, 127, 1);
}
}
}
void stepBackwards() { //step backwards and send a MIDI note for each engaged step
tempoValue = analogRead(A13);
if (millis() > lastStep + tempoValue) {
lastStep = millis();
usbMIDI.sendNoteOff(44, 127, 1);
countDown();
if (switchedOn[currentStep] == HIGH) {
usbMIDI.sendNoteOn(44, 127, 1);
}
}
}
void countUp() { //countUp function (for stepForwards)
currentStep++;
if (currentStep == totalSteps) {
currentStep = 0;
}
}
//countDown function (for stepBackwards)
void countDown() {
currentStep--;
if (currentStep < 0) {
currentStep = 3;
}
}