I am an absolute beginner, never coded before, but I have worked with hardware a lot. I have an arduino Leonardo R3 and 14 digital buttons, 2 analog joysticks, I would like to make a USB controller that can be plug and play. I am fine with having to configure the controller, I just need it to recognize that it is a controller.
I really need help, because I've been trying to do this for 3 months, (even though I was told it would be simple) and I have not made any progress at all, so I obviously don't have the skills to code.
I also forgot to mention in this post but not all 14 buttons are necessary. I only need around 12 of them.
-
divide and conquer, don't try to do it all.
-
Show us your code. We won't write it for you, but can guide you back onto the road when you ditch it.
-
Show us how it's wired - schematic, please, fritzing is poor communication
-
show us a photo of your assembled circuit - much can be told by visual inspection
to make your post, you obviously entered the Leonardo Category. Pity you didn't notice this:
I see this topic was moved to General Guidance.
Do you in fact have a Leonardo, or an Uno?
I didn't move anything, but I'll post some of the code in a while. At around 3:00PM
likely a moderator, though that usually only happens when it's obvious that the post is in the wrong place. In this case, you referenced Leonardo, so I presumed Leonardo. We'll see what you have to say at 3; no idea when that is, this is a global forum, but hey, we'll see.
The board specific categories are intended for problems with the specific board and not so much for projects that involve the board. That is (more than likely) why it was moved.
As a self-declared "absolute beginner" you have chosen a very challenging project.
A good first step in your journey would be to methodically go through one of the code examples for creating an HID mouse from the Leonardo, and make sure that you understand every line of it. One such example, randomly chosen.
Oh, also if it wasn't already obvious, this is my first time posting on the Arduino forum
It's a bit early, but here's the code: (Though I don't think any of it actually properly works.)
void setup() {
// put your setup code here, to run once:
//Define pins for buttons
const int buttonPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; // Example pins for 10 buttons
const int numButtons = 10;
// Initialize serial communication
Serial.begin(9600);
// Set button pins as input with pull-up resistors
for (int i = 0; i < numButtons; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
}
void loop() {
// put your main code here, to run repeatedly:
// Define pins for joystick axes
const int joyX1 = A1; // Analog pin for joystick 1 X-axis
const int joyY1 = A2; // Analog pin for joystick 1 Y-axis
const int joyX2 = A3; // Analog pin for joystick 2 X-axis
const int joyY2 = A4; // Analog pin for joystick 2 Y-axis
int joyX1pin = 0;
int joyY1pin = 0;
int joyX2pin = 0;
int joyY2pin = 0;
// Read joystick values
joyX1pin = analogRead(joyX1);
joyY1pin = analogRead(joyY1);
joyX2pin = analogRead(joyX2);
joyY2pin = analogRead(joyY2);
//Define pins for buttons
const int buttonPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; // Example pins for 10 buttons
const int numButtons = 10;
// Read button states
String buttonStates = "";
for (int i = 0; i < numButtons; i++)
// DigitalRead returns LOW when button is pressed due to pull-up
if (digitalRead(buttonPins[i]) == LOW)
buttonStates = "1"; // Button pressed
else {
buttonStates = "0"; // Button not pressed
}
// Send data to Raspberry Pi in a structured format (e.g., CSV-like)
Serial.println(joyX1pin);
Serial.println(",");
Serial.println(joyY1pin);
Serial.println(",");
Serial.println(joyX2pin);
Serial.println(",");
Serial.println(joyY2pin);
Serial.println(",");
Serial.println(buttonStates); // Newline character to delimit messages
// Get rid of "ln" after testing on computer (I think)
delay(10); // Small delay to prevent overwhelming the serial buffer
}
Please explain what is "properly works" for you relating to the code?
The code needs to take the input from the 11 buttons (I forgot to add the 12th) and 2 control sticks, make the control sticks analog signal into a digital signal, and then send the digital signals to the Raspberry Pi (which will be expecting normal USB HID game controller inputs. It also easily lets you configure the buttons and joysticks, so as long as it thinks it's a HID controller it should work).
The code doesn't even begin to resemble an HID controller, but with some fixes, it could send button and joystick values to an RPi via a USB serial port connection.
Wait really? I thought this code was complete garbage, and I'd have to start over...
This part reads and reports only the status of the last button read (assuming that the wiring is correct).
for (int i = 0; i < numButtons; i++)
// DigitalRead returns LOW when button is pressed due to pull-up
if (digitalRead(buttonPins[i]) == LOW)
buttonStates = "1"; // Button pressed
else {
buttonStates = "0"; // Button not pressed
}
Why not actually test code before posting it?
I did test the code, but I was paying too much attention to If the sticks were working, to realize that the buttons weren't. So I'll try to find a fix to that first.
Nevermind, I'm really, really sorry for wasting your guys's time. I managed to find a tutorial while looking at ways to fix the button issue...