简体   繁体   中英

LCD Programming with Arduino

I would like my LCD to display "Voltage=(sensorValue)" but right now the only way I can get the program to recognize the value as I turn the potentiometer is if I put it in a loop. But when I put it in a loop the whole screen gets filled with 1s, 2s, 3s, 4s, or 5s depending on where the potentiometer is set.

If I don't have it in a loop then whatever setting the potentiometer is on is what will pop on the screen and will not change if potentiometer is turned.

How can I put the results of a loop outside of a loop so I can have "(Voltage=sensoreValue)"?

Here's my program:

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);  

void setup()
{
    lcd.init();                      
    lcd.backlight();
    int sensorPin = A0;
    int sensorValue = 0;
    sensorValue = 0.004882812 * analogRead(sensorPin) + 1;
    lcd.print("Voltage=");
}

void loop()
{
    int sensorPin = A0;
    int sensorValue = 0;
    sensorValue = 0.004882812 * analogRead(sensorPin) + 1;
    lcd.print(sensorValue);
}

You asked it to print the reading and it is doing - it's printing each reading!

I suspect you either want it to only print if the value changes

int sensorValue = 0;
int prevValue = 0;

void loop()
{    
    sensorValue = 0.004882812 * analogRead(sensorPin) + 1;
    if (sensorValue != prevValue) {
       lcd.print(sensorValue);
       prevValue == sensorValue;
    }
}

Alternatively you could print 'n' backspaces so the new value is printed over the top of the old one, if your display lcd.print supports that

It sounds like print() is clearing the screen of previous data every time it is called (although the relevant documentation and library code available here and here is unclear).

If this is the case you need to print the Voltage= string in the loop along with the sensor value. Try changing your code to:

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);  
int sensorPin = A0;

void setup()
{
    lcd.init();                      
    lcd.backlight();
}

void loop()
{
    int sensorValue = 0.004882812 * analogRead(sensorPin) + 1;
    String display = "Voltage=";
    display += sensorValue;
    lcd.print(display);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM