LC-Display - Improve my own library

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

Welcome to the world of Arduinos, it sounds like you're having fun!

I haven't really inspected your actual code (ie what it's doing), but there are some programming Best Practices that you might like to adopt to make it easier for yourself (in the future when you come to enhance or debug some code you've previously written) and when you post stuff here for others to inspect.

In no particular order:

  • Don't use magic strings or magic numbers. User meaningful names (constants, defines whatever). So digitalWrite (pinBit6....) is problematic inasmuch that you have not really described what pinBit6 is all about.
  • Similarly, using int i=0 is a definite no-no; don't use meaningless variable names (only exception in a for-loop count) and don't use magic numbers here either.
  • Use comments - to yourself, really. Describe the purpose of the code, so in the above int i=0 (which you will doubtless improve!) have a comment such as // Initialise temperature previous value. Don't have a comment which is just pseudo-code (set count to zero - yes we can SEE that, but why?)
  • Comments above each method also helps you (a) find them in the listing and (b) tells you what it's all for. One method for one responsibility, that is, don't do more than one thing in a method. So, for example, send a character to an LCD screen but don't ALSO flash an LED, that would be two things.

Everyone breaks the rules sometimes but until you know WHY you should try and follow some fundamental principles because it helps YOU read you code in a more-or-less self-documenting way.

Oh, and as a noob you may be interested in my ever-growing list of YouTube videos that describe a lot of beginner-related stuff. URL is in the footer of this post. You don't have to subscribe but if you like what you see please consider it!

It's nice to see someone new who's enthusiastic about the Arduino, the possibilities are endless, only constrained by your imagination!

You’re totally right, I really need to comment my code more and use meaningfull names for variables.
Ive got a few loops by now and I always just use “i” there, i should switch that to loopcounter or something…
Thanks for pointing that out!
The 2 actions in 1 method thingy is right, but this is kinda special, because the lcd is in 4bit mode and I need to transmit 8bit, so I need to flash the enable button twice. I didnt wanted to split that up, would that really be more useful?

And I really need to learn to properly describe what I want. Thanks for the kind post tho, even know my first post was kinda bad. I will definitly look into your Channel when im off work!

Back to the topic:
I will continue to improve the code (and comments :slight_smile: ) and I will be back.
Btw. Just found out that Strings kinda suck, they use so much space, I was on 14% global variable space and “re-did” pretty much the same code, but bytes and Im on 8% now + the code is much more “clean?”.
I will try to avoid Strings when not necessariy for now.

BTW I pretty much wanna make my own calculator with this project. He should be able to display text, numbers and do the whole format right.

My plan (at the moment) looks like this:
-Create a function that reads the Command-Byte and transmit twice as 4bit package to the LCD
-Create all methods/functions for the commands availible (transmit the commandcode to the first function)
-Create a way to display the text on the screen (maybe a string, read all the chars with readbyte and send them to the first function)
-Somehow do the formatting of the whole thing (havent really looked into that yet)
^That shouldve been in the first post :confused:

sazego:
I will try to avoid Strings when not necessariy for now.

Strings (with a capital 'S') are a construct invented to make life easier for Arduino developers; so a byte array is really what supports it in the background. You don't have to necessarily avoid Strings just be aware that each byte takes away 1 byte from your precious run-time memory.

But it seems you are already well on the track to improve your code (and recognise what you need to do first) so good luck and do come back with further questions as you move your project forward. And to avoid the scathing comments of contributors more experienced (and less tolerant) than me, do Google your question first (and look on the Arduino Playground too, plenty of examples on how to do things there).

See you around maybe!