Digital Electronics Lab 6

Video:

Code:

int potVals[4] = {0, 0, 0, 0};               //array for potentiometers
int mappedPotVals[4] = {0, 0, 0, 0};         //array for mapped pot values
int analogPinNum [4] = {A14, A15, A16, A17}; //array for analog pin numbers
int ledPins[4] = {29, 30, 31, 32};           //array for LEDs

int tempoPot = 0;
int frontBackToggle = 39;
int octaveToggle = 38;
int mappedPotValTempo = 0;
int currentStep = 0;                         //"counter" variable
unsigned long lastStepTime = 0;              //this is the timetracker setup

void setup() {
  for (int i = 0; i < 4; i++) {      //setting LEDs as outputs
    pinMode(ledPins[i], OUTPUT);
  }
  pinMode(octaveToggle, INPUT);
  pinMode(frontBackToggle, INPUT);
}
void loop() {
  if (digitalRead(frontBackToggle) == HIGH) {  //this checks which sequence direction is active.
    sequenceForward();
  } else {
    sequenceBackward();
  }
}

void sequenceForward() { //sequence forward
  tempoPot = analogRead(A18);
  mappedPotValTempo = map(tempoPot, 0, 1023, 100, 1000);   //set up tempo pot

  if (millis() >= lastStepTime + mappedPotValTempo) {      //using millis() to tell the teensy when to start seqenceing.

    lastStepTime = millis();
    digitalWrite(ledPins[currentStep], LOW);
    usbMIDI.sendNoteOff(mappedPotVals[currentStep], 127, 1);

    if (currentStep == 3) {
      currentStep = 0;
    } else {
      currentStep++;
    }

    potVals[currentStep] = analogRead(analogPinNum[currentStep]);
    mappedPotVals[currentStep] = map(potVals[currentStep], 0, 1023, 60, 72);   //maps pots to note/MIDI values

    if (digitalRead(octaveToggle) == HIGH) {                 
      for (int i = 0; i < 4; i++) {
        mappedPotVals[i] = mappedPotVals[i] + 12;
      }
    }

    digitalWrite(ledPins[currentStep], HIGH);
    usbMIDI.sendNoteOn(mappedPotVals[currentStep], 127, 1);
  }
}

void sequenceBackward() { //sequence backwards
  tempoPot = analogRead(A18);
  mappedPotValTempo = map(tempoPot, 0, 1023, 100, 1000);   //tempo pot

  if (millis() >= lastStepTime + mappedPotValTempo) {     

    lastStepTime = millis();
    digitalWrite(ledPins[currentStep], LOW);
    usbMIDI.sendNoteOff(mappedPotVals[currentStep], 127, 1);

    if (currentStep == 0) {
      currentStep = 3;
    } else {
      currentStep--;
    }

    potVals[currentStep] = analogRead(analogPinNum[currentStep]);
    mappedPotVals[currentStep] = map(potVals[currentStep], 0, 1023, 60, 72);   

    if (digitalRead(octaveToggle) == HIGH) {                 
      for (int i = 0; i < 4; i++) {
        mappedPotVals[i] = mappedPotVals[i] + 12;
      }
    }

    digitalWrite(ledPins[currentStep], HIGH);
    usbMIDI.sendNoteOn(mappedPotVals[currentStep], 127, 1);
  }
}

Leave a comment

Design a site like this with WordPress.com
Get started