Under normal operation, the value of a joystick's ADC range on my board is from 0 to 1023 (10-bits). However, my graph for either axis is not continuous. Rather, there is more than one min and maximum for both axis. I am running this thread on a MSP432P401R however the code isn't any different than an Arduino.
int jumpFlag;
int backwardsFlag;
int forwardsFlag;
int selPin = P5_1; //Select digital pin location
int xOutPin = A14;
int yOutPin = A13;
void setupPlayerActions() {
pinMode(selPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(selPin),jumpISR,RISING);
//pinMode(xOutPin, ); //Analog X pin setup
//pinMode(xOutPin, ); //Analog Y pin setup
}
void jumpISR() {
Serial.println("Jump Detected!");
jumpFlag = 1;
}
void loopPlayerActions() {
int x_adc_val, y_adc_val;
int leftThreshold=50,rightThreshold=900;
float x_volt, y_volt;
x_adc_val = analogRead(xOutPin);
y_adc_val = analogRead(yOutPin);
x_volt = ( ( x_adc_val )); /*Convert digital value to voltage */
y_volt = ( ( y_adc_val)); /*Convert digital value to voltage */
if (x_volt < leftThreshold){
Serial.println("Move Backwards");
}
else if (x_volt < rightThreshold){
Serial.println("Move Forwards");
}
Serial.println("X_Voltage = ");
Serial.print(x_volt);
Serial.print("\t");
//Serial.print("Y_Voltage = ");
//Serial.println(y_volt);
delay(100);
//delay(250);
}
The graph goes to zero for all positive x and y integers however it briefly reaches a maximum past a small value on the -x and -y axis then sinks to about ~420. What could be causing this?
1 Answer 1
A 10 bit ADC may not have 10 bits of resolution for a given sample. It might only have 5 or 6 bits due to random noise and other factors. Consult the chip manufactures specifications / recommendations if you need a specific number.
What can be done?
- If response time is not an issue, consider averaging the ADC reading. A common coding practice is to use an exponential moving average.
- Consider a better ADC. Many times a dedicated ADC chip (not integrated into a processor) will have better specifications.
For more in depth reading this NXP (aka: Motorola, Freescale) application note discusses "How to Increase the Analog-to-Digital Converter Accuracy in an Application".
if (x_volt < rightThreshold)
": do you meanif (x_volt > rightThreshold)
?