How to make this code better? (2x2 macro test)

// including keyboard n setting up pins + state
#include <Keyboard.h>
int state = digitalRead(2 & 4);
int state2 = digitalRead(3 & 4);
int state3 = digitalRead(5 & 2);
int state4 = digitalRead(5 & 3);
void setup() {
pins();
Keyboard.begin();
}

void loop() {
// 2x2 button looper
w;
a;
s;
d;
}

void pins() {
// defining pins
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
}
// Button press actions
void w() {
if (state == HIGH) {
delay(50);
Keyboard.press('w');
delay(50);
} else {Keyboard.release('w');
}
}
void a() {
if (state2 == HIGH) {
delay(50);
Keyboard.press('a');
delay(50);
} else {Keyboard.release('w');
}
}
void s() {
if (state3 == HIGH) {
delay(50);
Keyboard.press('s');
delay(50);
} else {Keyboard.release('w');
}
}
void d() {
if (state4 == HIGH) {
delay(50);
Keyboard.press('d');
delay(50); } else {Keyboard.release('w');
}
}

you can improve this code just by putting it in code tags

If that code actually compiles I would be surprised.

And don't forget to format it using the 'auto format' tool in the Arduino IDE.

What is this meant to do? & is the bitwise "and" operator... I have no idea what you're planning to use it for here.

You must always put parentheses at the end of a function call regardless of whether or not the function actually takes in arguments, so w; should be w();... etc.

I would advise against this... just put the pinMode()s in the setup. There is no enclosing function necessary for them, especially since you're only using 4 pins.

While this isn't (strictly) a problem, I hope that you realize that you're not actually dynamically checking for an updated state, since you're not calling digitalRead() to update the state in the loop... you put that at the top of your sketch instead (yes, this should be inside your loop :smiley: ). Additionally, digitalRead() returns a boolean, so it might be a good idea to make your states type bool instead. This goes for each state variable function.

Um, no.

int digitalRead(uint8_t pin)
{
int state = digitalRead(2 & 4);
int state2 = digitalRead(3 & 4);
int state3 = digitalRead(5 & 2);
int state4 = digitalRead(5 & 3);

The & does a bitwise AND of the two numbers, resulting in the first three statements reading port 0, the last statement reads port 1. This sets the initial value of the four variables, which will remain this value forever because you never change any of them anywhere in the code. These digitalReads are executed BEFORE setup() runs.

The most important thing you can do, to improve your code, is to stop guessing C language.

There is nothing wrong with it

it did...

ok thx, but how do i fix it?

the & is used in my code (Im guessing it works like that, i dont really know) to register a button press when two pins are "HIGH" or on.
4...5
2w...a
3 s...d

Here is your problem, don’t guess - know

Ok i think i fixed it...
// including keyboard n setting up pins + state
#include <Keyboard.h>
int state, state2, state3, state4;
void setup() {
pins();
Keyboard.begin();
}

void loop() {
// 2x2 button looper
state = digitalRead(2);
state2 = digitalRead(3);
state3 = digitalRead(4);
state4 = digitalRead(5);
w;
a;
s;
d;
}

void pins() {
// defining pins
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
}
// Button press actions
void w() {
if (state == HIGH && state3 == HIGH) {
delay(50);
Keyboard.press('w');
delay(50);
} else {Keyboard.release('w');
}
}
void a() {
if (state2 == HIGH && state3 == HIGH) {
delay(50);
Keyboard.press('a');
delay(50);
} else {Keyboard.release('w');
}
}
void s() {
if (state4 == HIGH && state == HIGH) {
delay(50);
Keyboard.press('s');
delay(50);
} else {Keyboard.release('w');
}
}
void d() {
if (state4 == HIGH && state2 == HIGH) {
delay(50);
Keyboard.press('d');
delay(50); } else {Keyboard.release('w');
}
}
but the buttons are still not working...

never mind i found the problem, but thx for helping... :smiley:

Looks like you paid no attention to the comments in this thread, but good luck with your project

i already found it, u dont have to point it out again, but thx for atleast posting it.
Also it is my first time coding for an arduino project.

then post an update