Digital Electronics Lab #5

Video:

Code:

int octaveSwitch = 30;                                          //switch variables
int powerSwitch = 29;
int directionSwitch = 28;

int tempoVal = 0;                                               //tempo variables
int lastTempoVal = 0;
int tempoPin = A13;

int octave = 12;                                                //octave switch variable


int ledPinArray[4] = {7, 8, 9, 10};                             //ledPin array
int totalLedPins = 4;

int notePotArray[4] = {A14, A15, A16, A17};                     //notePot array
int totalNotePots = 4;

int MIDINotes[4] = {0, 0, 0, 0};                                //MIDINotes array

void setup() {

  pinMode(octaveSwitch, INPUT);                                 //pinMode for switches
  pinMode(powerSwitch, INPUT);
  pinMode(directionSwitch, INPUT);

  for (int i = 0; i < totalLedPins; i ++) {                    //pinMode for LED loop
    pinMode(ledPinArray[i], OUTPUT);
  }

}
void loop() {

  if (digitalRead(powerSwitch) == HIGH) {                      //power swtich

    if (digitalRead(directionSwitch) == HIGH) {                //backward function

      sequenceBackward();
      checkOctave();
    }

    if (digitalRead(directionSwitch) == LOW) {                //forward function
      sequenceForward();
      checkOctave();
    }

  }
}




void sequenceForward() {

  tempoVal = map(analogRead(tempoPin), 0, 1023, 100, 1000);                 //tempoVal

  for (int i = 0; i < totalNotePots; i ++) {                                //forward loop
    MIDINotes[i] = map(analogRead(notePotArray[i]), 0, 1023, 60, 72);
    usbMIDI.sendNoteOn(MIDINotes[i] - octave, 127, 1);
    digitalWrite(ledPinArray[i], HIGH);
    delay(tempoVal);
    usbMIDI.sendNoteOff(MIDINotes[i] - octave, 0, 1);
    digitalWrite(ledPinArray[i], LOW);
    delay(tempoVal);
  }
}

void sequenceBackward() {

  tempoVal = map(analogRead(tempoPin), 0, 1023, 100, 1000);               //tempoVal

  for (int i = 3; i >= 0; i--) {                                          //backward loop
    MIDINotes[i] = map(analogRead(notePotArray[i]), 0, 1023, 60, 72);
    usbMIDI.sendNoteOn(MIDINotes[i] - octave, 127, 1);
    digitalWrite(ledPinArray[i], HIGH);
    delay(tempoVal);
    usbMIDI.sendNoteOff(MIDINotes[i] - octave, 0, 1);
    digitalWrite(ledPinArray[i], LOW);
    delay(tempoVal);
  }
}

void checkOctave() {                                  //checkOctave function
  if (digitalRead(octaveSwitch) == LOW) {             //if switch is off, octave is 0, won't add anything to the pitches
    octave = 0;
  }
  if (digitalRead(octaveSwitch) == HIGH) {            // if switch is on, octave is 12, will subtract an octave from the pitches
    octave = 12;
  }
}

Leave a comment

Design a site like this with WordPress.com
Get started