#include #include #include #include #include int maxVolume = 64; int userVolume = 64; int oldUserVolume = userVolume; int buttonState; int lastButtonState = LOW; unsigned long lastDebounceTime = 0; unsigned long debounceDelay = 50; bool debounceRead(int buttonPin); void setup() { pinMode(LED_HIGH, OUTPUT); // Green pinMode(LED_MED, OUTPUT); // Red LED (D4) pinMode(LED_LOW, OUTPUT); // Green LED pinMode(BTN_STAFF_UP, INPUT); // SW1 pinMode(BTN_STAFF_DWN, INPUT); // SW2 // RF Receiver pinMode(REMOTE_A, INPUT); // Remote Button A pinMode(REMOTE_B, INPUT); // Remote Button B pinMode(REMOTE_C, INPUT); // Remote Button C pinMode(REMOTE_D, INPUT); // Remote Button D pinMode(POT_CS, OUTPUT); // D10 - CS pinMode(POT_UD, OUTPUT); // D9 - U/D digitalWrite(POT_CS, HIGH); digitalWrite(POT_UD, LOW); for (int i = 0; i < 64; i++) { potIncrement(); } } void loop() { updateLeds(maxVolume); // SW1 - Staff Volume Up if (!debounceRead(BTN_STAFF_UP)) { if (maxVolume < 100) { maxVolume += STAFF_STEPS; } } // SW2 - Staff Volume Down if (!debounceRead(BTN_STAFF_DWN)) { if (maxVolume > STAFF_STEPS) { maxVolume -= STAFF_STEPS; if (userVolume > maxVolume) { for (int i = 0; i < (userVolume - maxVolume); i++) { potDecrement(); } userVolume = maxVolume; oldUserVolume = userVolume; } if (oldUserVolume > maxVolume) { oldUserVolume = maxVolume; } } } // Remote D - Mute if (debounceRead(REMOTE_D)) { // Check if we are in a muted state if (oldUserVolume > userVolume) { // This is if the user is muted and is asking to be unmuted // Check if max volume has been lowered since mute if (oldUserVolume > maxVolume) { // If the max volume has been lowered since mute, then set the user volume to the new max volume userVolume = maxVolume; for (int i = 0; i < maxVolume; i++) { potIncrement(); } } else { // If the max volume hasn't changed then, or the old user volume is lower than the new max - restore it to where it was userVolume = oldUserVolume; for (int i = 0; i < oldUserVolume; i++) { potIncrement(); } } } else { // This is if the user is unmuted and is aksing to be muted for (int i = 0; i < 64; i++) { potDecrement(); } oldUserVolume = userVolume; userVolume = 0; } } // Remote A - Significantly Lower Volume if (debounceRead(REMOTE_A)) { lowerVolume(&userVolume, &oldUserVolume, maxVolume); } // Remote B - Volume Up // Make sure we are not is a state of mute or lowered if (debounceRead(REMOTE_B) && !(oldUserVolume > userVolume)) { volumeUp(&userVolume, &oldUserVolume, maxVolume); } // Remote C - Volume Down if (debounceRead(REMOTE_C) && !(oldUserVolume > userVolume)) { volumeDown(&userVolume, &oldUserVolume); } } bool debounceRead(int buttonPin) { int reading = digitalRead(buttonPin); // If the switch changed, due to noise or pressing: if (reading != lastButtonState) { // reset the debouncing timer lastDebounceTime = millis(); } if ((millis() - lastDebounceTime) > debounceDelay) { // whatever the reading is at, it's been there for longer than the debounce delay, so take it as the actual current state: // if the button state has changed: if (reading != buttonState) { buttonState = reading; if (buttonState == HIGH) return true; else return false; } } // save the reading. Next time through the loop, it'll be the lastButtonState: lastButtonState = reading; return false; }