Blinking LED from serial

Hello guys, I need to write a program that blinks a LED equal to the number I enter from the keyboard. So far, this program can blink only upto 9 times. How can I increase this to two or three digits?

int x,n;
void setup()
{
  pinMode(13,OUTPUT);
  Serial.begin(9600);
  Serial.println("Input");
}

void loop()
{
  if(Serial.available ()>0)
    {
     n=Serial.read();
     for(x=48; x<n; x++)
     {
       Serial.print(x-47);
       Serial.println(": LED");
       digitalwrite(13, HIGH);
       delay(500);
       digitalwrite(13, LOW);
       delay(500);
     }
  }
}

I’m using parseInt(), and I had to add a check for isdigit().

// Tested with Arduino 1.6.0 on Arduino Uno

void setup()
{
  pinMode(13,OUTPUT);
  
  Serial.begin(9600);
  Serial.println("Input");
  
  Serial.setTimeout(200); // timeout for parseInt
}

void loop()
{
  if( Serial.available() > 0)        // something available ?
  {
    if( !isdigit(Serial.peek()))     // is it a digit ?
    { 
      Serial.read();                 // throw away CR LF and other things
    }
    else                             // going to read a number
    {    
      int n = Serial.parseInt();     // read integer with timeout
  
      Serial.print("n = ");
      Serial.println(n);
  
      for(int i=0; i<n; i++)
      {
        digitalWrite(13, HIGH);
        delay(500);
        digitalWrite(13, LOW);
        delay(500);
      }
    }
  }
}

I got one from adafruit website, it also works. It also uses the ‘parseInt()’ function. What does parseInt() does???

Do you have a link to the Adafruit code ? I like to see which one is better ;)

Serial.parseInt() read digits from the Serial stream. It stops when a non-digit is received or when there is a timeout. After that the digits are converted to a normal binary variable.

CR = Carriage Return is '\r' LF = Line Feed is '\n'

Suppose sending this via the serial terminal (not the spaces) : 1 3 5 3 CR LF The 1 3 5 3 are read, and the CR stops the parseInt, so the number 1353 is returned.

Suppose sending : 3 4 6 A timeout will stop parseInt, an the number 346 is returned.

The original code was for running a stepper motor using L298n, I have modified it for blinking a LED:

int x, pin1=13;
void setup()
{
pinMode(pin1, OUTPUT);
Serial.begin(9600);
Serial.println(“Input:”);
}

void loop()
{
if(Serial.available())
{
int steps= Serial.parseInt();

for(x=0;x<steps;x++)
{
Serial.print(x+1);
Serial.println(": LED");
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
}
}
}