I am sending 24 characters from C# through Serial to the arduino 16x02 LCD. So when i tried the code, i can only see 16 characters out of the 24. The 24 character will be send when i press a button. Is there anyone who know how should i solve the problem and allow me to see all 24 characters?
You want to split the received data into a chunk of 16 characters and a chunk of 8 characters. Write the first chunk to the first line and the second chunck to the second line of the LCD. Note that you need to allocate 17 and 9 characters respectively in the character arrays to be able to add the terminating '\0'.
Post your Arduino code for further help.
//Edit:
instead of splitting, just read 16 characters into the 17 character buffer and next read the remaining 8 characters in the same buffer; saves some memory.
Hi everyone,
Thanks for the quick reply. my code is as below:
Is there any things that i need to change to my arduino code? As i am not allow to change the C# code, the coding of C# is kind of fix. Thanks
// include the library code: #include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// initialize the serial communications:
Serial.begin(115200); //servo.attach(9);
pinMode(10,OUTPUT);
analogWrite(10,120);
}
void loop()
{
// when characters arrive over the serial port...
if (Serial.available()) {
// wait a bit for the entire message to arrive
delay(100);
// clear the screen
lcd.clear();
// read all the available characters
while (Serial.available() > 0) {
int a = Serial.read();// display each character to the LCD
lcd.print(a,DEC);
}
}
}
Ok will do, sorry for the inconvenience caused.
So is there any changes to my codes to let it received 16 and 8 characters separately? i read the LCD documentation and examples already but i still cant find any information regarding the separation of characters.
Thanks
So is there any changes to my codes to let it received 16 and 8 characters separately? i read the LCD documentation and examples already but i still cant find any information regarding the separation of characters.
You are going to use lcd.setCursor() to set the position to the beginning of the second row after 16 characters have been displayed on the top row. The internal memory of the lcd controller does not automatically bring the display to the second line until after 40 characters.
You are going to have to change the way in which you are reading the characters from serial so that you count them. If you follow the tutorial that Robin2 linked, you will have an array index that you can use in cursor management.
EDIT: If you follow Robin's example, you are best off adding a "break" character after 16 to help in parsing the second row. Something like this
So is there any changes to my codes to let it received 16 and 8 characters separately?
Lots of ways to do things. If you will always have 24 characters and want to seperate the first 16 from the remaining 8, then simple character count parsing code like below might be a solution.
//send string using serial monitor
String readString, data1, data2, data3;
void setup() {
Serial.begin(9600);
Serial.println("parse-test-3"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
delay(2);
if (Serial.available() >0) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
}
}
if (readString.length() >0) {
Serial.println(readString); //see what was received
// expect a string like 123456789 with three data partts
data1 = readString.substring(0, 3); //get the first three characters
data2 = readString.substring(3, 6); //get the next three characters
data3 = readString.substring(6, 9); //get the next three characters
Serial.println(data1); //print to serial monitor to see results
Serial.println(data2);
Serial.println(data3);
readString="";
data1="";
data2="";
data3="";
}
}
Hi guys, i modified zoomkat code however i am still not receiving what i want to.
Instead of using Serial monitor in zoomkat code, i will be using LCD to show the values as the serial monitor cant be open. The information that will be transferred to arduino is in Hex form.
So could anyone help me with the codes?
Thanks
/ include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
String readString, data1, data2, data3;
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// initialize the serial communications:
Serial.begin(115200);
//servo.attach(9);
pinMode(10,OUTPUT);
analogWrite(10,120);
}
void loop()
{
// when characters arrive over the serial port...
if (Serial.available()) {
// wait a bit for the entire message to arrive
delay(100);
// clear the screen
lcd.clear();
// read all the available characters
while (Serial.available() > 0) {
char a = Serial.read();// display each character to the LCD
//lcd.print(a,DEC);
readString += a;
}
}
if (readString.length ()>0)
{
data1 = readString.substring(0,16);
data2 = readString.substring(16,24);
int data3 = data1.toInt();
int data4 = data2.toInt();
lcd.clear();
lcd.print(data3);
lcd.setCursor(0,1);
lcd.print(data4);
readString = "";
data1 ="";
data2="";
}
}
The information that will be transferred to arduino is in Hex form.
Can you please expand on what that means exactly ? HEX is just another way of displaying binary data as is decimal. However, if you are receiving characters representing HEX data that is a different kettle of fish.
The values that are sent in arduino are in 24 characters (numbers) , i would like to see 16 characters on the top row of my LCD and 8 characters on the bottom row. However after trying the codes, i still cant seem to find the problem. Well, how do i know what type of form is being sent to my arduino? Cause in C# i need to move the slider and it will transfer the character already
0123456789ABCDEF is a 64 bit integer
"0123456789ABCDEF" is a 16 character text representation of the above 64 bit integer
Which one do you send? In your opening post, you spoke about characters so most of us expect that you're talking about the latter.
The latest code that you posted converts this text representation of the u][64[/u] bit integer to an 16 bit integer using the below line
int data3 = data1.toInt();
Side note
if (readString.length ()>0)
Is a possible flaw in your code; you expect all 24 characters to be received in one go which is never guaranteed; the subString() might crash if there are insufficient characters. You should check for 24 instead.
PS:
Can you attach the relevant part of your C# code? If you are sending the slider value directly (without slidervalue.toString()), I guess you send an integer / long in which case String in Arduino is the wrong choice to store the data.