I had 8 buttons originally but removed 4 and added the joystick. All 8 buttons worked but most of them had to be held down to get them to work (except Button 1 which worked perfectly). They're all coded and wired the same way so they should only have to be pressed to give an input. Now, some of the buttons have to be held for a few seconds just for the input to register.
I attached a pic of the schematic. The buttons are each using a 10k resistor. I don't understand why they're behaving stangely.
#include <Joystick.h>
const int Button1 = 3; //Right side - bottom
const int Button2 = 4; //Right side - right
const int Button3 = 5; //Right side - top
const int Button4 = 6; //Right side - left
const int X_pin = A0; //X-Axis
const int Y_pin = A1; //Y-Axis
const int SW_pin = 2; //SW button (Click Joystick)
int buttonState = 0; // variable for reading the pushbutton status
int x,y,z;
long lastDebounceTime = 0;
long debounceDelay = 50;
void setup() {
Serial.begin(9600);
pinMode(Button1, INPUT);
pinMode(Button2, INPUT);
pinMode(Button3, INPUT);
pinMode(Button4, INPUT);
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
}
void loop() {
x = analogRead(X_pin);
if (x == 1023)
{
Serial.println("Up:");
}
else
x = analogRead(X_pin);
if (x == 0)
{
Serial.println("Down:");
}
else
y = analogRead(Y_pin);
if (y == 1023)
{
Serial.println("Right:");
}
y = analogRead(Y_pin);
if (y == 0)
{
Serial.println("Left:");
}
int z = digitalRead(SW_pin);
if (z == 0)
{
Serial.println("Enter:");
}
if ( (millis() - lastDebounceTime) > debounceDelay) {
buttonState = digitalRead(Button1);// read the state of the pushbutton value:
if (buttonState == HIGH) { // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
lastDebounceTime = millis();
Serial.print("1:");
}
else {
lastDebounceTime = millis();
}
}
if ( (millis() - lastDebounceTime) > debounceDelay) {
buttonState = digitalRead(Button2);// read the state of the pushbutton value:
if (buttonState == HIGH) { // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
lastDebounceTime = millis();
Serial.print("2:");
}
else {
lastDebounceTime = millis();
}
}
if ( (millis() - lastDebounceTime) > debounceDelay) {
buttonState = digitalRead(Button3);// read the state of the pushbutton value:
if (buttonState == HIGH) { // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
lastDebounceTime = millis();
Serial.print("3:");
}
else {
lastDebounceTime = millis();
}
}
if ( (millis() - lastDebounceTime) > debounceDelay) {
buttonState = digitalRead(Button4);// read the state of the pushbutton value:
if (buttonState == HIGH) { // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
lastDebounceTime = millis();
Serial.print("4:");
}
else {
lastDebounceTime = millis();
}
}
}