Hello Guys
I programmed the LC-Display (LCM1602 the one in the starter packet) on my own, without library.
Since I just started with the arduino I thought it would be a good practice to do so.
Im using the Display in the 4Bit mode.
And I can controll the LCD just fine, everything works as it should (after 2days and alot of reading and failing :)).
To make things clear:
The target of this is, that I can set my functions with a simple dualcode and not turning every pin on and off manually.
I will need to update it for numbers & characters tho. Not sure how thats gonna work yet tho.
BUT I think I took a wrong approach to the whole thing, so I wanted to ask some pros how they wouldve done it differently.
So, Im just gonna post my progress, how I started and optimized, keep in mind I started like a week ago with the Arduino.
My start. YEAH, that is no joke, I wanted to try how it/if it even works.
pinBit are my outputs on DB7-DB4
enablePin is the enable Pin (captain obvious is obvious)
//1st function set -- all function sets are in 8 bit mode
digitalWrite(pinBit5, HIGH);
digitalWrite(pinBit4, LOW); //0 = 4bit mode, 1 = 8bit mode
digitalWrite(enablePin, HIGH);
digitalWrite(enablePin, LOW);
//2nd function set
digitalWrite(enablePin, HIGH);
digitalWrite(enablePin, LOW);
//3rd function set
digitalWrite(pinBit5, LOW);
digitalWrite(pinBit6, LOW); //0 = display off for reset
digitalWrite(pinBit7, HIGH); // 0 = 1 line mode, 2 = 2 line mode
digitalWrite(enablePin, HIGH);
digitalWrite(enablePin, LOW);
delay(200);
Well that is just the first reset sequence, but I think ull get the hang of it, the whole programm looked like that.
After that worked I tried to somehow get the outputs together - I tried Arrays, but since u cant overwrite an array (as a whole) and I didnt wanted to do everything manually I used a String.
That said, heres my solution with the String, with a example of one of my functions.
(Im trying to give a simple dualcode to print my messages to the LCD)
–dualCode is a string
Function
void clearDisplay() {
digitalWrite(commandOrData, LOW);
dualCode = "00000001";
dualToLcd(dualCode);
delay(3);
}
And here my string “solution”…
void dualToLcd(String theString) {
int i = 0;
int output = 8;
int stringLength = theString.length();
String stringChar;
//set the data output to high
for (i; i < (stringLength/2); i++) { //go through this loop half of the length of the string.
output = output - 1; //change the output to - 1 every time the loop gets activated
stringChar = theString.charAt(i); //go up a letter in the string every time the loop gets activated
if (stringChar.equals("1")) { //check if string is 1
digitalWrite(output, HIGH);
}
if (stringChar.equals("0")) { //check if string is 0
digitalWrite(output, LOW);
}
}
//Flash enable pin for the first 4bits
digitalWrite(enablePin, HIGH);
digitalWrite(enablePin, LOW);
//set the outputpin to 8 again, so he starts at pin 7 (-1)
output = 8;
for (i; i > (stringLength/2-1 && i < stringLength); i++) { //same as above, just with the start after the loop before
output = output - 1;
stringChar = theString.charAt(i);
if (stringChar.equals("1")) {
digitalWrite(output, HIGH);
}
if (stringChar.equals("0")) {
digitalWrite(output, LOW);
}
}
//flash the enable Pin a 2nd time to confrim the "transmission"
digitalWrite(enablePin, HIGH);
digitalWrite(enablePin, LOW);
delay(1);
}
And here an example where I just wanna change the display.
void displayOn() {
digitalWrite(commandOrData, LOW);
//Set the display On, without changing Cursor or blinking
dualCode.setCharAt(0, '0');
dualCode.setCharAt(1, '0');
dualCode.setCharAt(2, '0');
dualCode.setCharAt(3, '0');
dualCode.setCharAt(4, '1');
dualCode.setCharAt(5, '1');
dualToLcd(dualCode);
delay(1);
}
If anyone read this far, thanks already!
Im having a blast programming the Arduino and learning tons of new stuff.
If u guys got any advice how I can improve, or what I shouldve used instead please tell me.
An example would be awesome!
Thanks in advance and have a good day
In best regards
sazego