265 lines
5.2 KiB
C++
265 lines
5.2 KiB
C++
#include <Arduino.h>
|
|
|
|
const int USER_STEPS = 2;
|
|
const int STAFF_STEPS = 16;
|
|
|
|
int maxVolume = 64;
|
|
int userVolume = 64;
|
|
int oldUserVolume = userVolume;
|
|
|
|
void volumeUp();
|
|
void volumeDown();
|
|
|
|
void setup()
|
|
{
|
|
pinMode(11, OUTPUT); // Green
|
|
pinMode(12, OUTPUT); // Red LED (D4)
|
|
pinMode(LED_BUILTIN, OUTPUT); // Green LED
|
|
|
|
pinMode(2, INPUT); // SW1
|
|
pinMode(4, INPUT); // SW2
|
|
|
|
// RF Receiver
|
|
pinMode(A0, INPUT); // Remote Button D
|
|
pinMode(A1, INPUT); // Remote Button C
|
|
pinMode(A2, INPUT); // Remote Button B
|
|
pinMode(A3, INPUT); // Remote Button A
|
|
|
|
pinMode(10, OUTPUT); // D10 - CS
|
|
pinMode(9, OUTPUT); // D9 - U/D
|
|
digitalWrite(10, HIGH);
|
|
digitalWrite(9, LOW);
|
|
|
|
for (int i = 0; i < 64; i++)
|
|
{
|
|
volumeUp();
|
|
}
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
switch (maxVolume)
|
|
{
|
|
case 64:
|
|
digitalWrite(11, HIGH);
|
|
digitalWrite(12, LOW);
|
|
digitalWrite(LED_BUILTIN, LOW);
|
|
break;
|
|
case 48:
|
|
digitalWrite(11, LOW);
|
|
digitalWrite(12, HIGH);
|
|
digitalWrite(LED_BUILTIN, LOW);
|
|
break;
|
|
case 32:
|
|
digitalWrite(11, LOW);
|
|
digitalWrite(12, LOW);
|
|
digitalWrite(LED_BUILTIN, HIGH);
|
|
break;
|
|
case 16:
|
|
digitalWrite(11, HIGH);
|
|
digitalWrite(12, HIGH);
|
|
digitalWrite(LED_BUILTIN, HIGH);
|
|
break;
|
|
}
|
|
|
|
// SW1 - Staff Volume Up
|
|
if (!digitalRead(2))
|
|
{
|
|
if (maxVolume < 100)
|
|
{
|
|
maxVolume += STAFF_STEPS;
|
|
delay(300);
|
|
}
|
|
}
|
|
|
|
// SW2 - Staff Volume Down
|
|
if (!digitalRead(4))
|
|
{
|
|
if (maxVolume > STAFF_STEPS)
|
|
{
|
|
maxVolume -= STAFF_STEPS;
|
|
|
|
if (userVolume > maxVolume)
|
|
{
|
|
for (int i = 0; i < (userVolume - maxVolume); i++)
|
|
{
|
|
volumeDown();
|
|
}
|
|
|
|
userVolume = maxVolume;
|
|
oldUserVolume = userVolume;
|
|
}
|
|
|
|
if (oldUserVolume > maxVolume)
|
|
{
|
|
oldUserVolume = maxVolume;
|
|
}
|
|
delay(300);
|
|
}
|
|
}
|
|
|
|
// Remote D - Mute
|
|
if (digitalRead(A0))
|
|
{
|
|
|
|
// 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++)
|
|
{
|
|
volumeUp();
|
|
}
|
|
}
|
|
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++)
|
|
{
|
|
volumeUp();
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// This is if the user is unmuted and is aksing to be muted
|
|
for (int i = 0; i < userVolume; i++)
|
|
{
|
|
volumeDown();
|
|
}
|
|
|
|
oldUserVolume = userVolume;
|
|
userVolume = 0;
|
|
}
|
|
|
|
delay(300);
|
|
}
|
|
|
|
// Remote A - Significantly Lower Volume
|
|
if (digitalRead(A3))
|
|
{
|
|
|
|
// Check if we volume is already lowered
|
|
if (oldUserVolume > userVolume)
|
|
{
|
|
|
|
// If it is lowered - restore it
|
|
// Check if max volume has changed since lowering
|
|
if (oldUserVolume > maxVolume)
|
|
{
|
|
// If the max volume has been lowered since lowering, then set the user volume to the new max volume
|
|
userVolume = maxVolume;
|
|
|
|
for (int i = 0; i < maxVolume; i++)
|
|
{
|
|
volumeUp();
|
|
}
|
|
}
|
|
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++)
|
|
{
|
|
volumeUp();
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// This is if the user is not lowered and is aksing to be lowered
|
|
for (int i = 0; i < userVolume - 10; i++)
|
|
{
|
|
volumeDown();
|
|
}
|
|
|
|
oldUserVolume = userVolume;
|
|
userVolume -= 10;
|
|
}
|
|
|
|
delay(300);
|
|
}
|
|
|
|
// Remote B - Volume Up
|
|
// Make sure we are not is a state of mute or lowered
|
|
if (digitalRead(A2) && !(oldUserVolume > userVolume))
|
|
{
|
|
if ((userVolume + USER_STEPS) > maxVolume)
|
|
{
|
|
for (int i = 0; i < maxVolume - userVolume; i++)
|
|
{
|
|
volumeUp();
|
|
}
|
|
|
|
userVolume = maxVolume;
|
|
oldUserVolume = userVolume;
|
|
}
|
|
else
|
|
{
|
|
for (int i = 0; i < 2; i++)
|
|
{
|
|
volumeUp();
|
|
}
|
|
|
|
userVolume += USER_STEPS;
|
|
oldUserVolume = userVolume;
|
|
}
|
|
|
|
delay(300);
|
|
}
|
|
|
|
// Remote C - Volume Down
|
|
if (digitalRead(A1) && !(oldUserVolume > userVolume))
|
|
{
|
|
if ((userVolume - USER_STEPS) < 0)
|
|
{
|
|
userVolume = 0;
|
|
oldUserVolume = userVolume;
|
|
}
|
|
else
|
|
{
|
|
userVolume -= USER_STEPS;
|
|
oldUserVolume = userVolume;
|
|
}
|
|
|
|
delay(300);
|
|
}
|
|
}
|
|
|
|
void volumeUp()
|
|
{
|
|
digitalWrite(9, HIGH);
|
|
delay(1);
|
|
digitalWrite(10, LOW);
|
|
delay(1);
|
|
digitalWrite(9, LOW);
|
|
delay(1);
|
|
digitalWrite(9, HIGH);
|
|
delay(1);
|
|
digitalWrite(10, HIGH);
|
|
delay(1);
|
|
digitalWrite(9, LOW);
|
|
digitalWrite(10, HIGH);
|
|
}
|
|
|
|
void volumeDown()
|
|
{
|
|
digitalWrite(9, LOW);
|
|
delay(1);
|
|
digitalWrite(10, LOW);
|
|
delay(1);
|
|
digitalWrite(9, HIGH);
|
|
delay(1);
|
|
digitalWrite(9, LOW);
|
|
delay(1);
|
|
digitalWrite(10, HIGH);
|
|
} |