Displaying Sensor Values on LCD's

  1. 9 months ago

    How does one program a LCD to show the Sensor value of let's say a potentiometer. (Sorry if misspelled)

    I usually put the sensor value into a string and then display that. The following example reads a sensor port and displays the raw value on the first line of the LCD and a converted value on the second line.

    The comments may help you understand what's going on. The comments are, of course, non-functional.

    // declare some string variables to hold the formatted information
        
        string outLine1 // this will hold what we want on the first line of LCD
        string outLine2; // this will hold what we want on second line of LCD
       
        int mySensorValue;
       
        // expanderPort is configured as an analog sensor in Motors and Sensor setup
        // we plugged an extension cable between the power expander status port and the analog port
        
        // read the value
        mySensorValue = SensorValue[expanderPort]; 
        // format the value into a string
        sprintf(outLine1,"sensor is: %d",mySensorValue);
    
        // turn the integer value from sensor port into a floating point value
        // representing volts of the battery plugged into the power expander.
        // Then, format the information for printing. The "%f" in the format
        // string will be replaced by the floating point number calculated by the
        // division.
        sprintf(outLine2," %f Volts",(float)mySensorValue/270.0);
    
        clearLCDLine(0);
        clearLCDLine(1);
        displayLCDString(0,0,outLine1);
        displayLCDString(1,0,outLine2);

    For your purposes, you could either define the analog port as a potentiometer or just set it to "analog". It won't change how you use the port.

  2. kypyro

    14 Feb 2018 V5 Beta Tester Answer Central Kentucky
    Edited 9 months ago by kypyro

    I usually put the sensor value into a string and then display that. The following example reads a sensor port and displays the raw value on the first line of the LCD and a converted value on the second line.

    The comments may help you understand what's going on. The comments are, of course, non-functional.

    // declare some string variables to hold the formatted information
        
        string outLine1 // this will hold what we want on the first line of LCD
        string outLine2; // this will hold what we want on second line of LCD
       
        int mySensorValue;
       
        // expanderPort is configured as an analog sensor in Motors and Sensor setup
        // we plugged an extension cable between the power expander status port and the analog port
        
        // read the value
        mySensorValue = SensorValue[expanderPort]; 
        // format the value into a string
        sprintf(outLine1,"sensor is: %d",mySensorValue);
    
        // turn the integer value from sensor port into a floating point value
        // representing volts of the battery plugged into the power expander.
        // Then, format the information for printing. The "%f" in the format
        // string will be replaced by the floating point number calculated by the
        // division.
        sprintf(outLine2," %f Volts",(float)mySensorValue/270.0);
    
        clearLCDLine(0);
        clearLCDLine(1);
        displayLCDString(0,0,outLine1);
        displayLCDString(1,0,outLine2);

    For your purposes, you could either define the analog port as a potentiometer or just set it to "analog". It won't change how you use the port.

 

or Sign Up to reply!