I am having a bit of trouble trying to read multiple Analog Input Pins (A0 and A3, in this instance). I am using a Pololu A-Star 32U4 LV Robot Controller (https://www.pololu.com/product/3116), and am using the Arduino IDE to program the board; it is recognized as a Arduino Leonardo. I am using the Joystick Library (https://github.com/MHeironimus/ArduinoJoystickLibrary/tree/version-2.0) to have the controller be recognized by Windows as a Game Pad (irrelevant to the issue I am seeing, I believe).
The issue that I am specifically running into is when I attempt to get the analog value for each pin that I am monitoring, it appears that the values of the other pins change at the same time. Because I am trying to use the Analog Pin values to drive an X/Y Axis, this issue manifests itself in an X/Y graph that appears linear with some sort of defined slope (with an X change leading to a Y change); rather than the expected X Horizontal only movement and Y Vertical only movement.
Here is where I check the pins in the loop:
// Read the Analog Pins and update the controller
for(uint8_t a = 0; a < TOTAL_AXIS_PINS; a++) {
checkAnalogAxisState(a, axisPins, axisPinVals);
delay(10);
}
And here is where I attempt to send the X/Y Axis changes based off of the readings coming from the Analog Pins:
// Checks the Analog Pin for a value to be used as the Axis value. Only updates the state if it
// has been found to have changed. Will update the Joystick Axis based on which pin it is currently
// looking at
void checkAnalogAxisState(uint8_t pinIndex, uint8_t analogPins[], int analogPinVals[]) {
uint8_t analogPin = analogPins[pinIndex];
// The trick when using multiple analog sensors is to read them twice, with a small delay after each read (10ms is good)
// then discard the first reading. This is because the ADC multiplexer needs switching time and the voltage needs
// time to stabilize after switching.
int currentState = analogRead(analogPin);
delay(10);
currentState = analogRead(analogPin);
delay(10);
if(currentState <= analogPinVals[pinIndex] - 5 || currentState >= analogPinVals[pinIndex] + 5) {
if(analogPin == X_AXIS_PIN) {
Joystick.setXAxis(currentState - ANALOG_ADJUSTMENT);
} else if(analogPin == Y_AXIS_PIN) {
Joystick.setYAxis(currentState - ANALOG_ADJUSTMENT);
}
analogPinVals[pinIndex] = currentState;
}
}
I try to keep the Pin and the Value of that pin in an array, but even when I do this with 2 separate variable and function calls, I still see that the Analog values change at the same time.
Here is the full sketch, I can include the full contents of ControllerDefines.h and ControllerFunctions.h if they will be helpful, but I have pulled the relevant functions from each:
#include <AStar32U4.h>
#include <Joystick.h>
#include "libraries/ControllerDefines.h"
#include "libraries/ControllerFunctions.h"
void setup() {
// Setup pins for the Axis controls
for(uint8_t a = 0; a < TOTAL_AXIS_PINS; a++) {
pinMode(axisPins[a], INPUT);
}
// Setup Joystick
Joystick.begin();
Joystick.setXAxisRange(-ANALOG_ADJUSTMENT, ANALOG_ADJUSTMENT);
Joystick.setYAxisRange(-ANALOG_ADJUSTMENT, ANALOG_ADJUSTMENT);
// Setup Serial Comm
Serial.begin(9600);
}
void loop() {
// Read all button states and update the controller
for(uint8_t b = 0; b < TOTAL_BUTTONS; b++) {
checkButtonState(b, buttons, buttonStates);
}
// Read the Analog Pins and update the controller
for(uint8_t a = 0; a < TOTAL_AXIS_PINS; a++) {
checkAnalogAxisState(a, axisPins, axisPinVals);
delay(10);
}
// Delay before polling buttons again
delay(50);
}