Digital Lab 6

Aidan Singh


//pins
int potPins[4] = {A14, A15, A16, A17};
int ledPins[4] = {28, 29, 30, 31};
int speedPotPin = A13;
int stepPot = A21;
int octaveSwitchPin = 37;
int onSwitchPin = 38;
int backwardsSwitchPin = 39;


//values
int octave = 0;
int lastStep = 0;
int tempo = 1000;
int currentStep = 0;
int totalSteps = 4;
unsigned long lastStepTime = 0;

int lastPitch = 0;
int currentPitch = 0;

void setup() {
  Serial.begin(9600);
  for (int i = 0; i < 4; i++) {
    pinMode(ledPins[i], OUTPUT);
  }
  pinMode(octaveSwitchPin, INPUT);
  pinMode(onSwitchPin, INPUT);
  pinMode(backwardsSwitchPin, INPUT);
}


void loop() {

  if (digitalRead(onSwitchPin) == HIGH) {
    if (digitalRead(backwardsSwitchPin) == HIGH) {
      sequenceForwards();
    }
    else {
      sequenceBackwards();
    }
  }
}

void sequenceForwards() {

  if (millis() >= lastStepTime + tempo) {
    sequenceSetup();
    lastStepTime = millis();
    note();
    lastStep=currentStep;
    currentStep++;
    if (currentStep == totalSteps) {
      currentStep = 0;
    }
    Serial.println(currentStep);
  }
}

void sequenceBackwards() {
  if (millis() >= lastStepTime + tempo) {
    sequenceSetup();
    lastStepTime = millis();
    note();
    lastStep=currentStep;
    currentStep--;
    if (currentStep < 0) {
      currentStep = totalSteps - 1;
    }
  }
}

void sequenceSetup() {
  totalSteps = 4;//map(analogRead(stepPot), 0, 1023, 0, 4);
  tempo = map(analogRead(speedPotPin), 0, 1023, 100, 1000);
  if (digitalRead(octaveSwitchPin) == HIGH) {
    octave = 12;
  } else {
    octave = 0;
  }
}

void note() {
  currentPitch = map(analogRead(potPins[currentStep]), 0, 1023, 60, 72) + octave;
  digitalWrite(ledPins[lastStep], LOW);
  usbMIDI.sendNoteOff(lastPitch, 0, 1);
  usbMIDI.sendNoteOn(currentPitch, 90, 1);
  digitalWrite(ledPins[currentStep], HIGH);
  lastPitch = currentPitch;

}

Leave a comment

Design a site like this with WordPress.com
Get started