Decimal Places

Hi All,

I'm outputting the float value of "temperature" to a LCD & my phone from an ATMega, value is currently displayed as xx.xxx.

How can I change this so it only shows xx?

Many thanks,

Mick.

.print (, 2);

(deleted)

CtrlAltElite:
.print (, 2);

Thank you for your reply but that might as well be in dutch as far as I’m concerned.

mickjbriggs:
Thank you for your reply but that might as well be in dutch as far as I'm concerned.

Well, you didn't post any of your code, so it was the best I could do.

spycatcher2k:
With no rounding - Serial.print(int(temperature));
With rounding - Serial.print(int(temperature + 0.5));

Thank you for your reply but that doesn't work, it adds decimal places.

CtrlAltElite:
Well, you didn't post any of your code, so it was the best I could do.

Fair point,

I was emphasising my lack of knowledge, I didn't intend to be rude.

The relevant bit of code is:

{
int tempRaw = analogRead(tempPin);
float temp = (tempRaw/1024.0) *5.0;
float temperature = (temp - .5) * 100;
Blynk.virtualWrite(V0, temperature);

I'm sorry, I have no idea what a "Blynk" is, but the Arduino print methods allow you to specify how many decimal places to print - the default is two.

float temperature = (temp - .5) * 100;

Should be:

float temperature = (temp - 0.5) * 100;

Post the code that prints to the LCD.

outsider:

float temperature = (temp - .5) * 100;

Should be:

float temperature = (temp - 0.5) * 100;

Post the code that prints to the LCD.

What's the difference? (Apart from the obvious, redundant leading zero)

outsider:

float temperature = (temp - .5) * 100;

Should be:

float temperature = (temp - 0.5) * 100;

Post the code that prints to the LCD.

Thanks for your reply but that makes no difference.

LCD code is irrelevant as its only displaying the same value.

I think you should take a closer look at this Blynk thing - perhaps the documentation can help you.

CtrlAltElite:
I think you should take a closer look at this Blynk thing - perhaps the documentation can help you.

I don't think it has anything to do with the Blynk app, it is only displaying the same value that I have on my LCD & is "printed" to serial monitor.

mickjbriggs:
I don't think it has anything to do with the Blynk app, it is only displaying the same value that I have on my LCD & is "printed" to serial monitor.

The relevant bit of code is:

{
  int tempRaw = analogRead(tempPin);
  float temp = (tempRaw/1024.0) *5.0;
  float temperature = (temp - .5) * 100;
  Blynk.virtualWrite(V0, temperature);

Well, maybe we're missing what you think isn't the relevant bits of the code.

CtrlAltElite:

The relevant bit of code is:

{
 int tempRaw = analogRead(tempPin);
 float temp = (tempRaw/1024.0) *5.0;
 float temperature = (temp - .5) * 100;
 Blynk.virtualWrite(V0, temperature);



Well, maybe we're missing what you think isn't the relevant bits of the code.

Possible:

/* Comment this out to disable prints and save space */
//#define BLYNK_PRINT Serial

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
const int tempPin = A0;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth = “nothing to see here”;

// Your WiFi credentials.
// Set password to “” for open networks.
char ssid = “xxxxxxxxx”;
char pass = “yyyyyyyy”;

// Hardware Serial on Mega, Leonardo, Micro…
//#define EspSerial Serial1

// or Software Serial on Uno, Nano…
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(A4, A5); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 19200

ESP8266 wifi(&EspSerial);

BlynkTimer timer;

// This function sends Arduino’s up time every second to Virtual Pin (5).
// In the app, Widget’s reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
// You can send any value at any time.
// Please don’t send more that 10 values per second.
Blynk.virtualWrite(V1, millis() / 1000);
}
void temperature()
{
int tempRaw = analogRead(tempPin);
float temp = (tempRaw/1024.0) *5.0;
float temperature = (temp - 0.5) * 100;
Blynk.virtualWrite(V0, temperature);
}

void setup()
{
// Debug console
Serial.begin(9600);

// Set ESP8266 baud rate
EspSerial.begin(ESP8266_BAUD);
delay(10);

Blynk.begin(auth, wifi, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, wifi, ssid, pass, “blynk-cloud.com”, 8442);
//Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8442);

// Setup a function to be called every second
timer.setInterval(1000L, myTimerEvent);
timer.setInterval(1000L, temperature);
}

void loop()
{
Blynk.run();
timer.run(); // Initiates BlynkTimer
}

Blynk.virtualWrite(V0, temperature,0);

cherk:
Blynk.virtualWrite(V0, temperature,0);

Nope, doesn't work but thanks any way.

dtostrf() it to a c-string with the precision you want and then send it to Blynk.

gfvalvo:
dtostrf() it to a c-string with the precision you want and then send it to Blynk.

Or, set the number of decimals to display on the Virtual Pin in the Blynk app.

With no rounding - Serial.print(int(temperature));
With rounding - Serial.print(int(temperature + 0.5));

Thank you for your reply but that doesn't work, it adds decimal places.

Really? That SHOULD work; it should convert the float value to an integer, which should then be displayed without a decimal point. Unless int() is weird on you board (try Serial.print((long)(temperature+0.5)):wink: Or unless the Blynk device only displays floats...