Digital Lab 9

Aidan Singh


int ledPin [4] = {29,30,31,32};
int channelLedPin [3] = {7,8,9};
int buttonPin [4] = {33,34,35,36};
int switchPin = 38;
bool lastButtonState [4] = {LOW,LOW,LOW,LOW};
bool buttonState [4] = {LOW,LOW,LOW,LOW};
bool switchedOn[3][4] = {
  {false,false,false,false},
  {false,false,false,false},
  {false,false,false,false},
};

int channelButtonPin = 39;
int channelDisplayed = 0;
bool channelButtonState = LOW;
bool lastChannelButtonState = LOW;

int tempo=1000;
unsigned long lastStepTime = 0;
int currentStep = 0;
int totalSteps = 4;

void setup() {
  pinMode(channelButtonPin,INPUT);
  for(int i=0;i<4;i++){
    pinMode(ledPin [i],OUTPUT);
    pinMode(buttonPin [i],INPUT);
  }
  for(int i=0;i<3;i++){
    pinMode(channelLedPin [i],OUTPUT);
  }
  pinMode(switchPin,INPUT);
}

void loop(){


  digitalWrite(channelLedPin[channelDisplayed],HIGH);
  if(digitalRead(switchPin) == HIGH){ //checks forwards/backwards button
    stepForwards();
  }else{
  stepBackwards();}
  tempo =analogRead(A18);
  checkButtons();
  updateLeds();
}

void stepForwards(){
  if(millis() >= lastStepTime + tempo) {
    lastStepTime = millis();
    usbMIDI.sendNoteOff(37, 0, 1);//SENDS MIDI NOTE OFF
    usbMIDI.sendNoteOff(39, 0, 1);
    usbMIDI.sendNoteOff(40, 0, 1);
    currentStep++;
    if(currentStep == totalSteps){
      currentStep = 0;
    }
    if(switchedOn[0][currentStep] == true){ //SENDS MIDI NOTE ON
    usbMIDI.sendNoteOn(37, 90, 1);
  }
  if(switchedOn[1][currentStep] == true){ //SENDS MIDI NOTE ON
    usbMIDI.sendNoteOn(39, 90, 1);
  }
  if(switchedOn[2][currentStep] == true){ //SENDS MIDI NOTE ON
    usbMIDI.sendNoteOn(40, 90, 1);
  }
 }
}

 void stepBackwards(){
  if(millis() >= lastStepTime + tempo) {
    lastStepTime = millis();
    usbMIDI.sendNoteOff(37, 0, 1);//SENDS MIDI NOTE OFF
    usbMIDI.sendNoteOff(39, 0, 1);
    usbMIDI.sendNoteOff(40, 0, 1);
    currentStep--;
    if(currentStep < 0){
      currentStep = 3;
    }
    if(switchedOn[0][currentStep] == true){ //SENDS MIDI NOTE ON
    usbMIDI.sendNoteOn(37, 90, 1);
  }
  if(switchedOn[1][currentStep] == true){ //SENDS MIDI NOTE ON
    usbMIDI.sendNoteOn(39, 90, 1);
  }
  if(switchedOn[2][currentStep] == true){ //SENDS MIDI NOTE ON
    usbMIDI.sendNoteOn(40, 90, 1);
  }
}
 }



void checkButtons() {

    lastChannelButtonState = channelButtonState;
    channelButtonState = digitalRead(channelButtonPin);
    if(lastChannelButtonState == LOW && channelButtonState == HIGH) {
      digitalWrite(channelLedPin[channelDisplayed],LOW);
      changeChannel();
      digitalWrite(channelLedPin[channelDisplayed],HIGH);
      delay(5);
    } else if(lastChannelButtonState == HIGH && channelButtonState == LOW) {
      delay(5);
    }
  
    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 changeChannel() {
    channelDisplayed++;
    if(channelDisplayed == 3){
      channelDisplayed = 0;
    }
}

void flipButtonState(int i) {
    if(switchedOn[channelDisplayed][i] == true) {
      switchedOn[channelDisplayed][i] = false;
    } else if(switchedOn[channelDisplayed][i] == false) {
      switchedOn[channelDisplayed][i] = true;
    }  
}

void updateLeds() {
  for(int i=0;i<4;i++){
  if(switchedOn[channelDisplayed][i] == true) {//lights LED if button was pressed
    digitalWrite(ledPin[i], HIGH);
  } else if(currentStep == i){//lights LED of current step
    digitalWrite(ledPin[i], HIGH);
  }
    else {digitalWrite(ledPin[i], LOW);
  }
  }
}

Leave a comment

Design a site like this with WordPress.com
Get started