Hello. i ma making a mech sim controller with arduino leonardo and run into an issue. Sometimes, completly random, buttons are pressed without the actualy presseed.i suspect that it is because i have connected 11 buttons with a 300Ω resistor each.they are two button pins, i have connected on pin to 5v ,and the other the digital pin and a resistor that leads to ground. I am also using two keystudio analog joysticks.What have i done wrong? Should i wire the buttons a different way or change something in my sketch?
just show schematic
what? one single dual double button pin?
yeah, do something, post it for example
I cant realy post my schematic but i will post my sketch.
#include "Keyboard.h"
#include <Mouse.h>
byte up1 = 7;
byte down1 = 3;
byte left1 = 4;
byte right1 = 2;
byte trigger1 = 5;
byte alt1 = 6;
byte up2 = 12;
byte down2 = 13;
byte left2 = 11;
byte right2 = 10;
byte trigger2 = 9;
byte alt2 = 8;
byte wepon1 = 49;
byte wepon2 = 50;
byte wepon3 = 51;
byte thermal = 110;
byte target = 114;
byte jump = 32;
byte overdrive = 79;
byte power = 112;
byte interact = 101;
byte stop = 120;
byte MFS = 71;
byte forwards = 119;
byte backwords = 115;
byte left = 97;
byte right = 100;
byte center = 102;
void setup() {
Keyboard.begin();
Mouse_init();
Serial.begin(9600);
pinMode(A3,INPUT);
pinMode(A2,INPUT);
}
void loop() {
Mouse.move(readAxis(A1), readAxis(A0));
movement();
Check_button(left1,wepon1,1);
Check_button(up1,wepon2,1);
Check_button(trigger1,wepon3,1);
Check_button(alt1,thermal,1);
Check_button(right1,target,1);
Check_button(up2,stop,1);
Check_button(down2,center,1);
Check_button(alt2,power,1);
Check_button(trigger2,jump,1);
Check_button(left2,overdrive,1);
Check_button(right2,interact,1);
if(down1 == 1){
Mouse.press(MOUSE_MIDDLE);
}
if(down1 == 0){
Mouse.release(MOUSE_MIDDLE);
}
delay(6);
}
int readAxis(int thisAxis) {
// read the analog input:
int range = 30;
int threshold = range / 4;
int center = range / 2;
int reading = analogRead(thisAxis);
// map the reading from the analog input range to the output range:
reading = map(reading, 0, 1024, 0, range);
// if the output reading is outside from the rest position threshold, use it:
int distance = reading - center;
if (abs(distance) < threshold) {
distance = 0;
}
distance = -distance;
delay(5);
// return the distance for this axis:DwDw11111
return distance;
}
void movement(){
int value_x;
int value_y;
value_x = analogRead(A3);
value_y = analogRead(A2);
if(value_x < 200){
Keyboard.press(68);
}
if(value_x > 600){
Keyboard.press(97);
}
if(value_y < 200){
Keyboard.press(119);
}
if(value_y > 600){
Keyboard.press(115);
}//
if(value_x > 200 && value_x < 600){
Keyboard.release(68);
Keyboard.release(97);
}
if(value_y > 200 && value_y < 600){
Keyboard.release(119);
Keyboard.release(115);
}
}
void Mouse_init(){
pinMode(A0,INPUT);
pinMode(A1,INPUT);
pinMode(A2,INPUT);
pinMode(2,INPUT);
pinMode(3,INPUT);
pinMode(4,INPUT);
Mouse.begin();
}
bool Check_button(byte pin,byte key,bool on_state){
if(digitalRead(pin) == on_state){
Keyboard.press(key);
}
if(digitalRead(pin) == !on_state){
Keyboard.release(key);
}
delay(1);
}
yes i will post picture for refrence

Why do you have 11 push buttons on 2 pins? Also, I'd give the pin numbers a variable name, it's easier to get help that way if things have a name
are you playing with keyboard or joystick?
oh, just button, nothing special.
the arduino leonardo will act as a controller by sending keystrokes with each button press
you can see that in the sketch i posted
no each pushbutton has it's own pin, from pin 2 to pin 13
do you meant if(digitalRead(down1) == 1) ?
yea ignore tha bit of code
why?
because it is wrong as you pointed out
i dont think the code has any mistakes but the wiring might be wrong. i suspect that if each button has an 300Ω resistor, the arduino leonardo cant take it
1K would be bettor but it does not explain the random button pushing.
Maybe you have faulty buttons or wiring
this is exactly reason for your issue
make your work, declare button pins as pullup, i marked in sketch
#include "Keyboard.h"
#include <Mouse.h>
//buttons pins
const byte up1 = 7;
byte down1 = 3;
byte left1 = 4;
byte right1 = 2;
byte trigger1 = 5;
byte alt1 = 6;
byte up2 = 12;
byte down2 = 13;
byte left2 = 11;
byte right2 = 10;
byte trigger2 = 9;
byte alt2 = 8;
//Key codes
const byte wepon1 = 49;
byte wepon2 = 50;
byte wepon3 = 51;
byte thermal = 110;
byte target = 114;
byte jump = 32;
byte overdrive = 79;
byte power = 112;
byte interact = 101;
byte stop = 120;
byte MFS = 71;
byte forwards = 119;
byte backwords = 115;
byte left = 97;
byte right = 100;
byte center = 102;
void setup() {
Keyboard.begin();
Mouse.begin();
pinMode(up1, INPUT_PULLUP);
// and all your button pins here <<------------------<<
}
void loop() {
Mouse.move(readAxis(A1), readAxis(A0));
movement();
Check_button(left1, wepon1, 0);
Check_button(up1, wepon2, 0);
Check_button(trigger1, wepon3, 0);
Check_button(alt1, thermal, 0);
Check_button(right1, target, 0);
Check_button(up2, stop, 0);
Check_button(down2, center, 0);
Check_button(alt2, power, 0);
Check_button(trigger2, jump, 0);
Check_button(left2, overdrive, 0);
Check_button(right2, interact, 0);
if (digitalRead(down1))Mouse.press(MOUSE_MIDDLE);
else Mouse.release(MOUSE_MIDDLE);
delay();
}
int readAxis(int thisAxis) {
// read the analog input:
int range = 30;
int threshold = range / 4;
int center = range / 2;
int reading = analogRead(thisAxis);
// map the reading from the analog input range to the output range:
reading = map(reading, 0, 1024, 0, range);
// if the output reading is outside from the rest position threshold, use it:
int distance = reading - center;
distance = -distance;
if (abs(distance) < threshold) {
distance = 0;
}
return distance;
}
void movement() {
int value_x = analogRead(A3);
if (value_x < 200)Keyboard.press(68);
else if (value_x > 600)Keyboard.press(97);
else {
Keyboard.release(68);
Keyboard.release(97);
}
int value_y = analogRead(A2);
if (value_y < 200) Keyboard.press(119);
else if (value_y > 600) Keyboard.press(115);
else {
Keyboard.release(119);
Keyboard.release(115);
}
}
bool Check_button(byte pin, byte key, bool on_state) {
if (digitalRead(pin) == on_state) Keyboard.press(key);
else Keyboard.release(key);
}
one controller for one single game? why you not made joystick, or mech game accept no joysticks, only keyboard?
Thanks, i didnt even knew input pullup existed in arduino ide. Also, i found it easier to make mech controller as shield than make a universal controller for all games. i just clip on the correct shield for what game i wnat to play.
do you hear about "Xpadder.exe" ?