Digital Electronics Lab Final

Below is my code:

int buttonPin[8] = { 33, 34, 35, 36, 37, 38, 39, 13};
int totalButtonPins = 8;

int ledPin[8] = { 23, 21, 20, 19, 18, 17, 16, 14};
int totalLeds = 8;

bool lastButton1State[8] = { LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};
bool button1State[8] = { LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};

boolean switchedOn[5][8] = {
  { LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW},
  { LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW},
  { LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW},
  { LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW},
  { LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW}
};

int channelDisplayed = 0;
int channelLedPin[5] = { 2, 3, 4, 5, 6};
int totalChannelLeds = 5;
int totalChannelSteps = 5;

int channelButton = 32;
bool lastChannelButtonState = LOW;
bool channelButtonState = LOW;

int midiNotes[5] = { 60, 61, 62, 63, 64};

unsigned long lastStepTime = 0;
int currentStep = 0;
int totalSteps = 8;

int tempoPot = 0;
int mappedTempoPot = 0;

int onOffPin = 30;
int offPin = 24;

void setup() {
  for (int i = 0; i < totalLeds; i++) {
    pinMode(ledPin[i], OUTPUT);
  }
  for (int i = 0; i < totalButtonPins; i++) {
    pinMode(buttonPin[i], INPUT);
  }
  for (int i = 0; i < totalChannelLeds; i++) {
    pinMode(channelLedPin[i], OUTPUT);
  }
  pinMode(channelDisplayed, INPUT);
  pinMode(channelButton, INPUT);
  pinMode(onOffPin, INPUT);
  pinMode(offPin, OUTPUT);
}


void loop()
{
  tempoPot = analogRead(A12);
  mappedTempoPot = map(tempoPot, 0, 1023, 100, 1000);

  if (digitalRead(onOffPin) == HIGH) {
    checkButtons();
    updateLeds();
    sequenceForwards();
    channelStep();
    channelLed();
  }
  if (digitalRead(onOffPin) == LOW) {
    digitalWrite(offPin, HIGH);
    delay(1000);
    digitalWrite(offPin, LOW);
    delay(1000);
  }
}

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

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

void sequenceForwards() {
  if (millis() >= lastStepTime + mappedTempoPot) {
    lastStepTime = millis();
    for (int i = 0; i < 5; i++) {
      usbMIDI.sendNoteOff(midiNotes[i], 0, 1);
    }
    countUp();

    for (int i = 0; i < 5; i++) {
      if (switchedOn[i][currentStep] == HIGH) {
        usbMIDI.sendNoteOn(midiNotes[i], 127, 1);
      }
    }
  }
}

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

void channelStep() {
  channelButtonState = digitalRead(channelButton);
  if (channelButtonState == HIGH && lastChannelButtonState == LOW) {
    channelDisplayed++;
    if (channelDisplayed == totalChannelSteps) {
      channelDisplayed = 0;
    }
    delay(5);
    Serial.println(channelDisplayed);
  }
  else if (channelButtonState == LOW && lastChannelButtonState == HIGH) {
    delay(5);
  }
  lastChannelButtonState = channelButtonState;
}

void channelLed() {
  for (int i = 0; i < 5; i++) {
    if (channelDisplayed == i) {
      digitalWrite(channelLedPin[i], HIGH);
    }
    else {
      digitalWrite(channelLedPin[i], LOW);
    }
  }
}

Explanation:
For my final I made a 8 step drum sequencer with 5 channels. In addition to this I attached a tempo pot that controls the speed of the sequencer being played. I also added a pause button that stops the sequencer and tells you that it is paused by a blinking yellow LED. For the LEDS for the 8 steps, I alternated green and red LEDs for the upcoming holiday season.

Leave a comment

Design a site like this with WordPress.com
Get started