serialEvent problem

Hi,

I am sending three bytes of data through Android phone to Arduino via Bluetooth module.

When I am using code belove, everything works fine, without a single problem.

void serialEvent()
{

  byte buffer_rx [3];
  byte i = 0;
  
  byte deltaON_rx;
  byte deltaOFF_rx;
  
  while (Serial.available())
  {
    byte inByte = Serial.read();

    if (inByte == 0xFE)
    {     
      deltaON_rx = buffer_rx[0];
      deltaOFF_rx = buffer_rx[1];
      test_rx = buffer_rx[2];
       
      i = 0;
    }
    else
    {
      if (i < 3)
      {   
        buffer_rx [i] = inByte;
        i++;
      }
    }
    
    // EEPROM deltaON
    if (deltaON_rx != EEPROM.read(0)
    {
      EEPROM.write(0, deltaON_rx);
    }
    
    // EEPROM deltaOFF
    if (deltaOFF_rx != EEPROM.read(1)
    {
      EEPROM.write(1, deltaOFF_rx);
    }
  }
}

Now I want to add some kind of a security in case that some strange number would occur. Since I know what numbers to expect (from 3 to 15 for deltaON and from 1 to 13 for deltaOFF), I added just two simple condition before writing to EEPROM

    // EEPROM deltaON
    if ((deltaON_rx != EEPROM.read(0)) && (deltaON_rx >= 3) && (deltaON_rx <= 15)) 
    {
      EEPROM.write(0, deltaON_rx);
    }
    
    // EEPROM deltaOFF
    if ((deltaOFF_rx != EEPROM.read(1)) && (deltaOFF_rx >= 1) && (deltaOFF_rx <= 13)) 
    {
      EEPROM.write(1, deltaOFF_rx);
    }

But, it doesn’t work. Most of the time I get some random (wrong) numbers, when I send new values from Android app (usually 10 and 7), no matter what kind of numbers I send (in range from 3 to 15 for deltaON and 1 to 13 for deltaOFF).

I don’t understand what could be wrong, because if I remove this two conditions, everything works fine (tested many times).

Any idea is much appreciated :slight_smile:

Why don't you send the values received to the serial monitor to debug?

What happens in serialEvent if there's only one character to read?

Keep in mind that all serialEvent() does is save you the trouble of writing (as the last thing in loop() )

if (Serial.available() > 0) {
   mySerialFunction();
}

I would receive all of the data before analyisng any of it. One of the examples is serial input basics should be suitable.

...R

Robin2: Keep in mind that all serialEvent() does is save you the trouble of writing (as the last thing in loop() )

I was able to fix the problem. I renamed and changed function serialEvent so now I am calling function "getPacket()" from main loop.

  if (Serial.available() > 0) 
  {
    getPacket();
  }

Also, I changed variables in function for buffer and counter from local to global. Now it works as it should.

Again, thank you guys for all the advices! :D

luxy: Also, I changed variables in function for buffer and counter from local to global. Now it works as it should.

Changing them from local to static would also have worked.

Changing them from local to static would also have worked.

You mean, of course, keeping them local, but adding the static qualifier, would also have worked.