2

I'd like the double number 200.00 shown as 200.00 on my LCD screen.

For instance, when I pass just a number (int) like 40 to the LCD, it uses it as an ASCII code to show a character on the display.

Now I want to send a double to it and it should be displayed as a double.

#include <Wire.h>

void setup() {
   Wire.begin(0x63);
}


double f;
void loop() {

  f = 200.00;

  Wire.beginTransmission(0x63);
  Wire.write(byte(0x00));
  Wire.write(4); //HIDE CURSOR
  Wire.write(12); //CLEAR SCREEN
  Wire.write(13); //CURSOR 2nd LINE
  Wire.write("Frequenz: ");
  Wire.write(f); //<- How to do this?
  Wire.write("Hz");
  Wire.endTransmission();
}
2

The Arduino Wire class does only support write() of byte, string and vector of bytes. To write a double it must first be converted to a string. This can be done with the standard C library function dtostrf().

double f;

void loop() {
  char buf[10];

  f = 200.00;

  Wire.beginTransmission(0x63);
  Wire.write(byte(0x00));
  Wire.write(4); //HIDE CURSOR
  Wire.write(12); //CLEAR SCREEN
  Wire.write(13); //CURSOR 2nd LINE
  Wire.write("Frequenz: ");
  Wire.write(dtostrf(f, 0, 2, buf));
  Wire.write("Hz");
  Wire.endTransmission();
}

The class inherits from Print (via Stream) so that the overloaded member function print() for double can be used. In that case:

double f;

void loop() {
  f = 200.00;

  Wire.beginTransmission(0x63);
  Wire.write(byte(0x00));
  Wire.write(4); //HIDE CURSOR
  Wire.write(12); //CLEAR SCREEN
  Wire.write(13); //CURSOR 2nd LINE
  Wire.write("Frequenz: ");
  Wire.print(f);
  Wire.write("Hz");
  Wire.endTransmission();
}

Please note that the maximum number of bytes to write (between beginTransmission and endTransmission) is limited to 32 in the standard configuration of the Arduino core.

Cheers!

|improve this answer|||||
2

Use Wire.print(), instead of Wire.write() which is intended only to output single bytes or byte arrays.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.