Reputation: 420
I'm trying to create a simple 2-Axis, 2-Button joystick using an off-brand ATmega32U4 board, which registers as an Arduino Leonardo.
I've got it working just fine on the serial monitor in Arduino IDE, and it shows up as a game controller with the correct amount of inputs in "Devices and Printers", but for some reason the serial data isn't transferring to the Game Controller test dialog, so the joystick isn't working outside of the serial monitor.
This is my first project with Arduino, so I'm sure I'm just missing something simple here, but honestly, I'm stumped.
After exhausting all of my Google-fu, and some trial and error, this is the best I could come up with, so I'd appreciate any help I can get in order to get this working properly. I'm using THIS joystick library.
// Pin A2 = X Axis
// Pin A3 = Y Axis
// Pin 16 = Button 1
// Pin 10 = Button 2
//
//--------------------------------------------------------------
#include "Joystick.h"
// 2 Buttons, No hats, X and Y axis, no Z axis, Rx, Ry, Rz, Rudder, Throttle, Brake, or Steering
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_JOYSTICK, 2, 0, true, true, false, false, false, false, false, false, false, false, false);
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(16, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
Joystick.begin();
Joystick.setXAxisRange(-512, 512);
Joystick.setYAxisRange(-512, 512);
}
void loop() {
// X-Axis
int xAxis = analogRead(A2);
Joystick.setXAxis(xAxis);
Serial.println(xAxis);
// Y-Axis
int yAxis = analogRead(A3);
Joystick.setYAxis(yAxis);
Serial.println(yAxis);
// Button 1
int button1State = !digitalRead(16);
if (button1State = 1)
{
Joystick.pressButton(0);
}
// Button 2
int button2State = !digitalRead(10);
if (button2State = 1)
{
Joystick.pressButton(1);
}
}
I've added delays in my loop, which I thought seemed to fix my issue (and made me notice another issue), but for some reason it only seems to work part of the time... Sometimes it works just right, other times the test dialog doesn't notice my inputs, and other times the dialog box just freezes... Here are the changes I've made:
// X-Axis
int xAxis = analogRead(A2);
Joystick.setXAxis(xAxis);
Serial.println(xAxis);
delay(50);
// Y-Axis
int yAxis = analogRead(A3);
Joystick.setYAxis(yAxis);
Serial.println(yAxis);
delay(50);
// Button 1
int button1State = !digitalRead(16);
if (button1State == 1)
{
Joystick.pressButton(0);
delay(50);
}
// Button 2
int button2State = !digitalRead(10);
if (button2State == 1)
{
Joystick.pressButton(1);
delay(50);
}
Upvotes: 1
Views: 1849
Reputation: 420
After trying just about everything else I decided to try commenting out all of the serial communication and now it works! I'm not experienced enough with Arduino to know what part of the serial communication I had written was messing things up, but my sketch works just fine without it. (except for one of my buttons, but I think that might just be a faulty pin)
This is what I ended up with in case someone else finds this useful:
// Pin A2 = X Axis
// Pin A3 = Y Axis
// Pin 16 = Button 1
// Pin 10 = Button 2
//
//--------------------------------------------------------------
#include "Joystick.h"
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_JOYSTICK, 2, 0, true, true, false, false, false, false, false, false, false, false, false);
void setup() {
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(16, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
Joystick.begin();
}
void loop() {
// X-Axis
int xAxis = analogRead(A2);
Joystick.setXAxis(xAxis);
// Y-Axis
int yAxis = analogRead(A3);
Joystick.setYAxis(yAxis);
// Button 1
int button1State = !digitalRead(16);
if (button1State == 1)
{
Joystick.pressButton(0);
}
else
{
Joystick.releaseButton(0);
}
// Button 2
int button2State = !digitalRead(10);
if (button2State == 1)
{
Joystick.pressButton(1);
}
else
{
Joystick.releaseButton(1);
}
delay(50);
}
Upvotes: 1
Reputation: 6809
while (!Serial)
waits for the serial connection to open. When you open the Arduino IDE's serial monitor your code starts to execute. If you don't, the code won't run and no inputs will be sent to the PC.
Remove the
while (!Serial) {
;
}
it's only intended for debugging, so the program starts when you open the serial monitor and you don't miss any debugging messages at the beginning.
Upvotes: 1
Reputation: 1776
You probably want to use == inside the 'if' statement to check for equality, now it is assigning the value 1 regardless of the input.
Upvotes: 3