Digital Lab 10

Code:

int buttonPins[4] = {36, 35, 34, 33};
int ledPins[4] = {29, 30, 31, 32};
int total = 4;

bool lastButtonState[4] = {LOW, LOW, LOW, LOW};
bool buttonState[4] = {LOW, LOW, LOW, LOW};

bool switchedOn[4] = {false, false, false, false};

unsigned long lastStepTime = 0;
int currentStep = 0;
int tempo = 0;

void setup() {
  for(int i = 0; i < total; i++){
    pinMode(buttonPins[i], INPUT);
    pinMode(ledPins[i], OUTPUT);
  }

}

void loop() {
  checkButton();
  updateLed();
  tempo = analogRead(A18);
  stepForwards();

}


void stepForwards(){
  if(millis() >= tempo + lastStepTime){
    lastStepTime = millis();
    usbMIDI.sendNoteOff(60, 127, 1);
    if(switchedOn[currentStep] == HIGH){
      usbMIDI.sendNoteOn(60, 127, 1);
    }
    currentStep++;
  }
  if(currentStep == total){
    currentStep = 0;
  }
}

void checkButton(){
  for(int i = 0; i < total; i++){
    lastButtonState[i] = buttonState[i];
    buttonState[i] = digitalRead(buttonPins[i]);

    if(lastButtonState[i] == LOW && buttonState[i] == HIGH){
      switchedOn[i] = !switchedOn[i];
      delay(5);
    }
    else if(lastButtonState == HIGH && buttonState == LOW ){
      delay(5);
    }
  }
}

void updateLed(){
  for(int i = 0; i < total; i++){
    if(switchedOn[i] == true or i == currentStep){
      digitalWrite(ledPins[i], HIGH);
    }
    else{
      digitalWrite(ledPins[i], LOW);
    }
  }
}



Leave a comment

Design a site like this with WordPress.com
Get started