Video:
Code:
int ledPin[4] = {29, 30, 31, 32};
int channelLeds[3] = {18, 17, 16};
int buttonPin[5] = {33, 34, 35, 36, 27};
int tempo = 0;
int mappedPotValTempo = 0;
int currentStep = 0;
bool buttonState[5] = {LOW, LOW, LOW, LOW, LOW};
bool lastButtonState[5] = {LOW, LOW, LOW, LOW, LOW};
bool switchedOn[3][4] = { //this formatting was a challenge.
{LOW, LOW, LOW, LOW},
{LOW, LOW, LOW, LOW},
{LOW, LOW, LOW, LOW},
};
int activeChannel = 0;
int noteVals[3] = {40, 42, 44};
unsigned long lastStepTime = 0;
void setup() {
for (int i = 0; i < 5; i++) { //Sets I/O for all LEDs and button pins
pinMode(buttonPin[i], INPUT);
}
for (int i = 0; i < 4; i++) {
pinMode(ledPin[i], OUTPUT);
}
for (int i = 0; i < 3; i++) {
pinMode(channelLeds[i], OUTPUT);
}
}
void loop() {
changeChannels();
checkButtons();
updateLeds();
stepForwards();
}
void changeChannels() {
digitalWrite(channelLeds[activeChannel], LOW);
lastButtonState[4] = buttonState[4];
buttonState[4] = digitalRead(buttonPin[4]);
if(lastButtonState[4] == LOW && buttonState[4] == HIGH) {
activeChannel++;
if (activeChannel == 3) {
activeChannel = 0;
}
delay(5);
}
digitalWrite(channelLeds[activeChannel], HIGH);
}
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) {
switchedOn[activeChannel][i] = !switchedOn[activeChannel][i];
delay(5);
} else if(lastButtonState[i] == HIGH && buttonState[i] == LOW) {
delay(5);
}
}
}
void updateLeds() {
for (int i = 0; i < 4; i++) {
if(switchedOn[activeChannel][i] == HIGH 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();
for (int i = 0; i < 3; i++) {
usbMIDI.sendNoteOff(noteVals[i], 0, 1);
}
currentStep++; //increase counter
if (currentStep == 4) {
currentStep = 0; //reset counter
}
for (int i = 0; i < 3; i++) {
if (switchedOn[i][currentStep] == true) {
usbMIDI.sendNoteOn(noteVals[i], 127, 1);
}
}
}
}