[SOLVED]float to string on nokia 5110

Hello,

Im making my own small portable UV meter and I'm using the nokia 5110 as a display. I have everything working, the uv meter and the display but the library I'm using can't use floats for the display, its the one from rinkydink.
The format for the display is:

myGLCD.print(string, int1, int2);

But i have:

myGLCD.print(float, int1, int2);

How can I solve this problem?

Sincere,
T.

I think people would want to see the rest of your code ..

Meanwhile - can’t you avoid using float ?

  • other libraries for the display?

  • google for some examples using this display ?

Hello,

My code is based of a code I found on sparkfun, it was the only proper code i could find that works for my uv meter the ml8511 the rest are pretty useless and dont work for what i want to do. Which sadly works with a float function.
https://learn.sparkfun.com/tutorials/ml8511-uv-sensor-hookup-guide

And the nokia lcd code from rinkydink.
My code is:

/* 
 ML8511 UV Sensor Read Example
 By: Nathan Seidle
 SparkFun Electronics
 Date: January 15th, 2014
 License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).

 The ML8511 UV Sensor outputs an analog signal in relation to the amount of UV light it detects.

 Connect the following ML8511 breakout board to Arduino:
 3.3V = 3.3V
 OUT = A0
 GND = GND
 EN = 3.3V
 3.3V = A1
 These last two connections are a little different. Connect the EN pin on the breakout to 3.3V on the breakout.
 This will enable the output. Also connect the 3.3V pin of the breakout to Arduino pin 1.

 This example uses a neat trick. Analog to digital conversions rely completely on VCC. We assume
 this is 5V but if the board is powered from USB this may be as high as 5.25V or as low as 4.75V:
 http://en.wikipedia.org/wiki/USB#Power Because of this unknown window it makes the ADC fairly inaccurate
 in most cases. To fix this, we use the very accurate onboard 3.3V reference (accurate within 1%). So by doing an
 ADC on the 3.3V pin (A1) and then comparing this against the reading from the sensor we can extrapolate
 a true-to-life reading no matter what VIN is (as long as it's above 3.4V).

 Test your sensor by shining daylight or a UV LED: https://www.sparkfun.com/products/8662

 This sensor detects 280-390nm light most effectively. This is categorized as part of the UVB (burning rays)
 spectrum and most of the UVA (tanning rays) spectrum.

 There's lots of good UV radiation reading out there:
 http://www.ccohs.ca/oshanswers/phys_agents/ultravioletradiation.html
 https://www.iuva.org/uv-faqs

*/


// It is assumed that the LCD module is connected to
// the following pins:
//      CLK  - Pin 11
//      DIN - Pin 10
//      DC   - Pin 9
//      CE   - Pin 8
//      RST  - Pin 7
//
#include "LCD5110_Graph.h"

LCD5110 myGLCD(11,10,9,7,8);

extern unsigned char SmallFont[];

//Hardware pin definitions
int UVOUT = A0; //Output from the sensor
int REF_3V3 = A1; //3.3V power on the Arduino board

void setup()
{
  Serial.begin(9600);

  pinMode(UVOUT, INPUT);
  pinMode(REF_3V3, INPUT);
  Serial.println("ML8511 example");
  myGLCD.InitLCD();
  myGLCD.setFont(SmallFont);

}



void loop()a
{
  int uvLevel = averageAnalogRead(UVOUT);
  int refLevel = averageAnalogRead(REF_3V3);

  //Use the 3.3V power pin as a reference to get a very accurate output value from sensor
  float outputVoltage = 3.3 / refLevel * uvLevel;

  float uvIntensity = mapfloat(outputVoltage, 0.92, 2.8, 0.0, 15.0); //Convert the voltage to a UV intensity level
  float uvIndex = ((outputVoltage-0.92)/0.08); // uvIntensity converted to UV Index value
  
  //Serial.print("output: ");
  //Serial.print(refLevel);

  //Serial.print("ML8511 output: ");
  //Serial.print(uvLevel);

  Serial.print(" / ML8511 voltage: ");
  Serial.print(outputVoltage);

  Serial.print(" UV Intensity : ");
  Serial.print(uvIntensity);

  Serial.print(" UV index : ");
  Serial.print(uvIndex);
   
  Serial.println();

  delay(1000);
  {
    myGLCD.print("UVIndex: ", LEFT, 20);
//    myGLCD.print(uvIndex, CENTER, 20);
    myGLCD.update();
    delay(50);
  }
}


//Takes an average of readings on a given pin
//Returns the average
int averageAnalogRead(int pinToRead)
{
  byte numberOfReadings = 8;
  unsigned int runningValue = 0; 

  for(int x = 0 ; x < numberOfReadings ; x++)
    runningValue += analogRead(pinToRead);
  runningValue /= numberOfReadings;

  return(runningValue);  
}

//The Arduino Map function but for floats
//From: http://forum.arduino.cc/index.php?topic=3922.0
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

I think you are missing something basic. You should be able to just get a float variable and myglcd.print it, without all that complicated stuff, and there must surely be axamples with the library that demonstrate that.. I use the Henning Karlsen library on other LCDs and it prints floats just fine. The PCD 8544 library is fine for printing floats on the 5110, and probably more efficiently too.

yeah i thought the same thing but couldnt find it. Im going to dive in the examples again, thanks.

Edit:
SOLVED!
I added this and it prints it to the nokia lcd!:

char uvIndexlcdresult[10]; // Buffer big enough for 9-character float
    dtostrf(uvIndex, 8, 3, uvIndexlcdresult); // Leave room for too large numbers!

  Serial.print(uvIndexlcdresult);
    myGLCD.print(uvIndexlcdresult, RIGHT, 20);

It only somehow started at 1.0 and not 0.0, but i just added a -1 to the formula so it starts at 0.0.
Thanks for the feedback, I still however couldn't find a way to directly print the float on the screen.

Well, you've got a result, but it seems all too complicated. Does your library not have the printNumF command?

Yes, but it needs 7 values of which most I didnt understand, I'm quite the noob when it comes to programming/coding.
Here's the finished thing:

Of which the last three are optional, and should be described as that. All you needed was something like
myGLCD.printNumF(InTemp, 2, 174, 165);Still, you have a result - and the enclosure looks quite neat.
The PCD8544 library would be fine for what you are doing.

From UTFT.h (which is a similar library)

 void printNumF(double num, byte dec, int x, int y, char divider='.', int length=0, char filler=' ');

Yes, there are 7 arguments but 3 of them are seldom used. You would normally just use

     myGLCD.printNumF(1.23456, 1, x, y);   //1.2 with 1 decimal places
     myGLCD.printNumF(1.23456, 2, x, y);   //1.23 with 2 decimal places
     myGLCD.printNumF(1.23456, 3, x, y);   //1.235 with 3 decimal places
     myGLCD.printNumF(1.23456, 4, x, y);   //1.2346 with 4 decimal places

In practice you might want to RIGHT justify the number with the x argument.

You only use divider if you want a comma e.g. 1,23
You only use length if you want to format neatly. (similar to x=RIGHT)
You only use filler if you want to pad with leading zero e.g. 0001.23

Personally, I don't like Henning Karlsen's choice of methods. But printNumF() seems a lot easier to use than your sequence.

Incidentally, Mr Pyner's example should use 3 decimal places and sensible x, y arguments

David.

No it shouldn't. I said "something like", and it is just lifted from the programme I use.