Skip to content

Commit ba0a087

Browse files
committed
test reading potentiometers with analog inputs
To demo reading the analogs potentiometers continuously and still be able to see button presses, skip printing the analogs while a button is down.
1 parent bb298fd commit ba0a087

File tree

1 file changed

+64
-2
lines changed

1 file changed

+64
-2
lines changed

phoenix-arduino.ino

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
/* Optional delay between shift register reads.
2424
*/
25-
#define POLL_DELAY_MSEC 1
25+
#define POLL_DELAY_MSEC 10
2626

2727
const size_t numButtonGroups = 3;
2828

@@ -59,6 +59,48 @@ void setup()
5959
hc165_collection_setup(hc165_collection);
6060
hc165_collection_read(hc165_collection);
6161
hc165_collection_print(hc165_collection);
62+
63+
analogReference(DEFAULT);
64+
pinMode(A0, INPUT);
65+
pinMode(A1, INPUT);
66+
pinMode(A8, INPUT);
67+
pinMode(A9, INPUT);
68+
}
69+
70+
/*
71+
* Attempting to read the voltage across only a potentiometer will always
72+
* return 1023. The voltage difference is 5V because there is nothing else in
73+
* the circuit. To measure the resistance, we use a reference resistor in
74+
* series with the potentiometer:
75+
*
76+
* 5V --v^v^v-- V1 --v^v^v-- GROUND
77+
* Rpot Rref
78+
*
79+
* To solve for Rpot, use Ohm's law and the fact that 2 resistors in series
80+
* must have the same current:
81+
*
82+
* (Vsupply - V1) / Rpot = (V1 - 0V) / Rref
83+
* (Vsupply - V1) * Rref = V1 * Rpot
84+
* (Vsupply / V1 - 1) * Rref = Rpot
85+
* Vsupply * Rref / V1 - Rref = Rpot
86+
*
87+
* The ADC maps 5V (assumed to be both the ADC reference voltage and the supply
88+
* voltage) to a value 0 - 1023 (kAdcOutputMax). Since only the ratio of
89+
* Vsupply and V1 is important, we can treat both as ADC outputs instead of
90+
* converting from ADC to voltage on both sides.
91+
*
92+
* See http://www.built-to-spec.com/blog/2009/09/10/using-a-pc-joystick-with-the-arduino/
93+
*/
94+
const long kDivResistanceKOhm = 100;
95+
const long kAdcOutputMax = 1023;
96+
97+
uint8_t get_potentiometer_resistance(int adc_in)
98+
{
99+
if(adc_in > 0) {
100+
return uint8_t((kAdcOutputMax * kDivResistanceKOhm) / adc_in - kDivResistanceKOhm);
101+
} else {
102+
return 0;
103+
}
62104
}
63105

64106
void loop()
@@ -68,5 +110,25 @@ void loop()
68110
hc165_collection_print(hc165_collection);
69111
}
70112

113+
bool isButtonPressed = false;
114+
for(int i = 0; i < hc165_collection.size; i++) {
115+
if(hc165_data[i].values != 0xff) {
116+
isButtonPressed = true;
117+
break;
118+
}
119+
}
120+
121+
if(!isButtonPressed) {
122+
Serial.print("Axes: x:");
123+
Serial.print(get_potentiometer_resistance(analogRead(A0)));
124+
Serial.print(" y:");
125+
Serial.print(get_potentiometer_resistance(analogRead(A1)));
126+
Serial.print(" r:");
127+
Serial.print(get_potentiometer_resistance(analogRead(A8)));
128+
Serial.print(" t:");
129+
Serial.print(get_potentiometer_resistance(analogRead(A9)));
130+
Serial.print("\r\n");
131+
}
132+
71133
delay(POLL_DELAY_MSEC);
72-
}
134+
}

0 commit comments

Comments
 (0)