Video of Working Circuit: https://youtu.be/q9PFLHi0AZQ
Code:
int led1 = 8; //variable for led1
int led2 = 9; //variable for led2
int led3 = 10; //variable for led3
int led4 = 11; //variable for led4
int button1 = 37; //variable for button1
int button2 = 36; //variable for button2
int button3 = 35; //variable for button3
int button4 = 34; //variable for button3
int switchPin1 = 31; //variable for slide switch 1
int potValue = 0;
void setup() {
pinMode(led1, OUTPUT); //set pin modes to input/output
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(switchPin1, INPUT);
}
void loop() { //determine mode
if (digitalRead(switchPin1) == HIGH) {
arpeggioMode();
} else if (digitalRead(switchPin1) == LOW) {
keyboardMode();
}
}
void keyboardMode() {
checkButtonPin(button1, led1, 60);
checkButtonPin(button2, led2, 64);
checkButtonPin(button3, led3, 67);
checkButtonPin(button4, led4, 71);
}
void arpeggioMode() {
if (digitalRead(button1) == HIGH) {
arpeggio(60);
} if (digitalRead(button2) == HIGH) {
arpeggio(64);
} if (digitalRead(button3) == HIGH) {
arpeggio(67);
} if (digitalRead(button4) == HIGH) {
arpeggio(71);
}
}
void checkButtonPin(int buttonPin, int ledPin, int note) {
if (digitalRead(buttonPin) == HIGH) {
usbMIDI.sendNoteOn(note, 127, 1);
digitalWrite(ledPin, HIGH);
delay(500);
usbMIDI.sendNoteOff(note, 0, 1);
digitalWrite(ledPin, LOW);
delay(500);
}
}
void arpeggio(int note) {
potValue = analogRead(A13);
usbMIDI.sendNoteOn(note, 127, 1); //play note
digitalWrite(led1, HIGH);
delay(potValue);
usbMIDI.sendNoteOff(note, 0, 1);
digitalWrite(led1, LOW);
usbMIDI.sendNoteOn(note + 4, 127, 1); //note + 4 semitones
digitalWrite(led2, HIGH);
delay(potValue);
usbMIDI.sendNoteOff(note + 4, 0, 1);
digitalWrite(led2, LOW);
usbMIDI.sendNoteOn(note + 7, 127, 1); //note + 7 semitones
digitalWrite(led3, HIGH);
delay(potValue);
usbMIDI.sendNoteOff(note + 7, 0, 1);
digitalWrite(led3, LOW);
usbMIDI.sendNoteOn(note + 11, 127, 1); //note + 11 semitones
digitalWrite(led4, HIGH);
delay(potValue);
usbMIDI.sendNoteOff(note + 11, 0, 1);
digitalWrite(led4, LOW);
}