need help with some simple coding

hi there!

I am new to the amazing arduino thingamajig, and i need some help.
I hooked up an LCD to my arduino uno, and i am trying to get this to stop after 'z' then clear up and start again from the beginning.
can't find the right command to do this.

#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

int thisChar = 'a';

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // turn on the cursor:
  lcd.cursor();
}

void loop() {
    lcd.write(thisChar);
  delay(1000);
  thisChar++;
  if (thisChar == 'p'){
   lcd.write(thisChar);
    delay(1000);
    int (thisChar = 'q');
    lcd.setCursor(0, 1);
   lcd.write(thisChar);
    delay(1000);
    thisChar++;
  }
  if (thisChar == 'z'){
    lcd.write(thisChar);
    delay(1000);
    lcd>home();
  }
}  ]

also i can probably simplify this whole code and make it much smaller, so any advice would be very helpful.

thanks a million
shmily

abcd_sketch.ino (620 Bytes)

Welcome to the Forum. Please read this post:

How to use this forum - please read.

Please post your code using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpreted by the forum code as italics or funny emoticons.

Unless the sketch is too large, it's better if you post your code, rather than attach it. When it's attached, we have to download it, create a folder then open your code in our IDE. And afterwards, the folder remains unless we navigate to the "Temp" folder and manually remove it. It's much easier to just view the code in your post.

Also please explain what the code does and what you want it to do exactly.

int thisChar = 'a';

Nonsense. The type of a variable named thisChar should obviously be float.

    int (thisChar = 'q');

More nonsense. What are those parentheses doing? Why are you assigning 'q' to thisChar?

If you want something to happen a fixed number of times, and then stop forever, put that something in a for loop in setup(). Leave loop() empty.

PaulS:

int thisChar = 'a';

Nonsense. The type of a variable named thisChar should obviously be float.

Definitely. Especially when trying to reassign a type to a variable, should never be the same as what was used before.

By the way, resetting thisChar to 'A' would also help to get it to start from the beginning of the alphabet again.

wvmarle:
Also please explain what the code does and what you want it to do exactly.

I am trying to get the lcd tho print all the letters of the alphabet.
When it gets to 'p' i made it go back to the second line, and continue from there until 'z', then i want it to repeat the whole loop.

Everything works fine until i want it to start again. It just doesn't, it continues printing gibberish.

thanks

PaulS:

int thisChar = 'a';

Nonsense. The type of a variable named thisChar should obviously be float.

    int (thisChar = 'q');

More nonsense. What are those parentheses doing? Why are you assigning 'q' to thisChar?

If you want something to happen a fixed number of times, and then stop forever, put that something in a for loop in setup(). Leave loop() empty.

I am really new to this, so would you mind explaining why a float would be better for this?

thanks

shmilylauber:
I am trying to get the lcd tho print all the letters of the alphabet.
When it gets to 'p' i made it go back to the second line, and continue from there until 'z', then i want it to repeat the whole loop.
Everything works fine until i want it to start again. It just doesn't, it continues printing gibberish.

The answer to that I gave you already. Reset your variable.

I am really new to this, so would you mind explaining why a float would be better for this?

thanks

That was just us messing with you.

The real question is: why would you declare a variable called "char" as type "int" in the first place? And, related, why do you redeclare this?

The real question is: why would you declare a variable called "char" as type "int" in the first place? And, related, why do you redeclare this?

I don't really know. i saw that online for a similar project i was making, so used the same variable.
And of course i can't find that article now online :roll_eyes:.

And i redeclared it cause that's the only way i could think of making it continue on the next line, and further to reset it when it finishes.

There's probably a much more simple way to write such a code, i just don't know it.

Letters of the alphabet should be stored in a variable of type char. You can store them in a variable of type int but that just wastes memory because a letter just needs 1 byte whereas an int uses 2 bytes.

...R

Ok thanks for that tidbit, i'll fix up my code.
But can someone help with my code, please.
it would be greatly appreciated.

All fixed up. here's my revised sketch.

#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

int thisChar = 'a';

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // turn on the cursor:
  lcd.cursor();
}

void loop() {
    lcd.write(thisChar);
  delay(1000);
  thisChar++;
  if (thisChar == 'p'){
   lcd.write(thisChar);
    delay(1000);
    int (thisChar = 'q');
    lcd.setCursor(0, 1);
   lcd.write(thisChar);
    delay(1000);
    thisChar++;
  }
  if (thisChar == 'z'){
    lcd.write(thisChar);
    delay(1000);
    lcd.clear();
    int (thisChar = 'a');}
}

thank you everyone for your help, especially wvmarle!

shmilylauber:

int thisChar = 'a';

Make this a char.

shmilylauber:

    int (thisChar = 'a');}

No need to redeclare the variable here. Just assign the new value 'a'.

    thisChar = 'a';
  }

No need to redeclare the variable here.

That code does not declare a new variable. The parentheses there actually cause the int() "function" to be called, which is really a macro that really does a cast. What the compiler actually sees is:

   (int)thisChar = 'a';

Well, thisChar is already an int, so the cast is useless.

So, the parentheses in the original code are useless. Removing them, though, completely changes the meaning of the statement. Which would, in this case, be a good thing, because then it becomes perfectly obvious that the statement is wrong.