I am having a bit of trouble trying to read multiple Analog Input Pins (A0A0 and A3A3, 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).
// Read the Analog Pins and update the controller
for(uint8_t a = 0; a < TOTAL_AXIS_PINS; a++) {
checkAnalogAxisState(a, axisPins, axisPinVals);
delay(10);
}
// Read the Analog Pins and update the controller
for(uint8_t a = 0; a < TOTAL_AXIS_PINS; a++) {
checkAnalogAxisState(a, axisPins, axisPinVals);
delay(10);
}
// 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;
}
}
// 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;
}
}
Here is the full sketch, I can include the full contents of ControllerDefines.hControllerDefines.h and ControllerFunctions.hControllerFunctions.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);
}
#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);
}
Edit:Edit:
// Read the Analog Pins and update the controller
for(uint8_t a = 0; a < TOTAL_AXIS_PINS; a++) {
checkAnalogAxisState(a, axisPins, axisPinVals);
delay(10);
}
// For the plotter
Serial.print(axisPinVals[0]);
Serial.print(",");
Serial.println(axisPinVals[1]);
// Read the Analog Pins and update the controller
for(uint8_t a = 0; a < TOTAL_AXIS_PINS; a++) {
checkAnalogAxisState(a, axisPins, axisPinVals);
delay(10);
}
// For the plotter
Serial.print(axisPinVals[0]);
Serial.print(",");
Serial.println(axisPinVals[1]);
I would expect each variable (line) to track independently of one another, when the Pot is moved from A0A0 to A3A3.

