Serial Data to Graphic LCD

Okay, My current project involves a Nokia 5110 and a brand new dilemma 2.0 :). I have requested help from the forum’s users to help me print Serial Data from a barometer to a Nokia Graphic LCD. I have been favored to get the same barometer to display its data on both the Serial monitor and a 16x2 LCD. Some threads suggest using the sprintf function to convert Serial data into numbers, however, multiple tries show that I need some tutoring on this problem. I have had many ideas, but the one that seemed closest to a solution(and didn’t error out) I clearly marked(///) in the code down below.

#include <SFE_BMP180.h>
#include <Wire.h>
#define RST 12
#define CE 11
#define DC 10
#define DIN 9
#define CLK 8

static const byte ASCII[][5] =
{

/* This is where the ASCII  Font/Alphabet was,
 I had to take it out cause I exceeded the maximum amount of text.  */
 };

void LcdWriteString(char *characters)
{
  while (*characters) LcdWriteCharacter(*characters++);
}
void LcdWriteCharacter(char character)
{
  for (int i = 0; i < 5; i++) LcdWriteData(ASCII[character - 0x20][i]);
  LcdWriteData(0x00);
}
void LcdWriteData(byte dat)
{
  digitalWrite(DC, HIGH);
  digitalWrite(CE, LOW);
  shiftOut(DIN, CLK, MSBFIRST, dat);
  digitalWrite(CE, HIGH);

}
void LcdWriteCmd(byte cmd)
{
  digitalWrite(DC, LOW);
  digitalWrite(CE, LOW);
  shiftOut(DIN, CLK, MSBFIRST, cmd);
  digitalWrite(CE, HIGH);

}


SFE_BMP180 pressure;

double baseline; // baseline pressure

void setup()
{
  Serial.begin(9600);
  Serial.println("REBOOT");

  // Initialize the sensor (it is important to get calibration values stored on the device).

  if (pressure.begin())
    Serial.println("BMP180 init success");
  else
  {
    // Oops, something went wrong, this is usually a connection problem,
    // see the comments at the top of this sketch for the proper connections.

    Serial.println("BMP180 init fail (disconnected?)\n\n");
    while (1); // Pause forever.
  }

  // Get the baseline pressure:

  baseline = getPressure();

  Serial.print("baseline pressure: ");
  Serial.print(baseline);
  Serial.println(" mb");

  pinMode(RST, OUTPUT);
  pinMode(CE, OUTPUT);
  pinMode(DC, OUTPUT);
  pinMode(DIN, OUTPUT);
  pinMode(CLK, OUTPUT);
  digitalWrite(RST, LOW);
  digitalWrite(RST, HIGH);

  LcdWriteCmd(0x21);
  LcdWriteCmd(0xB8);
  LcdWriteCmd(0x04);
  LcdWriteCmd(0x14);
  LcdWriteCmd(0x20);
  LcdWriteCmd(0x0C);
}

void loop()
{
  double a, P;

  // Get a new pressure reading:

  P = getPressure();

  // Show the relative altitude difference between
  // the new reading and the baseline reading:

  a = pressure.altitude(P, baseline);

  Serial.print("relative altitude: ");
  if (a >= 0.0) Serial.print(" "); // add a space for positive numbers
  Serial.print(a, 1);
  Serial.print(" meters, ");
  if (a >= 0.0) Serial.print(" "); // add a space for positive numbers
 Serial.print(a * 3.28084, 0);
  Serial.println(" feet");

  delay(500);
if (Serial.available()) {
     delay(100);                                   /////// /////////////////////////
     while (Serial.available() > 0) {    /////// //////////////////////
       // display each character to the LCD    ////////////////////
       LcdWriteData(Serial.read());      ///// /////////////////////
       }}}                                              ///////////////////////////
double getPressure()
{
  char status;
  double T, P, p0, a;

  // You must first get a temperature measurement to perform a pressure reading.

  // Start a temperature measurement:
  // If request is successful, the number of ms to wait is returned.
  // If request is unsuccessful, 0 is returned.

  status = pressure.startTemperature();
  if (status != 0)
  {
    // Wait for the measurement to complete:

    delay(status);

    // Retrieve the completed temperature measurement:
    // Note that the measurement is stored in the variable T.
    // Use '&T' to provide the address of T to the function.
    // Function returns 1 if successful, 0 if failure.

    status = pressure.getTemperature(T);
    if (status != 0)
    {
      // Start a pressure measurement:
      // The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait).
      // If request is successful, the number of ms to wait is returned.
      // If request is unsuccessful, 0 is returned.

      status = pressure.startPressure(3);
      if (status != 0)
      {
        // Wait for the measurement to complete:
        delay(status);

        // Retrieve the completed pressure measurement:
        // Note that the measurement is stored in the variable P.
        // Use '&P' to provide the address of P.
        // Note also that the function requires the previous temperature measurement (T).
        // (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.)
        // Function returns 1 if successful, 0 if failure.

        status = pressure.getPressure(P, T);
        if (status != 0)
        {
          return (P);
        }
        else Serial.println("error retrieving pressure measurement\n");
      }
      else Serial.println("error starting pressure measurement\n");
    }
    else Serial.println("error retrieving temperature measurement\n");
  }
  else Serial.println("error starting temperature measurement\n");
}

Any ideas? I’m confident that it is possible to display Serial Data on the Graphic LCD, I just don’t know how :slight_smile:
When I did upload this program I only received random bits of pixels.
As always help is appreciated. :slight_smile:

Some threads suggest using the sprintf function to convert Serial data into numbers

I suspect that you misunderstood something. sprintf() converts numbers to strings.

however, multiple tries show that I need some tutoring on this problem

I suspect that this IS true.

if (Serial.available()) {
     delay(100);                                   /////// /////////////////////////

We KNOW that there is something to do, so let's diddle around for a while doing nothing. You must be a government employee.

       }}}                                              ///////////////////////////

You're fired.

Your LcdWriteData() function takes a byte. You are sending it an int. Doesn't seem like a good idea to me.

The int you are sending it contains a character. The function expects a set of pixel values to show one of the 5 lines that makes up the display of the character.

You probably should be using to LcdWriteCharacter() the char.

Some threads suggest using the sprintf function to convert Serial data into numbers

sp. "sscanf" ?

You probably should be using to LcdWriteCharacter() the char

I must not be understanding what your exactly saying. Pardon me. Should I char a Serial.read so I can save the data of then write it. Either way, how should this task be preformed I haven't wrote chars like this before, besides the one in the code which I got from an example. The only program that compiled correctly doesn't have a char in it.

if (Serial.available()) {
     delay(100);                                   // short hysteresis 
     while (Serial.available() > 0) {  
       // display each character to the LCD    
       LcdWriteCharacter(Serial.read());      // how to I arrange this with the char
       }}}

I was wondering is it possible to make adjustable float points, like a double. to expound this situation

Should I char a Serial.read so I can save the data of then write it.

You should learn to speak like a programmer.

"Should I save the output from Serial.read() in a char variable, so I can write it to the LCD?" That shouldn't be necessary, but it certainly wouldn't hurt, and you could Serial.print() it to assure yourself that what you got was what you expected.

The only program that compiled correctly doesn't have a char in it.

What errors are you seeing?

I was wondering is it possible to make adjustable float points, like a double.

See comment #1.

It is possible to convert a float or double to a string, using dtostrf(), and then print that string to the LCD.

What errors are you seeing?

"expected primary expression before char"

if (Serial.available()) {
     delay(100);
     while (Serial.available() > 0) {
       // display each character to the LCD
       LcdWriteCharacter(char*)(Serial.read());
       }}}

I firmly grasp the fact that I don't know 'Char' very well. Theirs obviously a situation In this string of code on which I lack of answers. Suggestions?

It is possible to convert a float or double to a string, using dtostrf(), and then print that string to the LCD.

Shall I use this?

       LcdWriteCharacter(char*)(Serial.read());

Your cast is in the wrong place, and is the wrong type. The function expects a char, not a char *. The cast is NOT needed. There is an implicit cast from int to char. The compiler will warn about the possible loss of data, since the cast is a downcast to a smaller type. But, the Arduino team doesn't want you to be worried about that, so they suppress all warnings.

Shall I use this?

If you have a reason to.

So, I eliminated the cast and I still seize the same code. The cast is the * right? The error thing you mentioned Is that what could be causing my problem. error: expected primary-expression before 'char' What shall I need to complete this constructor? Thanks for giving me ideas to troubleshoot and fix my problem.

The cast is the * right?

No. The "(char *)" part is the cast. All you need is:

       LcdWriteCharacter(Serial.read());

I used Pauls suggestion and now I have

if (Serial.available()) {
     delay(100);
     while (Serial.available() > 0) {
       // display each character to the LCD
       LcdWriteCharacter(Serial.read());
       }}}

However all I get on the display is random pixels. . (Sorry about the size of the pic, I couldn't figure out how to re-size) Any way Im still at a standstill in this project

However all I get on the display is random pixels.

What if you try something like:

if (Serial.available())
{
     while (Serial.available() > 0)
     {
       // display each character to the LCD
       char crap = Serial.read();
       LcdWriteCharacter(crap);
     }
}