I assume that you want to read the input voltage that is modified by changing the setting of a potentiometer, a kind of variable resistor that has a knob that by twisting divides the incoming voltage into a different output voltage.
It looks like you are using the wrong class, AnalogOut
, and should instead be using the class AnalogIn
. See https://os.mbed.com/docs/mbed-os/v5.15/apis/analogin.html
Use the AnalogIn
API to read an external voltage applied to an analog
input pin. AnalogIn()
reads the voltage as a fraction of the system
voltage. The value is a floating point from 0.0(VSS) to 1.0(VCC). For
example, if you have a 3.3V system and the applied voltage is 1.65V,
then AnalogIn()
reads 0.5 as the value.
So your program should look something like the following. I don't have your facilities so can't test this but it is in accordance with the Mbed documentation. You will need to make sure you have things wired up to the proper analog pin and that you specify the correct analog pin to be reading from.
#include "mbed.h"
#include "TextLCD.h"
AnalogIn mypot(A0);
TextLCD lcd(D1, D2, D4, D5, D6, D7 ); // rs, rw, e, d4, d5, d6, d7
int main() {
while(1) { // begin infinite loop
// read the voltage level from the analog pin. the value is in
// the range of 0.0 to 1.0 and is interpreted as a percentage from 0% to 100%.
float voltage = mypot.read(); // Read input voltage, a float in the range [0.0, 1.0]
lcd.printf("normalized voltage read is %f\n", voltage);
wait(0.5f); // wait a half second then we poll the voltage again.
}
}
wait()
is deprecated but it should work. See https://os.mbed.com/docs/mbed-os/v5.15/apis/wait.html
See as well Working with Text Displays.