Digital Lab 4

Aidan Singh

See comments in code for explanation



int speedPotPin=A13;
int pot1 = 33;
int pot2 = 34;
int pot3 = 35;
int pot4 = 36;

int speedPotValue=100;

int ledPin1 = 28;
int ledPin2 = 29;
int ledPin3 = 30;
int ledPin4 = 31;

int switchPin = 37;
int switchPin2 = 38;

int pitch1= 0;
int pitch2= 0;
int pitch3= 0;
int pitch4= 0;

int octave= 0;

void setup() {
    Serial.begin(9600);
    pinMode(ledPin1, OUTPUT);
    pinMode(ledPin2, OUTPUT);
    pinMode(ledPin3, OUTPUT);
    pinMode(ledPin4, OUTPUT);
    pinMode(switchPin, INPUT);
    pinMode(switchPin2, INPUT);
}


void loop() {
  if(digitalRead(switchPin2) == HIGH) {     //This if statement checks the "on" switch,
    arpeggio();                             //and only runs arpeggio() if on
  }
}


void arpeggio(){
  
  speedPotValue = map(analogRead(speedPotPin), 0, 1023, 100, 1000);
  
  if(digitalRead(switchPin) == HIGH) {
    octave=12;
  }
  if(digitalRead(switchPin) == LOW) {
    octave=0;
  }
  
  /*The previous if statements determine
  the octave from the octave switch*/
                                            
  pitch1= map(analogRead(pot1), 0, 1023, 60, 72)+octave;
  pitch2= map(analogRead(pot2), 0, 1023, 60, 72)+octave;
  pitch3= map(analogRead(pot3), 0, 1023, 60, 72)+octave;
  pitch4= map(analogRead(pot4), 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"*/
 
  digitalWrite(ledPin1, HIGH);
  note(pitch1, speedPotValue);
  digitalWrite(ledPin1, LOW);

  digitalWrite(ledPin2, HIGH);
  note(pitch2, speedPotValue);
  digitalWrite(ledPin2, LOW);
  
  digitalWrite(ledPin3, HIGH);
  note(pitch3, speedPotValue);
  digitalWrite(ledPin3, LOW);
  
  digitalWrite(ledPin4, HIGH);
  note(pitch4, speedPotValue);
  digitalWrite(ledPin4, LOW);

  //Pitches play in order, LED's blink in order

}



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