hey, arduino noob here working on my first non tutorial project.
I plan on using an arcade fight stick to control LED's and am trying to get the debouncing working well. I have finally deduced that my problems have to do with my code. Only my "left" button clicks/tilts of the stick work, regardless of the order of the code. I have it lighting up an RGB led, but digitally as if they were 3 LED's. i dont think its the circuitry thats a problem, because I also Serial.print each possibility, but only the left actually prints.
heres the code
const int leftPin = A1;
const int upPin = A2;
const int rightPin = A3;
const int downPin = A4;
const int bluePin = 10;
const int redPin = 11;
const int greenPin = 12;
const int ledPin = 13;
int redState = HIGH;
int leftState;
int blueState = HIGH;
int upState;
int greenState = HIGH;
int rightState;
int ledState = HIGH;
int downState;
int prvLeftState = LOW;
int prvUpState = LOW;
int prvRightState = LOW;
int prvDownState = LOW;
long lastDebounceTimeL = 0;
long lastDebounceTimeU = 0;
long lastDebounceTimeR = 0;
long lastDebounceTimeD = 0;
long debounceDelay = 50;
void setup(){
Serial.begin(9600);
pinMode(leftPin, INPUT);
pinMode(upPin, INPUT);
pinMode(rightPin, INPUT);
pinMode(downPin, INPUT);
pinMode(bluePin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(bluePin, blueState);
digitalWrite(redPin, redState);
digitalWrite(greenPin, greenState);
digitalWrite(ledPin, ledState);
}
void loop(){
int leftReading = digitalRead(leftPin);
int upReading = digitalRead(upPin);
int rightReading = digitalRead(rightPin);
int downReading = digitalRead(downPin);
// this is where the LEFT button and BLUE led debounce begins
//********************************************
//******************************************
//******************************************************
if (leftReading != prvLeftState){
lastDebounceTimeL = millis();
}
if ((millis() - lastDebounceTimeL) > debounceDelay){
if (leftReading != leftState){
leftState = leftReading;
if (leftState == LOW){
blueState = !blueState;
Serial.println("left");
}
}
}
// this is where the left button and blue led debounce ends
//********************************************
//******************************************
//******************************************************
//-------------------------------------------------------------
// this is where the UP button and RED led debounce begins
//********************************************
//******************************************
//******************************************************
if (upReading != prvUpState){
lastDebounceTimeU = millis();
}
if ((millis() - lastDebounceTimeU) > debounceDelay){
if (upReading != upState){
upState = upReading;
if (upState == LOW){
redState = !redState;
Serial.println("up");
}
}
}
// this is where the UP button and RED led debounce ends
//********************************************
//******************************************
//******************************************************
//-------------------------------------------------------------
// this is where the RIGHT button and GREEN led debounce begins
//********************************************
//******************************************
//******************************************************
if (rightReading != prvRightState){
lastDebounceTimeR = millis();
}
if ((millis() - lastDebounceTimeR) > debounceDelay){
if (rightReading != rightState){
rightState = rightReading;
if (rightState == LOW){
greenState = !greenState;
Serial.println("right");
}
}
}
// this is where the RIGHT button and GREEN led debounce ends
//********************************************
//******************************************
//******************************************************
//-------------------------------------------------------------
// this is where the DOWN button and ONBOARD led debounce begins
//********************************************
//******************************************
//******************************************************
if (downReading != prvDownState){
lastDebounceTimeD = millis();
}
if ((millis() - lastDebounceTimeD) > debounceDelay){
if (downReading != downState){
downState = downReading;
if (downState == LOW){
ledState = !ledState;
Serial.println("down");
}
}
}
digitalWrite(bluePin, blueState);
digitalWrite(redPin, redState);
digitalWrite(greenPin, greenState);
digitalWrite(ledPin, ledState);
prvLeftState = leftReading;
prvUpState = upReading;
prvRightState = rightReading;
prvDownState = downReading;
}
is there an aspect that I'm overlooking??