I'm working on an Arduino sketch that will read in values from two potentiometers. The code for this is below:
int lastPotentiometerOneValue = 0;
int lastPotentiometerTwoValue = 0;
void setup() {
// initialize serial communication at 57600 bits per second:
Serial.begin(57600);
}
void loop() {
delay(10);
readAndSendPotentiometerDataIfChanged();
}
void readAndSendPotentiometerDataIfChanged(void) {
//Potentiometer One
int newPotentiometerOneValue = analogRead(A0) / 10.2;
if (newPotentiometerOneValue == lastPotentiometerOneValue) return;
Serial.print("!pos1");
Serial.print(newPotentiometerOneValue);
Serial.print(";");
lastPotentiometerOneValue = newPotentiometerOneValue;
//Potentiometer Two
int newPotentiometerTwoValue = analogRead(A1) / 10;
if (newPotentiometerTwoValue == lastPotentiometerTwoValue) return;
Serial.print("!pos2");
Serial.print(newPotentiometerTwoValue);
Serial.print(";");
lastPotentiometerTwoValue = newPotentiometerTwoValue;
}
The schematic for my circuit is below (I've left out the code for reading the state of the push-button in my listing above, as that's in a separate method from the method that reads in the potentiometer data):
However, there seems to be a rather subtle error I'm experiencing. If I turn Potentiometer One, it will display the correct output in the serial monitor. If I turn Potentiometer Two, no value will be displayed. However, if I remove Potentiometer Two from the circuit board and turn Potentiometer One, then the serial monitor will display the Potentiometer Two as having the same data as Potentiometer Two.
I'm sure the problem is with my circuit, but I'm not quite sure what I'm doing wrong. Does anyone have some insight? Thank you!