LED and Serial Help

Hi, I have this code that takes the input from the serial monitor and turns on the led corresponding to the number entered. I have a few questions/improvements that I would like to make but I don't know how. Heres the code:

void setup()
{
  Serial.begin(9600);
  int pin;
  for(pin = 2; pin <=10; pin++)
  {
    pinMode(pin, OUTPUT);
  }
  pinMode(13, OUTPUT);
}

void loop()
{


  if (Serial.available() > 0)
  {
    int LED = Serial.read() - 48;
    digitalWrite(LED, HIGH);
    delay(200);
  }




  if(digitalRead(2) && digitalRead(3) &&digitalRead(4) &&digitalRead(5) &&digitalRead(6) &&digitalRead(7) &&digitalRead(8) &&digitalRead(9) == HIGH)
  {
    int count;
    for(count = 1; count <= 10; count++)  //Blinks All LEDS 10 Times to alert
    {
      int LEDNumber;
      for(LEDNumber = 1; LEDNumber <= 13; LEDNumber++)
      {
        digitalWrite(LEDNumber, HIGH);
      }
      delay(100);
      for(LEDNumber = 1; LEDNumber <= 13; LEDNumber++)
      {
        digitalWrite(LEDNumber, LOW);
      }
      delay(100);
    }
    int Reds;  //Turns all LEDS back on
    for(Reds = 2; Reds <= 10; Reds++)
    {
      digitalWrite(Reds, HIGH);
    }
    delay(500);
    Serial.print("All LEDs on. Turning off.");
    Serial.println("");
    int LEDoff;
    for(LEDoff = 2; LEDoff <= 10; LEDoff++) //Turns all LEDS off
    {
      digitalWrite(LEDoff, LOW);
      delay(100);
    }
    Serial.print("All LEDs are now off. Enter New Number to Continue.");
    Serial.println("");
  }
}

It currently works exactly how I want it to: When a number is entered in the serial monitor, it turns on the led connected to the corresponding pin. When all the pins 2-9 are on, it blinks all of them, turns them off and returns a message to the serial monitor. First, I would like to know how to receive multiple digit numbers from the serial monitor so I can use pins 10-13. When I currently type in a number such as 13, it attempts to turn on pin 1 then pin 13. Question 2 is why is it necessary for the -48 on the digital read line in the first part of void loop()? There is no logic behind it, it is just a pattern i noticed. When i type a number like 3 into the serial monitor, the arduino sees it as a value of 48 higher. Finally, how could i code it if I entered a number that was already on, the arduino would turn it off? For example if i entered 2 and 2 again, it would turn on then off.
Thanks for reading and hopefully you can shed some light on this :slight_smile:

Hi,

  1. To read more characters, you have read from serial until you got the End Of Line character. There is a setting for the EOL in the serial monitor, if you set it to Return, then the EOL will be '\r', so you store characters until a '\r" is found.

Here is a reply that I post often enough, because you aren't the first one to ask about this :wink:
http://forum.arduino.cc/index.php?topic=214145.msg1568144#msg1568144

  1. The logic behind the -48 thing, is that the ASCII decimal value of character '0' is 48, '1' is 49 ... '9' is 57. So to get the numerical value of an ASCII numeric character, you simply substract 48 from it :wink:

  2. Use a boolean variable and invert it everytime the command is entered:

//at the top of your program
bool ledState = false;

.......

//where you check your command:
ledState = !ledState; //invert
digitalWrite( ledPin, ledState );