Storage in variables

Hello, I´m new in Arduino and i have a problem with a code.
When I deposit the number 35 for the monitor series does not do at all my code, only it works if I put individual numbers as 0, 1, 2, .... , 9.

I do not manage to store in the variable numbers more of two digits since it it is 35.

#define led 13
int n;

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

void loop()
{
n = Serial.read();
n-=48;

if(n == 35)
{
digitalWrite(led, HIGH);
Serial.println(n);
}

}

When you type in your Serial monitor 35 and send you are sending 2 bytes. Serial.read() only read one byte at a time so n will always just store one byte even if it an int!
When you do:

n = Serial.read();

You are just grabbing the first caracter

if(n == 35)

n will never be 35, it will be just 3

n = Serial.read();//Don't try to read if you don't have nothing to read Use if(Serial.available() >0) n = Serial.read();
n-=48;

if(n == 35)
{
digitalWrite(led, HIGH);
Serial.println(n);
}

Tomorrow I will know a BIT more than yesterday

Don't BYTE off too much today.

When there are 2 characters available, read the first char, do your -48, then multiple it by 10, read the second char,-48, add it it the previous.

Even I could not have managed to do this code not with his recommendations, they can show me an example since do it please

Hints:
if(Serial.available() =>2)
{
n = Serial.read();
n = n - 48;
n = n * 10;
n = n + (Seral.read() - 48);

}

You'll need to change this to get numeric data, but this is a start:

char incomingByte;

void setup()
{
  // start serial port at 9600 bps:
  Serial.begin(9600);
}

void loop()
{
  if (Serial.available() > 0) {    // Anything there?
    incomingByte = Serial.read();  // Yep...read it.
    Serial.print(incomingByte);    // Show it...            
  }
}

LarryD:

Tomorrow I will know a BIT more than yesterday

Don't BYTE off too much today.

Very punny :smiley:

Oh! Thank you very much for his help. :grin: