Been lurking around the forums for a few months now and have learned a lot about Arduino and programming for it - but now my bits of code are getting messy and would like some help from the experts! (Before getting arduino, I had never written a program, unless the old "10 PRINT..." command on the c64 counts!)
I'm trying to build a prototype system, which will prompt on an LCD display for the user to enter their name on the PS2 keyboard, then print that name on a label printer.
I have everything hooked up, the LCD prompts for the name, but so far, the only way I have managed to capture the string that the user enters on the keyboard is a messy piece that looks like this:
I.e n1= First letter, n2 = second letter and so on. (I also need to count the number of characters, as well as work out how many 'I's are in it so I can align it to the center.)
As you can see, doing that for each character entered is very messy and ineffieient.
Is there any better way of doing this? I need the name to be stored as a string so I can manipulate the string later in the code.
As you can see, doing that for each character entered is very messy and ineffieient.
Yes but that is what has to happen at some level.
So what you as saying is that you don't want to write that bit, you want some one else to have written it for you and buried it under some layers of an operating system or library.
So what you as saying is that you don't want to write that bit, you want some one else to have written it for you and buried it under some layers of an operating system or library.
No not at all! If the direction I am going with my first code is the only way to do what I am trying to do - then so be it! It's just as I said, I am VERY new to programming and had no idea if I was doing it the best way.
You want some kind of loop to keep repeating the "get a key" code without physically typing it out in your program umpteen times, and a character array to store the incoming characters in.
This hasn't been tested and probably isn't complete, but it should give you the idea:
#define MAXNAMELENGTH 40
int getkeyb()
{
char key = 0;
while(!keyboard.available()) {}
key = keyboard.read();
if ((key== PS2_KC_ENTER) || (key == PS2_KC_BKSP)) {
return (key);
}
else if (key == PS2_KC_ESC) {
loop();
}
else {
lcd.print(key);
}
return (key);
}
int namearray[MAXNAMELENGTH];
void loop() {
int c = 0;
int numchars = 0;
while (c != PS2_KC_ENTER) {
c = getkeyb();
if (c == PS2_KC_BKSP) {
lcd.cursorTo(numchars, 2); // assuming first parameter is column to move to
lcd.printIn(" ");
lcd.cursorTo(numchars, 2);
numchars = numchars - 1;
}
else if (c == PS2_KC_ENTER) {
namearray[numchars] = 0; // Terminate name string
}
else {
namearray[numchars] = c;
numchars = (numchars + 1);
}
}
// do stuff here with string in namearray
}