/*
* Created by Chris Mudford - StimTech Pty Ltd
*
* This program is to use the Linn Lingo momentry push button with 2 LEDs to turn on and control the turntable motor speed.
*
*/
#include <ezButton.h>
//Define the Digital IO's
const int LP12GreenLED = 4; // LP12 Green LED 45RPM (switches HIGH through 2k2 current limiting resistor on Arduino PCB)
const int LP12RedLED = 5; // LP12 Red LED 33RPM (switches HIGH through 2k2 current limiting resistor on Arduino PCB)
const int LP12PushButton = 6; // LP12 momentary button (switches 5V LOW through Arduino input internal pullup)
const int SG4RotPushButton = 7; // SG4 rotary encoder momentary button (switches 5V LOW through Arduino input internal pullup)
const int SG4StandbyButton = 8; // SG4 Standby momentary button (switches LOW through 2k2 resistor on SG4)
const int SG4SpeedButton = 9; // SG4 Speed momentary button (switches LOW through 2k2 resistor on SG4)
const int SG4RotClk = 10; // SG4 Rotay encoder clk (switches LOW to deactivate clk when LP12 push button or SG4 Standby button are pressed)
const int SG4RotDt = 11; // SG4 Rotay encoder clk (switches LOW to deactivate dt when LP12 push button or SG4 Standby button are pressed)
const int SHORT_PRESS_TIME = 5000; // 5 seconds
const int LONG_PRESS_TIME = 5000; // 5 seconds
const int SG4ButtonPressDuration = 10; // Time duration of SG4 standby button to be pulled LOW
ezButton button1 (LP12PushButton); // Create ezButton object that is attached to the LP12PushButton pin
ezButton button2 (SG4RotPushButton); // Create ezButton object that is attached to the SG4StandbyButton pin
unsigned long pressedTime = 0;
unsigned long releasedTime = 0;
bool isPressing = false;
bool isLongDetected = false;
bool RunningState = false; // False is not running
void setup() {
button1.setDebounceTime (50); // Set debounce time to 50 milliseconds
button2.setDebounceTime (50); // Set debounce time to 50 millisceonds
//Serial.begin(9600);
// Defines IO Pins operation and initial state. Note: ezButton is Input_Pullup
pinMode (SG4StandbyButton, INPUT_PULLUP); // Set as Input_Pullup so that it's output is already high when changed to output
pinMode (SG4StandbyButton, OUTPUT); // Set pin as output
digitalWrite (SG4StandbyButton, HIGH); // Set output HIGH
pinMode (SG4SpeedButton, INPUT_PULLUP); // Set as Input_Pullup so that it's output is already high when changed to output
pinMode (SG4SpeedButton, OUTPUT); // Set pin as output
digitalWrite (SG4SpeedButton, HIGH); // Set output HIGH
pinMode (LP12RedLED, OUTPUT); // Set pin as output
pinMode (LP12GreenLED, OUTPUT); // Set pin as output
}
void loop() {
// If a push button is pressed, SG4 rotary encoder up and down are deactivated until the button is released
if(button1.getState() == LOW || button2.getState() == LOW) { // Is a push button pressed?
pinMode (SG4RotClk, OUTPUT); // Set pin as output - LOW by default
pinMode (SG4RotDt,OUTPUT); // Set pin as output - LOW by default
}
else {
pinMode (SG4RotClk, INPUT); // Set pin as input
pinMode (SG4RotDt, INPUT); // Set pin as input
}
button1.loop(); // MUST call the loop() function first
button2.loop(); // MUST call the loop() function first
if(button1.isPressed() || button2.isPressed()){ // Checks if the LP12 button or rotary encoder switch is being pressed
pressedTime = millis();
isPressing = true;
isLongDetected = false;
Serial.write("test");
// If a push button is pressed and the turntable is not running take SG4 out of standby and turn on RedLED
button1.loop(); // MUST call the loop() function first
button2.loop(); // Must call the loop() function first
if((button1.getState() == LOW || button2.getState() == LOW) && RunningState == false){ // Is a push button pressed and the turntable not running?
RunningState = !RunningState; // Toggles the running state
digitalWrite (SG4StandbyButton,LOW); // Takes the SG4 out of standby
digitalWrite (LP12RedLED,HIGH); // Switches the LP12RedLED on indicating 33RPM
delay (SG4ButtonPressDuration); // How long the GS4Standby button is switched LOW for
digitalWrite (SG4StandbyButton,HIGH); // NEED TO CHANGE THIS TO HIGH WHEN USING WITH SG4
Serial.write("turned on");
}
// If a push button is pressed and the turntable is running return SG4 into standby, switch SG4 speed to 33RPM, turn off RedLED and GreenLED
else{
button1.loop(); // MUST call the loop() function first
button2.loop(); // Must call the loop() function first
if((button1.getState() == LOW || button2.getState() == LOW) && RunningState == true){ // Is a push button pressed and the turntable not running?
RunningState = !RunningState; // Toggles the running state
digitalWrite (SG4StandbyButton, LOW); // Puts The SG4 Into Standby
digitalWrite (SG4SpeedButton, HIGH); // Switches The SG4 Speed To 33RPM
digitalWrite (LP12RedLED, LOW); // Switches The LP12RedLED off
digitalWrite (LP12GreenLED, LOW); // Switches The LP12GreenLED off
delay (SG4ButtonPressDuration); // How long the GS4Standby Button is activated for
digitalWrite (SG4StandbyButton, HIGH); // NEED TO CHANGE THIS TO HIGH WHEN USING WITH SG4
Serial.write("turned off");
}
}
}
if(button1.isReleased() || button2.isReleased()) { // Checks for LP12 button or SG4 rotary encoder switch being released
isPressing = false;
releasedTime = millis();
long pressDuration = releasedTime - pressedTime;
}
if(isPressing == true && isLongDetected == false) { // Checks if a button is being pressed and a long press not detected
long pressDuration = millis() - pressedTime; // Calculates how long button has been pressed
if( pressDuration > LONG_PRESS_TIME ) { // If button is pressed longer than long press time a long press is detected
isLongDetected = true;
// If a push button is pressed long and the turntable is running changes SG4 speed to 45RPM, switches LP12RedLED LOW and LP12GreenLED HIGH
if( isLongDetected == true && RunningState == true){ // If a long press is detected and the turntable is running
digitalWrite (SG4SpeedButton, LOW); // Sets SG4SpeedButton to LOW i.e. 45RPM
digitalWrite (LP12RedLED, LOW); // Turns LP12RedLED off
digitalWrite (LP12GreenLED, HIGH); // Turns LP12GreenLED on indicating 45RPM
Serial.write("45 RPM Selected");
}
}
}
}