Capture Strings Started with% t and% u

2

What modification can I make to capture the humidity and temperature of my serial port to insert a variable inside the variable that is printed on the serial port as% u0000 and temperature% t00.00.

Generally with unique characters, example 'O', I use part of the function below, now what I'm not getting is that this buffer only filters the values starting at% t or% u so I save in a variable and print on my LCD.

void recebeir(){
char valorlido = Serial.read();
if (valorlido == 'O'){ //liga ou desliga a tv
{ 
for (int i = 0; i < 1; i++) //Envia um flash de led com 3 comandos
irsend.sendRaw(S_pwr,68,38); //código clonado
delay(52);
 }
 Serial.println("Liguei ou Desliguei a TV");
 }

Thank you!

    
asked by anonymous 26.09.2016 / 01:15

1 answer

1

No Arduino connection, but for your explanation and the code you showed the logic could be something like this:

void recebeir()
{
   char temp[5], umid[5];

   char valorlido = Serial.read();

   if (valorlido == 'O')
   { //liga ou desliga a tv
      for (int i = 0; i < 1; i++) //Envia um flash de led com 3 comandos
          irsend.sendRaw(S_pwr,68,38); //código clonado
      delay(52);
      Serial.println("Liguei ou Desliguei a TV");
   }

   else if (valorlido == '%')
   {
      // leitura de 't' ou 'u'
      valorlido = Serial.read();
      if (valorlido == 't')
      {
          for (int i = 0; i < 5; i++)
              temp[i] = Serial.read();
          // temp agora tem a temperatura: nn.nn
      }
      else if (valorlido == 'u')
      {
          for (int i = 0; i < 5; i++)
              umid[i] = Serial.read();
          // umid agora tem a umidade: nn.nn
      }
      else
      {
         // nao e' 't' nem 'u'
         // ...
      }
   }
   else
   {
      // nao e' 'O' nem '%'
      // ...
   }
}
    
26.09.2016 / 02:47