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);
}
}
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);
}
}
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);
}
}
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.
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.
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;
}
}
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