Hi Guys,
Need your help This is part of my project where I need to read serial communication from a device/pc and display it in LED array 5 digit 7 segment. However, as of now I would like to read the value given by a PC and display it to an LCD for testing purposes.
I am using COM PORT 3 of my PC then usb to mini usb cable to arduino. Then I connected my arduino to a WG12864 (LCD 128x64 device). My connections are all good it is just I have a problem with my program. For example I wanted to display "1" from PC to LCD it will display "1" (using serial monitor in Arduino IDE). When I type "2" it will display "12". Basically, it does delete the previous data. I wanted to delete the previous data after I pressed enter. See pictures below. This is using "No Line ending. When I use "New Line", it does not display anything.
The code is below, I just copied this codes from other examples and I experimented it. I think the problem is when I received \r or \n it does not refresh the display or data. I hope you could help me with this.
#include "U8glib.h"
U8GLIB_ST7920_128X64_4X u8g(13, 11, 12);
String inputString = "";
boolean stringComplete = false;
String command = "";
String value = "";
char inChar = "";
void draw(void) {
u8g.setFont(u8g_font_unifont);
u8g.setPrintPos(1,20);
u8g.print(inputString);
//u8g.setFont(u8g_font_osb21);
//u8g.drawStr( 0, 22, "Hello World");
}
void setup(void) {
// assign default color value
Serial.begin(9600);
inputString.reserve(50); // reserve 50 bytes in memory to save for string manipulation
if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
u8g.setColorIndex(255); // white
}
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
u8g.setColorIndex(3); // max intensity
}
else if ( u8g.getMode() == U8G_MODE_BW ) {
u8g.setColorIndex(1); // pixel on
}
else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
u8g.setHiColorByRGB(255,255,255);
}
}
void loop(void) {
// picture loop
while (Serial.available()>0) {
// get the new byte:
char inChar = (char)Serial.read();
inputString += inChar;
// add it to the inputString:
// if the incoming character is a newline or a carriage return, set a flag
// so the main loop can do something about it:
if (inChar == '\n' || inChar == '\r') {
stringComplete = true;
}
}
if (stringComplete) {
//Serial.println(inputString);
delay(100);
// identified the posiion of '=' in string and set its index to pos variable
int pos = inputString.indexOf('=');
// value of pos variable > or = 0 means '=' present in received string.
if (pos > -1) {
// substring(start, stop) function cut a specific portion of string from start to stop
// here command will be the portion of received string till '='
// let received string is open=test123
// then command is 'open'
command = inputString.substring(0, pos);
// value will be from after = to newline command
// for the above example value is test123
// we just ignoreing the '=' taking first parameter of substring as 'pos+1'
// we are using '=' as a separator between command and vale
// without '=' any other character can be used
// we are using = menas our command or password must not contains any '=', otherwise it will cause error
value = inputString.substring(pos+1, inputString.length()-1); // extract command up to \n exluded
//Serial.println(command);
//Serial.println(value);
// password.compareTo(value) compare between password and value string,if match return 0
}
// clear the string for next iteration
inputString = "";
stringComplete = false;
}
u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );
// rebuild the picture after some delay
//delay(50);
}



