int delayLength = 200; int notes[4] = {50, 55, 60, 65}; int LEDPins[4] = { 7, 8, 9, 10 }; void setup() { Serial.begin(9600); //Set pinMode for outputs (Pins 7,8,9,10) for (int i = 0; i < sizeof(LEDPins); i++) { pinMode(LEDPins[i], OUTPUT); //+7 offsets the pin number to start at 7 } //Set inputs' pinMode for (int i = 0; i < 3; i++) { pinMode(i+37, INPUT); //Same offset but for 37 for setting inputs } } void loop() { //Get delayLength by reading from pin 13 and mapping it between 100 and 1000 delayLength = map(analogRead(13), 0, 1023, 100, 1000); //Get the pot values for each step, add to notes array for (int i = 0; i < 4; i++) { notes[i] = map(analogRead(i+33), 0, 1023, 60, 72); //Allows values between 60 and 72 //If pin 37 is high, put it an octave up if (digitalRead(37) == HIGH) { notes[i] = notes[i] + 12; } } //Play each note from the array and flash appropriate LED if 38 is high if (digitalRead(38) == HIGH) { if (digitalRead(39) == HIGH) { sequenceForwards(); } else { sequenceBackwards(); } } } void sequenceForwards() { for (int i = 0; i < 4; i++) { digitalWrite(i+7, HIGH); //Light correct LED usbMIDI.sendNoteOn(notes[i], 127, 1); //Send note on //Wait so the note plays for given length delay(delayLength); //Turn of both LED and note digitalWrite(i+7, LOW); usbMIDI.sendNoteOff(notes[i], 0, 1); } } void sequenceBackwards() { for (int i = 3; i >=0; i--) { digitalWrite(i+7, HIGH); //Light correct LED usbMIDI.sendNoteOn(notes[i], 127, 1); //Send note on //Wait so the note plays for given length delay(delayLength); //Turn of both LED and note digitalWrite(i+7, LOW); usbMIDI.sendNoteOff(notes[i], 0, 1); } }
Categories: Digital Electronics Class
0 Comments