Capture Started Strings with %t and %u

Asked

Viewed 45 times

2

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

Usually with single characters, example 'O',I use part of the function below, now what I’m not getting is that this buffer only matches the values that start in %t or %u so that I store 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!

1 answer

1

I don’t know Arduino, but by your explanation and by the code you showed the logic could be more or less 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 '%'
      // ...
   }
}
  • Good, I hadn’t thought of it that way. Today I can’t test because I kept my plates, but I’ll try this logic and put it here if it worked. Thanks so far.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.