int delayLength = 200; int notes[4] = {50, 55, 60, 65}; int LEDPins[4] = { 7, 8, 9, 10 }; int currentStep = 0; int noteOffset = 0; unsigned long lastStepTime; 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, add offset if (digitalRead(37) == HIGH) { noteOffset = 12; } else { noteOffset = 0; } stepEither(); } void stepEither() { if (millis() > lastStepTime + delayLength) { lastStepTime = millis(); digitalWrite(LEDPins[currentStep], LOW); //Send noteOff to every possible note to avoid getting stuck while changing note/octave for (int i = 60; i <= 84; i++){ usbMIDI.sendNoteOff(i, 0, 1); } if (digitalRead(39) == HIGH) { currentStep++; if (currentStep >= 4) { currentStep = 0; } } else { currentStep--; if (currentStep < 0) { currentStep = 3; } } digitalWrite(LEDPins[currentStep], HIGH); usbMIDI.sendNoteOn(notes[currentStep] + noteOffset, 127, 1); //Send note on } }
Categories: Digital Electronics Class
0 Comments