// obviously replace these with whatever pins your led and button are 
int buttonPin[4] = {33, 34, 35, 36};
int ledPin[4] = {7, 8, 9, 10};
int channelLedPin[3] = {25, 26, 27};

int midiNotes[3] = { 60, 64, 67 };

bool lastButtonState[4];
bool buttonState[4];

bool switchedOn[3][4];

int channelDisplayed = 0;

int currentStep = 0;

int delayLength = 200;

unsigned long lastStepTime;

unsigned long lastClickTime;

void setup() {
  Serial.begin(9600);
  
  for (int i = 0; i<sizeof(buttonPin); i++){

    pinMode(i+33, INPUT);
    
  }

  pinMode(37, INPUT);
  
  for (int i = 0; i<sizeof(ledPin); i++){

    pinMode(i+7, OUTPUT);
    
  }

  for (int i = 0; i<3; i++){

    pinMode(i+25, OUTPUT);
    
  }
  
}

void loop() 
{

  delayLength = map(analogRead(13), 0, 1023, 100, 1000);

  if (digitalRead(37) == HIGH && (millis() > lastClickTime + 150)){
    lastClickTime = millis();
    channelDisplayed++;
    if (channelDisplayed > 2) {
      channelDisplayed = 0;
    }
    
  }
  
  stepForwards();
  checkButtons();
  updateLeds();
}

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[channelDisplayed][i] = !switchedOn[channelDisplayed][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() {
  for (int i = 0; i<4; i++) {
    if(switchedOn[channelDisplayed][i] == true || i == currentStep) {
      digitalWrite(ledPin[i], HIGH);
    } else {
      digitalWrite(ledPin[i], LOW);
    }
  }

  for (int i=0; i<3; i++) {

    if (i == channelDisplayed) {

      digitalWrite(channelLedPin[i], HIGH);
      
    } else {

      digitalWrite(channelLedPin[i], LOW);
      
    }
    
  }
  
}

void stepForwards() {
  
  if (millis() > lastStepTime + delayLength) {

    lastStepTime = millis();

    for (int i = 0; i<3; i++){
      usbMIDI.sendNoteOff(midiNotes[i], 0, 1);
    }
    
    currentStep++;
    if (currentStep >= 4) {
      currentStep = 0;
    }

    Serial.write(currentStep);

    for (int i = 0; i<3; i++){

      if (switchedOn[i][currentStep]) {
  
        usbMIDI.sendNoteOn(midiNotes[i], 127, 1);
        
      }
    }
    
  }
  
}


0 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.