LCD Help.

Hi all!

I'm really getting stuck here! I'm new to the whole Arduino/C++ thing.
What I am trying to do is print serial data from my computer to a LCD display.
Easy enough, I got that to work using the tutorials on this site. Where I am getting stuck is I what it to look like this:

Received: 48

Where just the number 48 changes.

Please help! I have been working on this for way to long!

Here is the code that I've been working with:


int incomingByte = 0; // for incoming serial data

void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {

// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();

// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte);
}
}


Thanks in advance!

What kind of LCD display is it ?

Is the LCD display serially controlled ?

Looks a little to me like your trying to use serial communication from Arduino to both the PC and the display, am i wrong ?

Hey MikMo!

Thanks for replying!

I have this LCD:

Yes it is serially controlled.

Looks a little to me like your trying to use serial communication from Arduino to both the PC and the display, am i wrong ?

No, I'm not trying to. But I might be.
That is just one of many scripts that I've tried.

Thanks!

Okay, I'm really close!

int incomingByte = 0;      // for incoming serial data

void setup() {
      Serial.begin(9600);      // opens serial port, sets data rate to 9600 bps
}

void loop() {

      // send data only when you receive data:
      if (Serial.available() > 0) {
            // read the incoming byte:
            incomingByte = Serial.read();

            // say what you got:
            Serial.print("I received: ");
            Serial.println(incomingByte, BYTE);
      }
}

If I type 1 into the serial monitor it prints:

I received: 1

But if I type 12 in it prints:

I received: 1
I received: 2

And if I type 123 it says

I received: 1
I received: 2
I received: 3

How do I fix this????

Help!!

Thanks!

Just pull the ATmega168 chip out of the Arduino and it will work fine. :slight_smile:
You'd be 'borrowing' the serial circuitry.

If you require the Arduino to process the information then use SoftwareSerial for the LCD.

And if I type 123 it says

I received: 1
I received: 2
I received: 3

How do I fix this????

Well, what do you WANT it to do? You'll have to do some sort of parsing of the data you read from the serial port, and that can get sort of arbitrarily complex. For a first try you can do something like:

if (Serial.available() > 0) {
// read the incoming byte:
  incomingByte = Serial.read();
  if (incomingByte < '0' || incomingByte > '9') {
   Serial.println(' ', BYTE);  //newline         
   Serial.print("I received: ");  // new "header"
  } else {
   Serial.print(incomingByte, BYTE);
  }
}

I wanted it to say:

I Received: 123. Sorry, I should of said that.

However I changed some stuff, and now all I want it to say is:

123

And then if it type 456 it will clear the screen and say:

Not:

123456

Like it does now. Here is my code:

byte incomingByte = 0;      // for incoming serial data

void setup() {
      Serial.begin(9600);      // opens serial port, sets data rate to 9600 bps
        clearLCD(); //Command 1
             }

void loop() {
  
    //clearLCD(); //Command 2
  

      // send data only when you receive data:
      if (Serial.available()) { // read the incoming byte:

                //clearLCD(); // Command 3
            incomingByte = Serial.read(); // say what you got:
                Serial.print(incomingByte);
               


    }
}


void clearLCD(){
   Serial.print(0xFE, BYTE);   //command flag
   Serial.print(0x01, BYTE);   //clear command.
}

If I uncomment the second clearLCD (Command 2) as soon as I try to use a serial connection it starts printing all of these odd characters. And If I uncomment the third clearLCD (Command 3) it will only print the last character.

Just pull the ATmega168 chip out of the Arduino and it will work fine. Smiley
You'd be 'borrowing' the serial circuitry.

Really? Are you serious? I don't know that much about electronics, but it doesn't seem good to be pulling off chips. :wink:

Thank you for all your help!

You are almost there - cool.

What you have to do is when you check with serial.available that there is data then you should use a loop to read all the data one byte at a time until serial.available returns false. Put these data together to make a string, send that string to the LCD, and then use a small delay in your main loop to allow all the data the PC is sending in "the next batch" to be recieved before checking with seriel.available again.

The problem is that serial.available will return true as soon as there is one byte available, so if your main loop is very fast you run the risk of checking for available data before all the data is recieved, reading only one byte (digit), sending that to the LCD, and start over.

You should not let the PC send to frequently, give the Arduino board time to read and process all data before sending again.

Normally it would be smart to have some kind if two way communication, leting the Arduino board signal back to the PC, "got that, i'm ready for more". In your case that would be difficult since you don't have anything on the PC to listen for that.

HELP!!!!!

I can't figure this out.

Here is what I want it to do:

if Serial.available is > 1, then save data to a val called test until Serial.available = < 1 .
if Serial.available is < 1, then do nothing.

How do I this???????

I just don't know how to save data to a val.

Sorry for all the questions, but this is my first time in C/C++.

Thank you for all your help!

This should get you pretty close to what you want.

int val[10];               // variable used to store data from serial port
int ByteCount = 0;

void setup() {
  Serial.begin(9600);         // connect to the serial port
  Serial.println("Arduio Online");
}

void loop() {
  if (Serial.available() > 0) {
    delay(100);
    while (Serial.available() > 0) {
      ByteCount ++;
      val[ByteCount] = Serial.read();
    }
  }
  
  if (ByteCount > 0) {
    for (int i = 1; i <= ByteCount; i++) {
      if (val[i] >= '0' && val[i] <= '9' ) {
        val[i]= val[i] - '0';
        Serial.print(val[i]);
      }
    }
    Serial.println();
    ByteCount = 0;
  }
}

Thanks for the code!!

I've been away for the weekend, therefore I have not been able to try it. :-/
I'll be able to try it a little later on today, I will let you know how it turns out.

BTW, westfw, don't you think that I should have your icon? Lol

Thank again for the code!!

BTW, westfw, don't you think that I should have your icon? Lol

Certainly. It's one of the standard avatars in the forum library...