Digital Electronics Lab #9

Video:

Code:

int buttonPin[4] = {33, 34, 35, 36};
int ledPin[4] = {7, 8, 9, 10};
int totalLedPins = 4;
int totalButtonPins = 4;

int tempoPot = A13;
int tempoValue = 0;

unsigned long lastStep = 0;
int currentStep = 0;
int totalSteps = 4;

int channelDisplayed = 0;
int totalChannels = 3;
int channelLedPins[3] = {28, 29, 30};
int channelButtonPin = 37;
bool lastChannelButtonState = LOW;
bool channelButtonState = LOW;


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

bool switchedOn[3][4] = {
  { LOW, LOW, LOW, LOW },
  { LOW, LOW, LOW, LOW },
  { LOW, LOW, LOW, LOW }
};

int midiNotes[3] = { 40, 42, 44 };
int totalMidiNotes = 3;



void setup() {
  for (int i = 0; i < totalLedPins; i ++) {
    pinMode(ledPin[i], OUTPUT);
  }
  for (int i = 0; i < totalButtonPins; i ++) {
    pinMode(buttonPin[i], INPUT);
  }

  for (int i = 0; i < 3; i++) {
    pinMode(channelLedPins[i], OUTPUT);
  }
  pinMode(channelButtonPin, INPUT);
}
void loop() {


  checkButtons();
  updateLeds();
  stepForwards();

  checkChannelButton();
  updateChannelLeds();

}


void checkButtons() {                                     
  for (int i = 0; i < totalButtonPins; i++) {
    lastButtonState[i] = buttonState[i];
    buttonState[i] = digitalRead(buttonPin[i]);
    if (lastButtonState[i] == LOW && buttonState[i] == HIGH) {
      switchedOn[channelDisplayed][i] = !switchedOn[channelDisplayed][i];                         
      delay(5);
    } else if (lastButtonState[i] == HIGH && buttonState[i] == LOW) {
      delay(5);
    }
  }
}


void updateLeds() {                                     
  for (int i = 0; i < totalLedPins; i++) {
    if (switchedOn[channelDisplayed][i] == true) {
      digitalWrite(ledPin[i], HIGH);
    } else {
      digitalWrite(ledPin[i], LOW);
    }
  }
  digitalWrite(ledPin[currentStep], HIGH);            
}

void stepForwards() {                                 
  tempoValue = analogRead(A13);
  if (millis() > lastStep + tempoValue) {
    lastStep = millis();
    for (int i = 0; i < totalMidiNotes; i++) {
      usbMIDI.sendNoteOff(midiNotes[i], 127, 1);
    }
    countUp();
    for (int i = 0; i < totalMidiNotes; i++)
      if (switchedOn[i][currentStep] == HIGH) {
        usbMIDI.sendNoteOn(midiNotes[i], 127, 1);
      }
  }
}

void checkChannelButton() {

  lastChannelButtonState = channelButtonState;
  channelButtonState = digitalRead(channelButtonPin);

  if (lastChannelButtonState == LOW and channelButtonState == HIGH) {
    countUpChannel();
    delay(5);
  }
  if (lastChannelButtonState == HIGH and channelButtonState == LOW) {
    delay(5);
  }
}


void updateChannelLeds() {

  for (int i = 0; i < totalChannels; i++) {
    if (i == channelDisplayed) {
      digitalWrite(channelLedPins[i], HIGH);
    } else {
      digitalWrite(channelLedPins[i], LOW);
    }
  }
}

void countUpChannel() {
  channelDisplayed++;
  if (channelDisplayed == totalChannels) {
    channelDisplayed = 0;
  }
}


void countUp() {                                   
  currentStep++;
  if (currentStep == totalSteps) {
    currentStep = 0;
  }
}

Leave a comment

Design a site like this with WordPress.com
Get started