-1

I am doing a project using the Mbed OS where I have to use a LCD 1602 to display the value of the potentiometer.

I was able to connect and display "Hello World" on the LCD in my previous project but I don't know how create one where it'll read the potentiometer. I've read through the Arm Mbed web site and still don't know how to create the code.

I'm using a Nucleo board.

The source code for my project is as follows.

#include "mbed.h"
#include "TextLCD.h"

AnalogOut mypot(A0);

TextLCD lcd(D1, D2, D4, D5, D6, D7 ); // rs, rw, e, d4, d5, d6, d7


int main() {
      while(1){
        for(i=0.0f, i<1.0f , i+=1.0f){
        mypot = i
        lcd.printf("mypot.read()");

        }
    }
}
Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
  • You should focus/limit your problem further down. Is it about knowing how to get the position of the potentionmeter inside the code? Is it how to generate a good string representation of it (`printf()` usage)? Is it about putting that string to the LCD? Please add that information the headline, and more clearly – HelpingHand May 21 '20 at 13:44
  • 1
    An off-topic hint concerning your sample code: Do not (I mean: never!) use a float variable as a loop counter variable. Counters are integer by nature, floats are not. – HelpingHand May 21 '20 at 13:45
  • 1
    Please add the code for classes `AnalogOut` and `TextLCD` (especially, `AnalogOut::read()` and `TextLCD::printf()`, resp.) - unless focussing the question renders one of these two unnecessary. – HelpingHand May 21 '20 at 13:48
  • 1
    If the `TextLCD::printf()` API works only a bit similar to usual `printf()` functions/methods, the line `lcd.printf("mypot.read()");` is likely wrong because this would require that particular `printf()` implementation to parse a string and interpret it in terms of a class object like `mypot` - a virtually unfeasible programming task. => Please check that line first. – HelpingHand May 21 '20 at 13:51
  • what are you seeing on the lcd if anything? – old_timer May 21 '20 at 16:04
  • Yes I'm need to display the position of the potienitmotor onto the LCD. When I displayed Hello World I just used lcd.print(...), I didn't use it before but I'll try as you mentioned. When I compile it nothing is displayed it. – Aron Rodgers May 21 '20 at 16:54
  • I provided an answer and I was wondering whether you had made the modifications suggested and how your project has turned out. What are you with making this work? – Richard Chambers Jun 29 '20 at 23:37

1 Answers1

0

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.

Richard Chambers
  • 16,643
  • 4
  • 81
  • 106