I'm digging into Arduino C and have written a module to read from a joystick, but I'm a bit worried about how I'm using global variables. I've done some reading on the potential pitfalls of globals—like maintainability and possible dangers—but I'm unsure if my use is acceptable in my specific case. I've put a lot of effort into keeping my joystick module neat and tidy. You can check out my module [here](https://github.com/hashtaglifesux/ELEGOO-Kit-module-drivers/blob/a38ea480b6ee0a941fb7cca23321af7d8bda06c7/src-arduino/joystick.h) and my test program [here](https://github.com/hashtaglifesux/ELEGOO-Kit-module-drivers/blob/main/tests/arduino/max7219_snake.ino). Can anyone tell me if my use of global variables is appropriate, especially in the joystick module?
2 Answers
You might want to consider how your project could handle multiple joysticks. If you keep your data in a struct and pass that struct as a parameter to your functions, it’ll make extending the project for things like a tank game or a two-player setup a breeze. Keeping things flexible is key!
Just to build on what TechSavvy42 mentioned, be cautious with global variables! If you have multiple instances of what you're controlling (like buttons or contexts), that can get messy. Using C functions efficiently can help you manage different contexts, making it easier to maintain and extend your code later on. Consider structuring your methods around passing the appropriate context rather than relying on globals.

Ok cool, I'll refactor to make room for any number of joysticks. Any other things to improve?