I det stora hela så försöker jag få till en "grundfunktion" för att hantera knapptryckningarna. Dels så en knapptryckning blir en one-shot samt debounce.
Just nu försöker jag debugga vart det går fel genom att skriva ut text seriellt vilken knapp jag trycker på och det enda jag får ut är Left button pressed! som rullar om och om igen, utan att jag ens tryckt någon knapp.
Kod: Markera allt
/*****************************************CONSTANTS****************************************************/
const uint8_t buttonLeft = 3;
const uint8_t buttonRight = 4;
const uint8_t buttonFwd = 5;
const uint8_t buttonX = 6;
/*****************************************************************************************************/
/**************************************GLOBAL VARIABLES***********************************************/
uint32_t debounceDelay = 50;
uint32_t lastDebounceChk = 0;
uint8_t debounceReg_Left = 0;
uint8_t debounceReg_Right = 0;
uint8_t debounceReg_Fwd = 0;
uint8_t debounceReg_X = 0;
uint8_t buttonLeft_Pressed = 0;
uint8_t buttonRight_Pressed = 0;
uint8_t buttonFwd_Pressed = 0;
uint8_t buttonX_Pressed = 0;
uint8_t Left_Press_Done = 0;
uint8_t Right_Press_Done = 0;
uint8_t Fwd_Press_Done = 0;
uint8_t X_Press_Done = 0;
uint8_t buttonCounter = 0;
enum Directions : uint8_t {LEFT, RIGHT, FWD, X, NONE};
enum Directions key;
enum StatusOfButtons : uint8_t {WAIT_FOR_KEYPRESS, WAIT_FOR_KEYRELEASE};
enum StatusOfButtons Buttonstatus;
/*****************************************************************************************************/
void pushButtonChk() {
if(millis() >= (lastDebounceChk + debounceDelay)) {
lastDebounceChk = millis();
debounceReg_Left <<= 1; //
debounceReg_Right <<= 1;
debounceReg_Fwd <<= 1;
debounceReg_X <<= 1;
debounceReg_Left |= buttonLeft;
debounceReg_Right |= buttonRight;
debounceReg_Fwd |= buttonFwd;
debounceReg_X |= buttonX;
if(debounceReg_Left == 0xFF) {
Directions key = LEFT;
}
else if(debounceReg_Left == 0x00) {
Directions key = NONE;
}
if(debounceReg_Right == 0xFF) {
Directions key = RIGHT;
}
else if(debounceReg_Right == 0x00) {
Directions key = NONE;
}
if(debounceReg_Fwd == 0xFF) {
Directions key = FWD;
}
else if(debounceReg_Fwd == 0x00) {
Directions key = NONE;
}
if(debounceReg_X == 0xFF) {
Directions key = X;
}
else if(debounceReg_X == 0x00) {
Directions key = NONE;
}
}
}
void setup() {
Serial.begin(9600);
pinMode(buttonLeft, INPUT);
pinMode(buttonRight, INPUT);
pinMode(buttonFwd, INPUT);
pinMode(buttonX, INPUT);
}
void loop() {
pushButtonChk();
switch(Buttonstatus) {
case WAIT_FOR_KEYPRESS :
buttonCounter = 0;
switch(key) {
case LEFT :
if(!Left_Press_Done) {
Left_Press_Done = 1;
Serial.println("Left button pressed!");
}
Buttonstatus = WAIT_FOR_KEYRELEASE;
break;
case RIGHT :
if(!Right_Press_Done) {
Right_Press_Done = 1;
Serial.println("Right button pressed!");
}
Buttonstatus = WAIT_FOR_KEYRELEASE;
break;
case FWD :
if(!Fwd_Press_Done) {
Fwd_Press_Done = 1;
Serial.println("Fwd button pressed!");
}
Buttonstatus = WAIT_FOR_KEYRELEASE;
break;
case X :
if(!X_Press_Done) {
X_Press_Done = 1;
Serial.println("X button pressed!");
}
Buttonstatus = WAIT_FOR_KEYRELEASE;
break;
case NONE :
Buttonstatus = WAIT_FOR_KEYPRESS;
break;
}
break;
case WAIT_FOR_KEYRELEASE :
switch(key) {
case LEFT :
Left_Press_Done = 0;
Buttonstatus = WAIT_FOR_KEYPRESS;
break;
case RIGHT :
Right_Press_Done = 0;
Buttonstatus = WAIT_FOR_KEYPRESS;
break;
case FWD :
Fwd_Press_Done = 0;
Buttonstatus = WAIT_FOR_KEYPRESS;
break;
case X :
X_Press_Done = 0;
Buttonstatus = WAIT_FOR_KEYPRESS;
break;
}
break;
default :
break;
}
}