Skip to main content

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.

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).

  // 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;
    }
}

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);
}

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]);

I would expect each variable (line) to track independently of one another, when the Pot is moved from A0 to A3.

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).

      // 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;
        }
    }

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);
    }

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]);

I would expect each variable (line) to track independently of one another, when the Pot is moved from A0 to A3.

added 703 characters in body
Source Link

I have taken a plot of the 2 Array values over time, as can be seen here. This is with the Pot hooked up to A3, which is axisPinVals[0]: Plotted Values

Moving the Pot to A0 gives the following: Plotted Values 2

As you can see, the two values track together. This same behavior is not present in the buttons presses, and multiple buttons can be held at the same time with no issue.

I would expect each variable (line) to track independently of one another, when the Pot is moved from A0 to A3.

I have taken a plot of the 2 Array values over time, as can be seen here: Plotted Values

As you can see, the two values track together. This same behavior is not present in the buttons presses, and multiple buttons can be held at the same time with no issue.

I have taken a plot of the 2 Array values over time, as can be seen here. This is with the Pot hooked up to A3, which is axisPinVals[0]: Plotted Values

Moving the Pot to A0 gives the following: Plotted Values 2

As you can see, the two values track together. This same behavior is not present in the buttons presses, and multiple buttons can be held at the same time with no issue.

I would expect each variable (line) to track independently of one another, when the Pot is moved from A0 to A3.

added 703 characters in body
Source Link

I am simulating an Axis by using a potentiometer, with the data line running into the Analog Input, the 5V baseline coming from the board, and the Ground on the opposite of the Analog Input. This does indeed cause the values to change.

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.

Further.. when I remove the second Axis (ie. only using X or only using Y), it appears to track as I expect, with the Pot moving the axis (changing the value of the Analog Input).

#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:

I have taken a plot of the 2 Array values over time, as can be seen here: Plotted Values

Using the modified code below:

  // 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]);

As you can see, the two values track together. This same behavior is not present in the buttons presses, and multiple buttons can be held at the same time with no issue.

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.

#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);
}

I am simulating an Axis by using a potentiometer, with the data line running into the Analog Input, the 5V baseline coming from the board, and the Ground on the opposite of the Analog Input. This does indeed cause the values to change.

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.

Further.. when I remove the second Axis (ie. only using X or only using Y), it appears to track as I expect, with the Pot moving the axis (changing the value of the Analog Input).

#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:

I have taken a plot of the 2 Array values over time, as can be seen here: Plotted Values

Using the modified code below:

  // 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]);

As you can see, the two values track together. This same behavior is not present in the buttons presses, and multiple buttons can be held at the same time with no issue.

Source Link
Loading