Simple texteditor with Arduino and 40x2 LCD

Hello,

I would like to do a simple texteditor with Arduino and 40x2 LCD (no i2c). I wrote a simple code for testing, but I cannot modify the code myself.

Here's my code:

//Simple texteditor for Arduino and 40x2 LCD

#include <LiquidCrystal.h>
#include <SD.h>

LiquidCrystal lcd(13, 11, 10, 9, 8, 7);

const int buttonAPin = 15;
const int buttonBPin = 16;
const int buttonCPin = 17;

String cachedText = "";

void setup() {
  lcd.begin(40, 2);
  lcd.blink();

  pinMode(buttonAPin, INPUT_PULLUP);
  pinMode(buttonBPin, INPUT_PULLUP);
  pinMode(buttonCPin, INPUT_PULLUP);
}

void loop() {
  int buttonAState = digitalRead(buttonAPin);
  int buttonBState = digitalRead(buttonBPin);
  int buttonCState = digitalRead(buttonCPin);

  if (buttonAState == LOW) {
    cachedText += "a";
  }
  if (buttonBState == LOW) {
    cachedText += "b";
  }
  if (buttonCState == LOW) {
    cachedText += "c";
  }

  lcd.setCursor(0, 0);
  lcd.print(cachedText);

  delay(100);
}

Now, with this code when I press the buttons (a,b,c) many times so that the text fills the whole LCD (40x2), the cursor gets back to position (0,0)and starts overwriting the existing letters in the first row...and I don't want that. I would like it to continue to next row in the LCD just like you would expect in a simple texteditor like this.

I would also like to add 4 navigation buttons (up, down, left, right) to navigate the text that I have written.

Is there anyone who can help me?

Later I would like to have "save text to SD-card" function after the texteditor is finished...

Thank you!
, Pete.

So that you don't overwrite one character over another you basically need to do two actions.
The first is to define the line and position on the LCD of the next character to be typed,
and secondly use a variable as a counter of already typed characters.

The first one uses the " lcd.setCursor(x, y);
where x is the position and Y is the line.
The first position is 0 and the second is 1.......
The first line is 0 and the second is 1.......

After you understand the setCursor(x,y); (@ruilviana) and make the U/D/L/R work, consider using the "underline" character when in "move" mode and the "block" character when in edit mode... you will need to keep track of the character being edited. Sounds like a fun project.

You can get a huge head start on this by obtaining an SD module, installing the SD library, and playing with the example sketches that come with it.

Thanks for the answers!

Ruilviana, I undestand what you are trying to say, but I simply cannot translate that to the code...please, could you modify the code?

Aarg, yes, I already printed out those SD-module examples, I'm studying those now! I already managed to save some text to SD-card, yeah! Things are progressing!

Xfpd, I would like to use LCD's lcd.blink()-command to see where the cursor is moving in the text...if that's what you meant...Yes, this is fun project! But all the fun stops when you get stucked...LOL.

Ok,
try this code:

//Simple texteditor for Arduino and 40x2 LCD

#include <LiquidCrystal.h>
#include <SD.h>

LiquidCrystal lcd(13, 11, 10, 9, 8, 7);

const int buttonAPin = 15;
const int buttonBPin = 16;
const int buttonCPin = 17;
int count = 0;
int line = 0;

String cachedText = "";

void setup() {
  lcd.begin(40, 2);
  lcd.blink();

  pinMode(buttonAPin, INPUT_PULLUP);
  pinMode(buttonBPin, INPUT_PULLUP);
  pinMode(buttonCPin, INPUT_PULLUP);
}

void loop() {

  if (digitalRead(buttonAPin) == LOW) {
    delay(30);
    if (digitalRead(buttonAPin) == HIGH) {
      cachedText = "a";
      display();
      count++;
    }
  }

  if (digitalRead(buttonBPin) == LOW) {
    delay(30);
    if (digitalRead(buttonBPin) == HIGH) {
      cachedText = "b";
      display();
      count++;
    }
  }
  if (digitalRead(buttonCPin) == LOW) {
    delay(30);
    if (digitalRead(buttonCPin) == HIGH) {
      cachedText = "c";
      display();
      count++;
    }
  }
  if (count == 40) {
    if (line == 0) {
      line++;
    }
    else {
      line = 0;
      lcd.clear();
    }
    cachedText = "";
    count = 0;
    display();
  }
}
void  display()
{
  lcd.setCursor(count, line);
  lcd.print(cachedText);
  delay(100);
}

Thank you for the code! I have to test it now. I will get back to you.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.