Digital Electrics Lab 6

Below is my code:

int ledPin1 = 8;
int ledPin2 = 9;
int ledPin3 = 10;
int ledPin4 = 11;
int ledPinArray[4] = { 8, 9, 10, 11 };
int totalLeds = 4;

int potVal1 = 0;
int potVal2 = 0;
int potVal3 = 0;
int potVal4 = 0;
int potValArray[4] = { 0, 0, 0, 0 };

int mappedPotVal1 = 0;
int mappedPotVal2 = 0;
int mappedPotVal3 = 0;
int mappedPotVal4 = 0;
int mappedPotValArray[4] = { 0, 0, 0, 0 };

int speedPotVal = 0;
int tempo = 0;

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

int backwardsSwitch = 39;
int octaveSwitch = 32;

void setup() {
  for (int i = 0; i < totalLeds; i++) {
    pinMode(ledPinArray[i], OUTPUT);
  }
  pinMode(backwardsSwitch, INPUT);
  pinMode(octaveSwitch, INPUT);
  Serial.begin(9600);
}

void loop() {
  speedPotVal = analogRead(A19);
  tempo = map(speedPotVal, 0, 1023, 100, 1000);

  potVal1 = analogRead(A15);
  potVal2 = analogRead(A16);
  potVal3 = analogRead(A17);
  potVal4 = analogRead(A18);

  mappedPotVal1 = potVal1;
  mappedPotVal2 = potVal2;
  mappedPotVal3 = potVal3;
  mappedPotVal4 = potVal4;

  mappedPotValArray[0] = map(potVal1, 0, 1023, 60, 72);
  mappedPotValArray[1] = map(potVal2, 0, 1023, 60, 72);
  mappedPotValArray[2] = map(potVal3, 0, 1023, 60, 72);
  mappedPotValArray[3] = map(potVal4, 0, 1023, 60, 72);


  if (digitalRead(backwardsSwitch) == LOW) {
    if (digitalRead(octaveSwitch) == HIGH) {
      octaveUpForwards();
    }
    if (digitalRead(octaveSwitch) == LOW) {
      sequenceForwards();
    }
  }
  if (digitalRead(backwardsSwitch) == HIGH) {
    if (digitalRead(octaveSwitch) == HIGH) {
      octaveUpBackwards();
    }
    if (digitalRead(octaveSwitch) == LOW) {
      sequenceBackwards();
    }
  }
}


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

void countDown() {
  currentStep--;
  if (currentStep < 0) {
    currentStep = totalSteps - 1;
  }
}

void sequenceForwards() {
  if (millis() >= lastStepTime + tempo) {
    lastStepTime = millis();
    usbMIDI.sendNoteOff(mappedPotValArray[currentStep], 0, 1);
    digitalWrite(ledPinArray[currentStep], LOW);
    countUp();
    //    mappedPotValArray[currentStep]
    usbMIDI.sendNoteOn(mappedPotValArray[currentStep], 127, 1);
    digitalWrite(ledPinArray[currentStep], HIGH);

    Serial.println(currentStep);
  }
}

void sequenceBackwards() {
  if (millis() >= lastStepTime + tempo) {
    lastStepTime = millis();
    usbMIDI.sendNoteOff(mappedPotValArray[currentStep], 0, 1);
    digitalWrite(ledPinArray[currentStep], LOW);
    countDown();
    usbMIDI.sendNoteOn(mappedPotValArray[currentStep], 127, 1);
    digitalWrite(ledPinArray[currentStep], HIGH);

    Serial.println(currentStep);
  }
}

void octaveUpForwards() {
  if (millis() >= lastStepTime + tempo) {
    lastStepTime = millis();
    usbMIDI.sendNoteOff(mappedPotValArray[currentStep] + 12, 0, 1);
    digitalWrite(ledPinArray[currentStep], LOW);
    countUp();
    usbMIDI.sendNoteOn(mappedPotValArray[currentStep] + 12, 127, 1);
    digitalWrite(ledPinArray[currentStep], HIGH);

    Serial.println(currentStep);
  }
}
void octaveUpBackwards() {
  if (millis() >= lastStepTime + tempo) {
    lastStepTime = millis();
    usbMIDI.sendNoteOff(mappedPotValArray[currentStep] + 12, 0, 1);
    digitalWrite(ledPinArray[currentStep], LOW);
    countDown();
    usbMIDI.sendNoteOn(mappedPotValArray[currentStep] + 12, 127, 1);
    digitalWrite(ledPinArray[currentStep], HIGH);

    Serial.println(currentStep);
  }
}

Leave a comment

Design a site like this with WordPress.com
Get started