#include <Arduino.h>
#include <Stepper.h>
#include <IRremote.h>
#include <RCSwitch.h>
#include "ButtonSystem.h"
#include "ButtonConfig.h"
#include "LEDSystem.h"
#include "LEDConfig.h"
// ─────────────────────────────────────────────
// S T A T E T R A C K I N G
// ─────────────────────────────────────────────
bool isZeroSet = false;
bool isMuted = false;
bool isVideoInput;
bool isCableInput;
int physicalVolume = -1;
int setVolume = -1;
// Volume offsets
const int offsetVideo = 1;
const int offsetCable = 3;
const int offsetCD = 0;
int currentOffset = 0;
// Presets
const int safeVolume = 15;
int lastSetVolume = 0;
//motor
const int stepsPerRevolution = 2048;
Stepper motor(stepsPerRevolution, 8, 10, 9, 11); // pins
int motorSpeed = 10; //rpm
//motor.step(-stepsPerRevolution); // one full revolution backwards
//IR stuff
const int IR_SEND_PIN = 3; // transmitter pin
const int IR_RECEIVE_PIN = 2; // Pin for IR receiver output
// IrSender.sendNEC(0x00FF, 0xA25D, 0); (to send a signal)
//RF stuff
RCSwitch Switch = RCSwitch();
int TransPin = 10;
int ReceptPin = 2;
//mySwitch.send(1234, 24); // send code 1234, 24 bits
void setup() {
IrSender.begin(IR_SEND_PIN);
IrReceiver.begin(IR_RECEIVE_PIN);
Switch.enableTransmit(TransPin);
Switch.enableReceive(ReceptPin);
Serial.begin(9600);
// Register your buttons and their modes here
Buttons.addButton(MuteBtn, tap);
Buttons.addButton(PowerBtn, hold);
Buttons.addButton(PlayBtn, super);
Buttons.addButton(VolUpBtn, cancel);
Buttons.addButton(VolDownBtn, spam);
Buttons.beginAll();
LEDs.addLED(RedLED, RedLED);
LEDs.addLED(BlueLED, BlueLED);
LEDs.beginAll();
motor.setSpeed(motorSpeed);
}
void loop() {
Buttons.updateAll();
LEDs.updateAll();
ButtonEvent event;
while (Buttons.getEvent(event)) {
if (!isZeroSet) {
UncalibratedButtonHandler(event);
} else {
ButtonHandler(event);
}
}
}
// Handle button events after calibration
void ButtonHandler(ButtonEvent& event) {
switch (event.pin) {
case MuteBtn:
if (event.type == TAP) Serial.println("Mute Tap");
break;
case PowerBtn:
if (event.type == TAP) Serial.println("Power Tap");
else if (event.type == HOLD) Serial.println("Power Hold");
break;
case PlayBtn:
switch (event.type) {
case TAP:
Serial.println("Play Tap");
break;
case INDICATOR:
Serial.println("Play indicator");
LEDs.setLED(RedLED, HOLD_FOR, 150);
break;
case HOLD:
Serial.println("Play Hold");
break;
case SUPER:
Serial.println("Play super");
break;
}
break;
case VolUpBtn:
if (event.type == TAP) Serial.println("VolUp Tap");
else if (event.type == CANCEL) Serial.println("Cancelled");
break;
case VolDownBtn:
if (event.type == TAP) Serial.println("VolDown Tap");
else if (event.type == SPAM) Serial.println("VolDown spam");
break;
default:
break;
}
}
// Handle button events before calibration
void UncalibratedButtonHandler(ButtonEvent& event) {
if (event.pin == MuteBtn && event.type == TAP) {
startupCalibration(true);
}
else if (event.pin == PowerBtn) {
if (event.type == CANCEL || event.type == TAP || event.type == HOLD || event.type == INDICATOR) {
if (event.type == TAP) Serial.println("Power Tap");
else if (event.type == HOLD) Serial.println("Power Hold");
}
}
else {
if (event.type == TAP || event.type == HOLD || event.type == SUPER || event.type == SPAM) {
startupCalibration(false);
}
}
}
void startupCalibration(bool isSetup) {
if (!isSetup) {
Serial.println("Error: System locked! Set volume knob to zero, then input to CD and press Mute.");
} else {
isZeroSet = true;
isVideoInput = false;
isCableInput = false;
physicalVolume = 0;
setVolume = 0;
Serial.println("Zero set via Mute button!");
}
}
debug
Power
CD/Cable switch
Aux input
Video input
Vol +
Vol -
Mute/safe vol
Play/pause