Hi guys,
I'm currently building a guitar tuner and am currently in the process of building the interface. I am using gamestates to represent the tuning of each string (standard tuning, E, A, D, G, B, e) and I have a strip of 6 LEDs with a button either side and when one LED is on, that will represent the string being tuned. The way you cycle through the gamestates is using the buttons, when you press the left one, the next LED to the left of the current one will turn on with every press and the same for the right button. However, when I upload the code, only the right button works and not the left. Both buttons are showing as high in the serial monitor when pressed. Any suggestions? Thanks!
const int stateLowE = 0;
const int stateA = 1;
const int stateD = 2;
const int stateG = 3;
const int stateB = 4;
const int stateHighE = 5;
int leftButton = 6;
int rightButton = 7;
int stateLeft = 0;
int stateRight = 0;
int lowE = 8;
int A = 9;
int D = 10;
int G = 11;
int B = 12;
int highE = 13;
int gameState;
void setup() {
pinMode(leftButton, INPUT);
pinMode(rightButton, INPUT);
pinMode(lowE, OUTPUT);
pinMode(A, OUTPUT);
pinMode(D, OUTPUT);
pinMode(G, OUTPUT);
pinMode(B, OUTPUT);
pinMode(highE, OUTPUT);
gameState = stateLowE;
Serial.begin(9600);
}
void loop() {
if(gameState == stateLowE) {
tuneLowE();
} else if (gameState == stateA){
tuneA();
} else if (gameState == stateD){
tuneD();
} else if (gameState == stateG){
tuneG();
} else if (gameState == stateB){
tuneB();
} else if (gameState == stateHighE){
tuneHighE();
}
Serial.println(digitalRead(rightButton));
//Serial.println(digitalRead(leftButton));
}
void tuneLowE()
{
stateLeft = digitalRead(leftButton);
stateRight = digitalRead(rightButton);
digitalWrite(lowE, HIGH);
digitalWrite(A, LOW);
digitalWrite(D, LOW);
digitalWrite(G, LOW);
digitalWrite(B, LOW);
digitalWrite(highE, LOW);
if(stateRight == HIGH){
delay(500);
gameState = stateA;
} else if (stateRight == LOW){
gameState = stateLowE;
} else if(stateLeft == HIGH){
delay(500);
gameState = stateLowE;
} else if (stateLeft == LOW){
gameState = stateLowE;
}
}
void tuneA(){
stateLeft = digitalRead(leftButton);
stateRight = digitalRead(rightButton);
digitalWrite(lowE, LOW);
digitalWrite(A, HIGH);
digitalWrite(D, LOW);
digitalWrite(G, LOW);
digitalWrite(B, LOW);
digitalWrite(highE, LOW);
if(stateRight == HIGH){
delay(500);
gameState = stateD;
} else if (stateRight == LOW){
gameState = stateA;
} else if(stateLeft == HIGH){
delay(500);
gameState = stateLowE;
} else if (stateLeft == LOW){
gameState = stateA;
}
}
void tuneD(){
stateLeft = digitalRead(leftButton);
stateRight = digitalRead(rightButton);
digitalWrite(lowE, LOW);
digitalWrite(A, LOW);
digitalWrite(D, HIGH);
digitalWrite(G, LOW);
digitalWrite(B, LOW);
digitalWrite(highE, LOW);
if(stateRight == HIGH){
delay(500);
gameState = stateG;
} else if (stateRight == LOW){
gameState = stateD;
} else if(stateLeft == HIGH){
delay(500);
gameState = stateA;
} else if (stateLeft == LOW){
gameState = stateD;
}
}
void tuneG(){
stateLeft = digitalRead(leftButton);
stateRight = digitalRead(rightButton);
digitalWrite(lowE, LOW);
digitalWrite(A, LOW);
digitalWrite(D, LOW);
digitalWrite(G, HIGH);
digitalWrite(B, LOW);
digitalWrite(highE, LOW);
if(stateRight == HIGH){
delay(500);
gameState = stateB;
} else if (stateRight == LOW){
gameState = stateG;
} else if(stateLeft == HIGH){
delay(500);
gameState = stateD;
} else if (stateLeft == LOW){
gameState = stateG;
}
}
void tuneB(){
stateLeft = digitalRead(leftButton);
stateRight = digitalRead(rightButton);
digitalWrite(lowE, LOW);
digitalWrite(A, LOW);
digitalWrite(D, LOW);
digitalWrite(G, LOW);
digitalWrite(B, HIGH);
digitalWrite(highE, LOW);
if(stateRight == HIGH){
delay(500);
gameState = stateHighE;
} else if (stateRight == LOW){
gameState = stateB;
} else if(stateLeft == HIGH){
delay(500);
gameState = stateG;
} else if (stateLeft == LOW){
gameState = stateB;
}
}
void tuneHighE(){
stateLeft = digitalRead(leftButton);
stateRight = digitalRead(rightButton);
digitalWrite(lowE, LOW);
digitalWrite(A, LOW);
digitalWrite(D, LOW);
digitalWrite(G, LOW);
digitalWrite(B, LOW);
digitalWrite(highE, HIGH);
if(stateRight == HIGH){
delay(500);
gameState = stateHighE;
} else if (stateRight == LOW){
gameState = stateHighE;
} else if(stateLeft == HIGH){
delay(500);
gameState = stateB;
} else if (stateLeft == LOW){
gameState = stateHighE;
}
}