Go Down

### Topic: Calculated float values to display on Adafruit ST7565 LCD (Read 300 times)previous topic - next topic

#### annmariehulver

##### Aug 17, 2015, 08:24 pmLast Edit: Aug 17, 2015, 08:27 pm by annmariehulver
Hey, I am building an underwater sensor that has pH, temp, dissolved oxygen, and depth. All of these create analog values to be calculated into their respective values. I am trying to print all of the values on the ST7565 Adafruit screen. When calculated, the values are floats, but therefore cannot be displayed on the LCD because it does not display Floats. Does anyone have any suggestions on how to convert these values into something that can be displayed on the screen? Here is an example of the code i have for displaying the pH
(i know that the glcd.drawstring(50,0,pH) will not work because the pH is currently a float, but that is the section that I would like to workout!)

Code: [Select]
`#include "ST7565.h"#include "math.h"#include "stdio.h"#define exp#define a1 0.391988367#define a0 2.51792ST7565 glcd(9, 8, 7, 6, 5);#define LOGO16_GLCD_HEIGHT 16#define LOGO16_GLCD_WIDTH 16void setup () {  Serial.begin(9600);}void loop() {  float raw=log(10000.0*((1024.0/analogRead(3)-1)));  float kelvin=1/(0.001405+(0.0002368+(0.0000001013*raw*raw))*raw);  float Temp= kelvin-273.15;  Serial.println(Temp);  delay(1000);  float Count2=0;  float Voltage2;  float pH;  float ft= 1.0732-(0.0039093*Temp)+(0.000012333*pow(Temp, 2));  Count2=analogRead(2);  //Serial.println(Count2);  Voltage2=(Count2/1024)*5;  pH=(((ft*(Voltage2-a0))/a1)+7);  delay(1000);  glcd.drawstring(50,0,pH);  glcd.display();}`

#### olikraus

#1
##### Aug 17, 2015, 10:04 pmLast Edit: Aug 19, 2015, 12:18 pm by olikraus
You could use u8glib, which uses the arduino print function. So similar to serial.print you would use u8g.print()
Other option would be to convert the float into a long or int variable (if this is handled better by the Adafruit lib):

float x;
int y;
y = (int)x;

Oliver

#### odometer

#2
##### Aug 19, 2015, 03:57 amLast Edit: Aug 19, 2015, 03:58 am by odometer Reason: clarification (I hope)
It looks like the Adafruit library does not handle printing float or int types. It only does characters and character strings.

If this were my project, I would display numbers by splitting them up into their digits, and then displaying those digits as characters in a string. Like so:

Code: [Select]
`float num = 123.45; // the number we want to displaychar num_string[10]; // a place to put the string for the numberint num_whole = num; // get the "whole" part of the numberint num_cent = ((num + 0.005 - num_whole) * 100); // hundredths, with roundingif (num_cent > 99) {  // 100 hundredths make 1 whole  num_cent = 0;  num_whole++; }// next we put the digits in the stringnum_string[0] = '0' +  (num_whole / 100);num_string[1] = '0' + ((num_whole / 10) % 10);num_string[2] = '0' +  (num_whole % 10);num_string[3] = '.';num_string[4] = '0' +  (num_cent / 10);num_string[5] = '0' +  (num_cent % 10);num_string[6] = 0; // to terminate the string// finally, we display the stringglcd.drawstring(50,0,num_string);`

Go Up

Please enter a valid email to subscribe