Hi! I am using an arduino micro to build essentially a button box with analogs rockers, buttons and rotary encoders. I having trouble right now, because I need 8 digital pins for the rotaries, 16 buttons and all 5 analog pins. This means the analogs the analog pins will have to be used as analog and digital simultaneously. I am using USB game controller software on Microsoft.
I have gotten it to work partially. The problem I have run into is when I use the analogs, they map correctly but also trigger a button press. Has anyone run into this problem? Is there a way to stop this from happening? The follow code doesn't include the rotaries.
#include <Keypad.h>
#include <Joystick.h>
Joystick_ Joystick(
JOYSTICK_DEFAULT_REPORT_ID,
JOYSTICK_TYPE_JOYSTICK,
24, 0, // Button Count, Hat Switch Count
true, true, true, // X and Y (joystick), and Z Axis
true, true, true, // Rx, Ry, or Rz
true, false, // Rudder or throttle
false, false, false); // accelerator, brake, steering
// define axis values
int xAxis_ = 0;
int yAxis_ = 0;
int zAxis_ = 0;
int rxAxis_ = 0;
int ryAxis_ = 0;
int rzAxis_ = 0;
bool xAxisChanged = true;
bool yAxisChanged = true;
bool ZAxisChanged = true;
bool RxAxisChanged = true;
bool RyAxisChanged = true;
bool RzAxisChanged = true;
// dead zone for analog read, used over the 0-1023 input range
const int axisDeadZone = 30; // ~5% This is for spring rockers with deadzones at center
const int DialDeadZone = 100; //This is rising curve filter for dials (beause of getting grounding noise)
const int PotDeadZone = 10; // We were't getting grounding noise on this pin so can be higher resolution
// initial value read for each analog axis
int axisInit = 514;
const bool initAutoSendState = true;
void setup() {
// put your setup code here, to run once:
// put your setup code here, to run once:
Serial.begin(9600);
// initialize btn pins
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);
pinMode(13, INPUT_PULLUP);
pinMode(14, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP);
pinMode(16, INPUT_PULLUP);
pinMode(17, INPUT_PULLUP);
pinMode(18, INPUT_PULLUP);
pinMode(19, INPUT_PULLUP);
pinMode(20, INPUT_PULLUP);
pinMode(21, INPUT_PULLUP);
pinMode(22, INPUT_PULLUP);
pinMode(23, INPUT_PULLUP);
Joystick.begin();
}
void loop() {
// put your main code here, to run repeatedly:
buttons();
//analogs();
}
void buttons() {
// Read pin values
int pinCount = 8; // start count for while loop, represents pins on arduino, first btn pin will be pin 8
int btnCount = 0; // start count for while loop, represents btn number on usb tester software, btn 0 represents btn 1 on game controller
int buttonState = 0; // define variable for button state, this will be the digital value read from the arduino pins
while (pinCount < 24) { // while loop, uses pin count as parameter, pinCount will always be from 8 to 23
if (pinCount == 11) {
digitalWrite(pinCount, HIGH); // solved the problem with digital pin 17 (SS) being stuck on LOW
buttonState = digitalRead(pinCount); // digital read of arduino pin
if (buttonState == LOW) { // if button is pressed for given pin, buttonState == LOW
Joystick.pressButton(btnCount); // presses button for give place on usb game controller
}
else { // if button is not pressed
Joystick.releaseButton(btnCount); // releases button after it has been pressed
analogs();
}
btnCount = btnCount + 1; // adds one to btnCount
pinCount = pinCount + 1; // adds one to pinCount, when pinCount exceeds 23 while loop will terminate
}
else{
digitalWrite(pinCount, HIGH); // solved the problem with digital pin 17 (SS) being stuck on LOW
buttonState = digitalRead(pinCount); // digital read of arduino pin
if (buttonState == LOW) { // if button is pressed for given pin, buttonState == LOW
Joystick.pressButton(btnCount); // presses button for give place on usb game controller
}
else { // if button is not pressed
Joystick.releaseButton(btnCount); // releases button after it has been pressed
}
btnCount = btnCount + 1; // adds one to btnCount
pinCount = pinCount + 1; // adds one to pinCount, when pinCount exceeds 23 while loop will terminate
}
}
delay(50);
}
void analogs() {
// Read analog values
// Reads analog values from variable an1 (pin A0), maps onto usb game controller
xAxis_ = analogRead(A0);
xAxis_ = map(xAxis_,100,925,0,1023);
Joystick.setXAxis(xAxis_);
// Reads analog values from variable an2 (pin A1), maps onto usb game controller
yAxis_ = analogRead(A1);
yAxis_ = map(yAxis_,100,925,0,1023);
Joystick.setYAxis(yAxis_);
// Reads analog value from variable an3 (pin A2), maps onto usb game controller
zAxis_ = analogRead(A2);
zAxis_ = map(zAxis_,100,925,0,1023);
Joystick.setZAxis(zAxis_);
// Reads analog value from variable an4 (pin A3), maps onto usb game controller
rxAxis_ = analogRead(A3);
rxAxis_ = map(rxAxis_,100,925,0,1023);
Joystick.setRxAxis(rxAxis_);
// Reads analog value from variable an5 (pin A4), maps onto usb game controller
ryAxis_ = analogRead(A4);
ryAxis_ = map(ryAxis_,100,925,0,1023);
Joystick.setRyAxis(ryAxis_);
// Reads analog value from variable an6 (pin A5), maps onto usb game controller
rzAxis_ = analogRead(A5);
rzAxis_ = map(rzAxis_,100,925,0,1023);
Joystick.setRzAxis(rzAxis_);
delay(50);
}
type or paste code here
type or paste code here