Code:
int ledPin[4] = {29, 30, 31, 32};
int buttonPin[4] = {33, 34, 35, 36};
bool switchedOn[4] = {false, false, false, false};
bool buttonState[4] = {LOW, LOW, LOW, LOW};
bool lastButtonState[4] = {LOW, LOW, LOW, LOW};
int tempo = 0;
int mappedPotValTempo = 0;
int currentStep = 0;
unsigned long lastStepTime = 0;
void setup() {
for (int i = 0; i < 4; i++) { //use a for loop to set all LEDs and button pins
pinMode(ledPin[i], OUTPUT);
pinMode(buttonPin[i], INPUT);
}
}
void loop() {
checkButtons();
updateLeds();
stepForwards();
}
void checkButtons() {
for (int i = 0; i < 4; i++) {
lastButtonState[i] = buttonState[i];
buttonState[i] = digitalRead(buttonPin[i]);
if(lastButtonState[i] == LOW && buttonState[i] == HIGH) {
flipbuttonstate(i);
delay(5);
} else if(lastButtonState[i] == HIGH && buttonState[i] == LOW) {
delay(5);
}
}
}
void flipbuttonstate(int i) { //for organization
if(switchedOn[i]== true){
switchedOn[i]= false;
}
else if(switchedOn[i]== false){
switchedOn[i]= true;
}
}
void updateLeds() {
for (int i = 0; i < 4; i++) {
if(switchedOn[i] == true or i == currentStep) { //so that it blinks if the button isn't pushed and stays lit if it is
digitalWrite(ledPin[i], HIGH);
} else {
digitalWrite(ledPin[i], LOW);
}
}}
void stepForwards() {
tempo = analogRead(A19);
if (millis() >= lastStepTime + tempo) { //tell Teensy when to start running the function with millis()
lastStepTime = millis();
usbMIDI.sendNoteOff(40, 0, 1);
currentStep++; //increase counter
if (currentStep == 4) {
currentStep = 0; //reset counter
}
if (switchedOn[currentStep] == true) {
usbMIDI.sendNoteOn(40, 127, 1);
}
}
}
Video: