Go Down

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


Aug 17, 2015, 08:24 pm Last 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.51792

ST7565 glcd(9, 8, 7, 6, 5);
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
void setup () {

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;
  float Count2=0;
  float Voltage2;
  float pH;
  float ft= 1.0732-(0.0039093*Temp)+(0.000012333*pow(Temp, 2));


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;



Aug 19, 2015, 03:57 am Last 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 display
char num_string[10]; // a place to put the string for the number

int num_whole = num; // get the "whole" part of the number
int num_cent = ((num + 0.005 - num_whole) * 100); // hundredths, with rounding
if (num_cent > 99) {
  // 100 hundredths make 1 whole
  num_cent = 0;
// next we put the digits in the string
num_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 string

Go Up