Digital Lab 5

Aidan Singh

This is a step sequencer that like lab 4 has potentiometers that control the pitches, an octave switch, and an on-off switch. In addition to this, it has a new forwards/reverse switch for the playing order of potentiometers/pitches, and a number potentiometer determining how many steps are in the sequencer [BONUS].

The code utilizes arrays and for loops to condense much of the previous code from lab 4.


//pins
int potPins[4] = {33, 34, 35, 36};
int speedPotPin = A13;
int ledPins[4] = {28, 29, 30, 31};
int octaveSwitchPin = 37;
int onSwitchPin = 38;
int backwardsSwitchPin = 39;
int BONUSPOT = A21;
int bonusValue = 0;

//values
int speedPotValue=100;
int pitches[4] = {0, 0, 0, 0};
int octave= 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(){
  bonusValue = map(analogRead(BONUSPOT), 0, 1023, 0, 3);
  speedPotValue = map(analogRead(speedPotPin), 0, 1023, 100, 1000);
  if(digitalRead(octaveSwitchPin) == HIGH) {
    octave=12;
  }if(digitalRead(octaveSwitchPin) == LOW) {
    octave=0;
  }

  for(int i=0; i<4; i++){
      pitches[i] = map(analogRead(potPins[i]), 0, 1023, 60, 72)+octave;
    }                              
  
  /*the pitches above are set according to their
 respective potentiometers, and made an octave higher
 based on the value of the int "octave"*/
 
  for(int i=0; i<=bonusValue; i++){
    digitalWrite(ledPins[i], HIGH);
    note(pitches[i], speedPotValue);
    digitalWrite(ledPins[i], LOW);
    }        
}

void sequenceBackwards(){
  bonusValue = map(analogRead(BONUSPOT), 0, 1023, 0, 3);
  speedPotValue = map(analogRead(speedPotPin), 0, 1023, 100, 1000);
  if(digitalRead(octaveSwitchPin) == HIGH) {
    octave=12;
  }if(digitalRead(octaveSwitchPin) == LOW) {
    octave=0;
  }

  for(int i=0; i<4; i++){
      pitches[i] = map(analogRead(potPins[i]), 0, 1023, 60, 72)+octave;
    }                              
  
  /*the pitches above are set according to their
 respective potentiometers, and made an octave higher
 based on the value of the int "octave"*/
 
  for(int i=bonusValue; i>=0; i--){
    digitalWrite(ledPins[i], HIGH);
    note(pitches[i], speedPotValue);
    digitalWrite(ledPins[i], LOW);
    }        
}

void note(int pitch, int speeed){  //simplifies playing a [pitch] for [speeed] amount of time
  usbMIDI.sendNoteOn(pitch,90,1);
  delay(speeed);
  usbMIDI.sendNoteOff(pitch,0,1);
}

Leave a comment

Design a site like this with WordPress.com
Get started