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