Why my program doesnt work (Serial Available)

This program should print some numbers if R or T letter comes first (R24324 or T343 should Serial.print (24324 or 343)

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  if (Serial.available()) {      // Look for char in serial queue and process if found
     if (Serial.read() == 84) { 
    setreg();
     }
     else if (Serial.read() == 82) {
      setreg();
     }
   }
 }
void setreg () {
 while (Serial.available()) {
  char incbyte = (Serial.read()-48);
uint8_t reg = (reg*10);
reg = incbyte;
          Serial.println(reg);
 }

What does the program do instead?

jremington:
What does the program do instead?

nothing

Then the usual approach is to put some Serial.println() statements into the program, to see what is being read, etc.

I did some debugging, that is doesnt work as should:
only first IF works, segond is ignored

void loop() {
if (Serial.available()) { // Look for char in serial queue and process if found
if (Serial.read() == 84) {
Serial.print("1");
}
else if (Serial.read() == 82) {
Serial.print("2");
}
}
}

Several problems:

First, in setreg(), you have:

void setreg () {
 while (Serial.available()) {
  char incbyte = (Serial.read()-48);
uint8_t reg = (reg*10);
reg = incbyte;
          Serial.println(reg);
 }

On each pass through the while loop, you redefine reg, discarding all previous values. Also, nothing is sent back to loop(). Also, using 82 and 84 is harder to read than using their ASCII equivalents. There are a few other hiccups, too.

This code should work:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  char input;
  int number;

  if (Serial.available()) {     
    input = toupper(Serial.read());         // Someone might enter lower case letters
    if (input == 'T' || input == 'R') {
      number = setreg();
      Serial.print("Input was: ");
      Serial.print(input);
      Serial.println(number);
    }
  }
}

int setreg () {
  char msg[10];
  int charsRead;

  while (1) {
    if (Serial.available()) {
      charsRead = Serial.readBytesUntil('\n', msg, sizeof(msg) - 1);
      msg[charsRead] = '\0';
      return atoi(msg);
    }
  }
}

While not everyone here likes the readBytesUntil() function because it blocks, it does make things pretty simple.

Or simpler change:

void loop() {
  if (Serial.available()) {      // Look for char in serial queue and process if found
byte incomingByte = Serial.read(); // read in a bye for processing

     if (incomingByte == 84) { 
      Serial.print("1");
     }
     else if (incomingByte == 82) {
      Serial.print("2");
      }
   }
 }

RastafarienTargaryen:
I did some debugging, that is doesnt work as should:
only first IF works, segond is ignored

void loop() {
if (Serial.available()) { // Look for char in serial queue and process if found
if (Serial.read() == 84) {
Serial.print("1");
}
else if (Serial.read() == 82) {
Serial.print("2");
}
}
}

if the first read fails it still removes the character / value from serial buffer. One solution is to save the returned value and than evaluate it.

if the first read fails it still removes the character / value from serial buffer.

True. Since the input comes in as "Rnnn" or "Tnnn", there should be one read to grab the character and, if it's the appropriate character, then read the numeric. This code can't work:

 if (Serial.read() == 84) {        // Read a 'T'
      Serial.print("1");
 } else 
      if (Serial.read() == 82) {   // Read an 'S'
         Serial.print("2");
      }

Given the expected format, it will never read an 'S' since the first Serial.read() already ate it.

The examples in Serial Input Basics read in all the data and then you can analyse it to your heart's content.

...R