Added Debounce timers and logic.

This commit is contained in:
KaraBun 2022-12-22 15:37:01 -05:00
parent 33d39f2ce2
commit a319e70d06

View file

@ -12,6 +12,7 @@ const int Enc1Btn = 16; //Encoder 1 Button
const int Enc2Btn = 17; //Encoder 2 Button const int Enc2Btn = 17; //Encoder 2 Button
const int ClockIn = 4; //Clock In const int ClockIn = 4; //Clock In
const int ClockDetect = 7; //Detect clock jack const int ClockDetect = 7; //Detect clock jack
const int DebounceTime = 10; //Debounce time in ms
Encoder LeftEnc( Enc1P1, Enc1P2); Encoder LeftEnc( Enc1P1, Enc1P2);
Encoder RightEnc( Enc2P1, Enc2P2); Encoder RightEnc( Enc2P1, Enc2P2);
@ -21,7 +22,8 @@ int E2 = 0;
bool E1Btn = false; bool E1Btn = false;
bool E2Btn = false; bool E2Btn = false;
bool ClockState = false; bool ClockState = false;
bool E1Prev, E2Prev, CDPrev = false; bool E1Prev, E2Prev, CDPrev, E1Click, E2Click, CDIn = false;
unsigned int E1Bounce, E2Bounce, CDBounce = 0;
unsigned long ClockPrev = 0; unsigned long ClockPrev = 0;
byte Input = 0; byte Input = 0;
int PpQN = 24; int PpQN = 24;
@ -56,7 +58,7 @@ void setup() {
ClockState = digitalRead(ClockIn); ClockState = digitalRead(ClockIn);
CDPrev = digitalRead(ClockDetect); CDPrev = digitalRead(ClockDetect);
//Clear the Lanes //Initialize the Lanes
for(int i = 0; i < 16; i++){ for(int i = 0; i < 16; i++){
Lane1[0][i] = random(0, 96); Lane1[0][i] = random(0, 96);
Lane1[1][i] = random(0, 255); Lane1[1][i] = random(0, 255);
@ -75,28 +77,46 @@ void setup() {
analogWrite( Lane1Pin, Lane1[1][0]); analogWrite( Lane1Pin, Lane1[1][0]);
analogWrite( Lane2Pin, Lane2[1][0]); analogWrite( Lane2Pin, Lane2[1][0]);
analogWrite( Lane3Pin, Lane3[1][0]); analogWrite( Lane3Pin, Lane3[1][0]);
// Timer0 is already used for millis() - we'll just interrupt somewhere
// in the middle and call the "Compare A" function below
OCR0A = 0xAF;
TIMSK0 |= _BV(OCIE0A);
} }
void loop() { void loop() {
//delay(1);
long newLEnc = LeftEnc.read(); long newLEnc = LeftEnc.read();
long newREnc = RightEnc.read(); long newREnc = RightEnc.read();
if (digitalRead(Enc1Btn) != E1Btn){ if (digitalRead(Enc1Btn) != E1Btn){
if (E1Bounce == 0 & E1Click == false){
Serial.println("E1Btn State Change"); Serial.println("E1Btn State Change");
E1Btn = !E1Btn; E1Btn = !E1Btn;
E1Bounce = DebounceTime;
E1Click = true;
}
} }
if (digitalRead(Enc2Btn) != E2Btn){ if (digitalRead(Enc2Btn) != E2Btn){
if (E2Bounce == 0 & E2Click == false){
Serial.println("E2Btn State Change"); Serial.println("E2Btn State Change");
E2Btn = !E2Btn; E2Btn = !E2Btn;
E2Bounce = DebounceTime;
E1Click = true;
}
} }
if (digitalRead(ClockDetect) != CDPrev){ bool newCD = digitalRead(ClockDetect);
Serial.println("Clock Detect Change");
CDPrev = !CDPrev; if (newCD != CDPrev){
if (CDBounce == 0){
Serial.println("Clock Jack Status Change");
CDPrev = CDIn = newCD;
CDBounce = DebounceTime << 4;
}
} }
if ( digitalRead(ClockIn) != ClockState){ if ( digitalRead(ClockIn) != ClockState){
@ -167,4 +187,12 @@ void loop() {
Lane3Time = ClockTime; Lane3Time = ClockTime;
analogWrite(Lane3Pin, Lane3[1][Lane3Pos]); analogWrite(Lane3Pin, Lane3[1][Lane3Pos]);
} }
}
// Interrupt is called once a millisecond,
SIGNAL(TIMER0_COMPA_vect)
{
E1Bounce = (E1Bounce - 1) & 0b10000000;
E2Bounce = (E2Bounce - 1) & 0b10000000;
CDBounce = (CDBounce - 1) & 0b10000000;
} }