// obviously replace these with whatever pins your led and button are int buttonPin[4] = {33, 34, 35, 36}; int ledPin[4] = {7, 8, 9, 10}; bool lastButtonState[4]; bool buttonState[4]; bool switchedOn[4]; int currentStep = 0; int delayLength = 200; unsigned long lastStepTime; void setup() { for (int i = 0; i<sizeof(buttonPin); i++){ pinMode(i+33, INPUT); } for (int i = 0; i<sizeof(ledPin); i++){ pinMode(i+7, OUTPUT); } } void loop() { stepForwards(); checkButtons(); updateLeds(); } void checkButtons() { for (int i = 0; i<4; i++) { lastButtonState[i] = buttonState[i]; buttonState[i] = digitalRead(buttonPin[i]); if(lastButtonState[i] == LOW && buttonState[i] == HIGH) { switchedOn[i] = !switchedOn[i]; // this means: if switchedOn is true then set it to false, if switchedOn is false then set it to true delay(5); } else if(lastButtonState[i] == HIGH && buttonState[i] == LOW) { delay(5); } } } void updateLeds() { for (int i = 0; i<4; i++) { if(switchedOn[i] == true || i == currentStep) { digitalWrite(ledPin[i], HIGH); } else { digitalWrite(ledPin[i], LOW); } } } void stepForwards() { if (millis() > lastStepTime + delayLength) { lastStepTime = millis(); usbMIDI.sendNoteOff(60, 0, 1); currentStep++; if (currentStep >= 4) { currentStep = 0; } if (switchedOn[currentStep]) { usbMIDI.sendNoteOn(60, 127, 1); } } }
Categories: Digital Electronics Class
0 Comments