Go Down

Topic: lcd.print("You: " + String(hp) + "hp"); (Read 982 times) previous topic - next topic

2C3D

Nov 01, 2015, 11:12 pm Last Edit: Nov 01, 2015, 11:17 pm by 2C3D
I'm messing around with my new arduino (uno) and LCD. New to this IDE, so I would love to know why this refuses to print. In the: String(hp) I am trying to refer to an hp integer (int hp = 10) and use it as a string.
lcd.print("You: " + String(hp)  + "hp");


So far I have this:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int hp = 10;
int ap = 1;
int multiplier = random(0.1, 1.9);
int ehp = 2;
int eap = 1;
int emultiplier = random (0.1, 1.9);
int switchpressed = 0;
String itname = "Slime";
int start = 0;
void setup() {
  // put your setup code here, to run once:
  lcd.begin (16, 2);
  pinMode(10, INPUT);
  pinMode(9, INPUT);
  pinMode(8, INPUT);
  lcd.print("r"); //This isn't the most efficient way of scrolling text, but I couldn't care less!
  delay(250);
  lcd.clear();
  lcd.print("PG");
  delay(250);
  lcd.clear();
  lcd.print("RPG");
  delay(250);
  lcd.clear();
  lcd.print(" RPG");
  delay(250);
  lcd.clear();
  lcd.print("r RPG");
  delay(250);
  lcd.clear();
  lcd.print("er RPG");
  delay(250);
  lcd.clear();
  lcd.print("per RPG");
  delay(250);
  lcd.clear();
  lcd.print("uperRPG");
  delay(250);
  lcd.clear();
  lcd.print("Super RPG");
  delay(250);
  lcd.clear();
  lcd.print(" Super RPG");
  delay(250);
  lcd.clear();
  lcd.print("  Super RPG");
  delay(250);
  lcd.clear();
  lcd.print("   Super RPG");
  delay(250);
  lcd.clear();
  lcd.print("    Super RPG");
  delay(250);
  lcd.clear();
  lcd.print("     Super RPG");
  delay(250);
  lcd.clear();
  lcd.print("      Super RPG");
  delay(250);
  lcd.clear();
  lcd.print("   ;   Super RPG");
  delay(250);
  lcd.clear();
  lcd.print("        Super RPG");
  delay(250);
  lcd.clear();
  lcd.print("         Super RP");
  delay(250);
  lcd.clear();
  lcd.print("          Super R");
  delay(250);
  lcd.clear();
  lcd.print("           Super ");
  delay(250);
  lcd.clear();
  lcd.print("            Super");
  delay(250);
  lcd.clear();
  lcd.print("             Supe");
  delay(250);
  lcd.clear();
  lcd.print("              Sup");
  delay(250);
  lcd.clear();
  lcd.print("               Su");
  delay(250);
  lcd.clear();
  lcd.print("                S");
  delay(250);
  lcd.clear();
  int start = 1;
}
 //Everything works up until here

void loop() {
  // put your main code here, to run repeatedly:
  lcd.begin (16, 2);
  lcd.print("You: " + String(hp)  + "hp");
  lcd.clear();
  delay(250);
}

david_prentice

#1
Nov 01, 2015, 11:39 pm Last Edit: Nov 01, 2015, 11:46 pm by david_prentice
The literal expression does not evaluate as a String.   So the print argument is not seen as a String.

Try this:

Code: [Select]

void setup()
{
    Serial.begin(9600);
    Serial.println("String concatenation");
    String s = "You: " + String(10) + "hp";
    Serial.println(s);
    Serial.println(s.length());
    int val = 15;
    Serial.println(String("Me: " + String(val) + "hp"));
}

void loop()
{
    // put your main code here, to run repeatedly:

}

You could equally well use lcd.print()

I have no idea what how this is actually accomplished.   I bet that it involves a lot of dynamic memory allocation.    I am sure that the C approach is a lot simpler to implement.

Incidentally,   you could use the lcd hardware to perform the scrolling.    Or just write a helper function that writes N spaces followed by len-N letters from your string.    You can do this with String methods or regular char array functions.

David.

ghlawrence2000

#2
Nov 02, 2015, 02:23 pm Last Edit: Nov 02, 2015, 02:32 pm by ghlawrence2000
It is kinda coincidental that you should say that!! I asked this EXACT question yesterday! http://forum.arduino.cc/index.php?topic=356782.msg2459553#msg2459553

Regards,

Graham
UTFT_SdRaw now included in library manager!! ;) High speed image drawing from SD card to UTFT displays for Mega & DUE.
UTFT_GHL - a VASTLY upgraded version of UTFT_CTE. Coming soon to a TFT near you! 8) Shipping April 1 2016!

KrisKasprzak

I'm surprised this line will not resolve in a string, i believe ive' done this before

lcd.print("You: " + String(hp)  + "hp");

I'm more suspicious of clearing the display immedaitaly after--i'm not sure you will ever see anything

lcd.clear();

call the delay before? or elimiate the lcd.clear()?

Thanks,

Kris

2C3D


Go Up