I was wondering if anyone could suggest how to display a number in scientific notation on an LCD display.
My example code is below. My range of numbers to be displayed is from 1e-10 to 1e10, but as it is, my code cannot display that range.
Thank you for any suggestions !
#include <Wire.h> #include <Adafruit_RGBLCDShield.h> #include <utility/Adafruit_MCP23017.h> #define GREEN 0x2
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
float sensorPin = A0;
int ledPin = 13;
float sensorValue = 0.00; // variable to store the value coming from the sensor
float Voltage;
float P;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
lcd.begin(16, 2);
lcd.print("P = ");
lcd.setBacklight(GREEN);
lcd.setCursor(0, 1);
Voltage=sensorValue5.00/1023.009.49;//Voltage calibration
P=pow(10.00,(Voltage*1.667-11.33));
lcd.print(P);
delay(10000);
}
The print() methods in the Print class (that the lcd class appears to derive from, though it is hard to tell, since you did not post a link to the library) do not do scientific notation.
The dtostrf() function converts a float/double to a string. The dtostre() function does the same, but in scientific notation format.
I tried suggestion from PaulS, using the dtostre(), but it turns the entire display into illegible characters (unlike dtostrf, which prints the same as just printing the float P).
Basically, I declared char Ps[40] and then printed Ps derived from: dtostre(P,7,2,Ps), but as I said, it doesn't work...
Sorry, here is the revised code;
<#include <Wire.h> #include <Adafruit_RGBLCDShield.h> #include <utility/Adafruit_MCP23017.h> #define GREEN 0x2
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
float sensorPin = A0;
int ledPin = 13;
float sensorValue = 0.00; // variable to store the value coming from the sensor
float Voltage;
static float P;
static char Ps[20];
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
lcd.begin(16, 2);
lcd.print("P = ");
lcd.setBacklight(GREEN);
lcd.setCursor(0, 1);
Voltage=sensorValue5.00/1023.009.49;//Voltage calibration
Voltage = 6.00;
P=pow(10.00,(Voltage*1.667-11.33));
dtostre(P,7,2,Ps);
lcd.print(Ps);
delay(10000);
}/>
I think it should be more like:
dtostre(P,Ps,DTOSTR_ALWAYS_SIGN | DTOSTR_UPPERCASE);
or leave out the "| DTOSR_UPPERCASE" if you want "e" instead of "E".