Converting String to Int

so, i'm new to the arduino, but it's been pretty easy to learn so far. however, i ran into a problem. i searched the forum for an answer but i couldn't really find what i was looking for.

void setup()
{
  Serial.begin(9600);
  pinMode(5, OUTPUT);
  
}

void loop()
{
String read1 = getstring().toLowerCase();
if(read1 == "on")
{
   digitalWrite(5, HIGH);
   Serial.println("LIGHT: ON");
}

if(read1 == "off")
{
   digitalWrite(5, LOW);
   Serial.println("LIGHT: OFF");
}

if(read1 == "blink")
{
   Serial.println("BLINKING...");
   digitalWrite(5, LOW);
   delay(500);
   digitalWrite(5, HIGH);
   delay(500);
   digitalWrite(5, LOW);
   delay(500);
   digitalWrite(5, HIGH);
   delay(500);
   digitalWrite(5, LOW);
}

if(read1.indexOf("dim:") >=0)
{
  String parsedstring = read1.replace("dim:", "");
  Serial.println("Light Dimmed to ");
  Serial.println(parsedstring);
  analogWrite(5, atoi(parsedstring));
}


delay(300);
}

String getstring()
{
  String result;
  while(Serial.available() > 0)
  {
    result = result + byte(Serial.read());
  }
  return result; 
}

Basically the intent of the code is to take data from serial and make an LED do stuff.
when you type on, it turns on, off it turns off, blink it blinks, and dim:4 it dims to 4 it's all worked out pretty well except the dim part

if(read1.indexOf("dim:") >=0)
{
  String parsedstring = read1.replace("dim:", "");
  Serial.println("Light Dimmed to ");
  Serial.println(parsedstring);
  analogWrite(5, atoi(parsedstring));
}

if i comment out analogWrite it sends the right value to the console, but i'm having trouble converting the String to an Integer
i keep getting

error: cannot convert 'String' to 'const char*' for argument '1' to 'int atoi(const char*)'

The String class has a toInt() function that performs an atoi() on the wrapped char array. But, before you get too excited, you have a serious problem that needs to be fixed.

String getstring()
{
  String result;
  while(Serial.available() > 0)
  {
    result = result + byte(Serial.read());
  }
  return result; 
}

The object result is allocated on the stack. You return a pointer to the object on the stack. As soon as the function returns, the stack is subject to being overwritten at any time, trashing the pointer that you returned.

You need to pass getString() the address of a String object that it can store the data in, rather than having the function declare and return a String object.

void getString(String &result)
{
  while(Serial.available() > 0)
  {
    result = result + byte(Serial.read());
  }
}

If (I really mean when) you do this, you'll need to move the function to before where it is called, or add a function prototype to the top of the file. The IDE creates function prototypes for all functions except those that have reference arguments.

I appreciate the help. it's working now. I'm not well educated in memory allocation. I'm pretty armature at some of this. could you dumb it down a little, or rewrite the code as a hole so i could look at it and try to get an understanding?

I'm pretty armature at some of this.

Well maybe you'll get around to learning the language...

I already re-wrote the function for you. Surely you can figure out how to cut and paste to move the function before loop().

The call to the function now looks like:

String read1 = getstring().toLowerCase();

Change that to:

String read1;
getString(read1);
read1 = read1.toLowerCase();