Do you have "No line ending" selected in the menu at the bottom of Serial Monitor? If not, the line ending characters are keeping the '1' from continuing to be the latest character received.
char data = 0;
void setup()
{
Serial.begin(9600);
// LED's on pins 2 through 9
for (int i = 2; i < 10; i++)
{
pinMode(i, OUTPUT);
}
}
void loop()
{
for (int i = 2; i < 10; i++)
{
if (Serial.available())
{
data = Serial.read();
}
if (data == '1')
{
// Turn on 'the current LED' for half a second
digitalWrite(i, 1);
delay(500);
digitalWrite(i, 0);
}
else if (data == '2')
{
// Turn off all LEDs
for (int i = 2; i < 10; i++)
{
digitalWrite(i, 0);
}
}
}
}