if (a == b) comparison seems to be ignored

Many thanks if anyone can spot an obvious mistake I must be making here.

This part of my sketch is supposed to check if two bytes are identical. if (a == b) {Serial.print("ok"); Serial.print('\n'); }

However, the serial monitor output says "ok", even when they are not equal.?

I am going to carry out a basic confirmation that the IR data is probably ok before acting on it, rather than 'send again' error correction. That is, binary switch the data bits and send a second check byte. Very unlikely random IR (mobile devices, key fobs) would produce that combination in two successive bytes. I checked with devices I have at hand.

Test sketch with above snippet;

[code]
//using serial monitor to test IRx
//needs 2 (double pairs) each character
  uint8_t bufferRx[2];
  uint8_t val;
  
void setup()
{
  Serial.begin(1200);
  pinMode(13, OUTPUT);

}

void loop()
{
  // if incoming serial
  if (Serial.available()) {
    readSerial();
    bufferRx[0] = val;
    digitalWrite(13, HIGH);
  } else {
    digitalWrite(13, LOW);
  }
  delay(10);
  if (Serial.available()) {
    readSerial();
    bufferRx[1] = val;
    digitalWrite(13, HIGH);
  } else {
    digitalWrite(13, LOW);
  }
  
  uint8_t a = bufferRx[0];
  uint8_t b = bufferRx[1];
  b = 255 - b;
  Serial.print(a);Serial.print('\n');
  Serial.print(b);Serial.print('\n');
  if (a == b)
  {Serial.print("ok");
   Serial.print('\n');
  }
  bufferRx[0] = 0;
  bufferRx[1] = 0;
  delay(100);
}

uint8_t readSerial() {
  val = Serial.read();
  return val;
}

[/code]

what happens when you run your test sketch?

the a = 0 and b = 255 values are printed, then the “ok”, that bit of code should have been skipped if the == was not true, which it isn’t.

interesting, your code didn't run for me...

you seem to do a lot of machinations of variables...

Try this:

uint8_t bufferRx[2] = {'b','b'};

bool gotValues = true;
int index = 0;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  if (Serial.available())
  {
    bufferRx[index++] = Serial.read();
    if (index > 1)
    {
      gotValues = true;
      index = 0;
    }
  }

  if (gotValues)
  {
    Serial.println(bufferRx[0]);
    Serial.println(bufferRx[1]);
    if (bufferRx[0] == bufferRx[1])
    {
      Serial.println("ok");
    }
    gotValues = false;
  }
}

Ok, changed your baud rate to match IRTX at 1200.

Gives the required comparison result. Thanks.

Not sure exactly why though. I do know the UART RCX register holds flag up if second byte has been received, but not wanting to go through Serial lib with a magnifying glass...

Changed your sketch lines to check for 'inverted bits' byte as follows; if (bufferRx[0] == (255-bufferRx[1]))

And this works, so thanks for all your effort. Ignores all attempts to introduce random IR signals.

BTW, first two bytes are always 98?

I moved your code from loop() to setup() so it would only run once. It works fine (except of course that it doesn't wait for input, but that is your design). I didn't change anything else except adding an else to the debug print to clarify the output, and change the baud rate.

//using serial monitor to test IRx
//needs 2 (double pairs) each character
uint8_t bufferRx[2];
uint8_t val;

void setup()
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);


  // if incoming serial
  if (Serial.available()) {
    readSerial();
    bufferRx[0] = val;
    digitalWrite(13, HIGH);
  } else {
    digitalWrite(13, LOW);
  }
  delay(10);
  if (Serial.available()) {
    readSerial();
    bufferRx[1] = val;
    digitalWrite(13, HIGH);
  } else {
    digitalWrite(13, LOW);
  }

  uint8_t a = bufferRx[0];
  uint8_t b = bufferRx[1];
  b = 255 - b;
  Serial.print(a); Serial.print('\n');
  Serial.print(b); Serial.print('\n');
  if (a == b)
  {
    Serial.print("ok");
    Serial.print('\n');
  }
  else
  {
    Serial.print("not ok");
    Serial.print('\n');
  }

  bufferRx[0] = 0;
  bufferRx[1] = 0;
  delay(100);

}

void loop()
{
}

uint8_t readSerial() {
  val = Serial.read();
  return val;
}

Output is:

0
255
not ok

There is something I don't understand with the monitor output.

Serial.println(bufferRx[0]); This prints out second byte transmitted, 0 Serial.println(bufferRx[1]);This prints out first byte transmitted, 255 if (bufferRx[0] == (255-bufferRx[1])) The condition is satisfied if == uses the correct order of bytes sent { Serial.println("ok");

monitor output is -

0 255 ok 0 255 ok ......

What don't you understand about it?

maybe I should re-write the test transmitter sketch with values other than 0 and 255.

But the first byte transmitted is not the first byte value printed! Serial.println(bufferRx[0]); this should be 255!

if (Serial.available()) {
    readSerial();
    bufferRx[0] = val;

You really need to use the function's return value, not the value of the global the function returns - this sort of programming just makes it look like you don't know what you're doing.

This is the condition test from the receive and monitor sketch - if (gotValues) { Serial.println(bufferRx[0]); //this value should be 170, value printed is 85 Serial.println(bufferRx[1]); //this value should be 85, value printed is 170 if (bufferRx[0] == (~ bufferRx[1])) { Serial.println("ok"); }

The '~' NOT is not compiled? I put in the extra else {Serial.println("not ok";}

So what I see on serial monitor is - 85 170 not ok

This is the test transmit sketch -

[code]
//test IR transmission using Arduino pro mini UART
//generates 38kHz carrier wave on pin 9 and 10
//sends data via TX every 500ms
void setup()
{
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);

  // Clear Timer on Compare Match (CTC) Mode
  bitWrite(TCCR1A, WGM10, 0);
  bitWrite(TCCR1A, WGM11, 0);
  bitWrite(TCCR1B, WGM12, 1);
  bitWrite(TCCR1B, WGM13, 0);

  // Toggle OC1A and OC1B on Compare Match.
  bitWrite(TCCR1A, COM1A0, 1);
  bitWrite(TCCR1A, COM1A1, 0);
  bitWrite(TCCR1A, COM1B0, 1);
  bitWrite(TCCR1A, COM1B1, 0);

  // No prescaling
  bitWrite(TCCR1B, CS10, 1);
  bitWrite(TCCR1B, CS11, 0);
  bitWrite(TCCR1B, CS12, 0);

  OCR1A = 210;
  OCR1B = 210;

  Serial.begin(1200);
}

void loop()
{
  Serial.write(170);
  delay(10);
  Serial.write(85);
  delay(500);
}

[/code]

simplystupid: BTW, first two bytes are always 98?

ASCII 98 = 0x62 = 0b01100010

it's all the same ;)

Serial.println(bufferRx[1]);
Serial.println(bufferRx[1], HEX);
Serial.println(bufferRx[1], BIN);
if (bufferRx[0] == (~ bufferRx[1]))

Let us assume [0] contains 170 or 0xAA, and that buffer [1] contains 85 or 0x55 What do you think the value of ~ bufferRx[1] is? 0xFFAA

You don't need to do any of this to implement your signature bytes. There is no special reason why one has to be the negation of the other. In fact, it makes more sense to use two arbitrary bytes, since there are many pairs that will pass. With a unique pair, only one pair will pass.

You're wasting your time and signal bandwidth. It would be more fruitful to send one signature byte and a checksum, for example.

I declared the bufferRx[] array with data type uint8_t? So the NOT '~' is expected performed on one byte of 8 bits, yes? Thanks for your explanation though, if it must be a result with two bytes.

Let us assume [0] contains 170 or 0xAA, and that buffer [1] contains 85 or 0x55 What do you think the value of ~ bufferRx[1] is? 0xFFAA

Thanks for tip regarding arbitrary byte and check sum. I'm using the bits within each byte to send data and multiplex code, keeping transmission short to limit impact of signal variations with relative movement between TX and RX optics.

The signal will probably swamp any interference, just trying the simplest method to reduce the odds of rogue bytes.

checksum - as in c = XOR or logic manipulation of one byte by another? I don't really want to start sending yet another two bytes for a checksum of eg byte a + b?

So the NOT '~' is expected performed on one byte of 8 bits, yes?

No. You've shown that it isn't - it's performed on an "int". If you want to compare the outcome of the ~ operation to a "uint8_t", cast the result to "uint8_t".

Thanks for the shove to do some work myself and stop whining.
Nick Gammon website page on Serial was helpful.
I adapted his example for data.
I’m sure this can be re-done a lot tidier, but it works and totally rejects attempts to inject some rogue codes.

I worked out a simple circuit with two PNP transistors in series with a 100 Ohm resistor and LED to GND. 1uF cap across transistor +side to ground. A 4k7 from the base of each transistor to Arduino TXO and PIN 9 allows to program without disconnecting. Same with IR TSOP38 sensor, 4k7 inline with signal to RX pin allows sketches to be uploaded with sensor in-situ.

[code]

const byte startOfDataDelimiter = 0;
const byte endOfDataDelimiter   = 255;
uint8_t bufferRx[4]{1,254,127,128};

void setup ()
  { 
  Serial.begin (1200);
  Serial.println ("Starting ...");
  } // end of setup
  
void processByte (const byte n)
  {
  Serial.println (n);
  }  // end of processNumber

void chksum ()
       {
       if (255 - bufferRx[0] == bufferRx[1])
       processByte (bufferRx[0]);
       
       if(255 - bufferRx[2] == bufferRx[3])
       processByte (bufferRx[2]);
       
       else {
       for (byte preset = 0;preset <=3;preset++)
       { bufferRx[preset] = 0;}
       }
       }
       
void processInput ()
  {
  static byte receivedByte = 0;
  static byte index = 0;
  byte c = Serial.read ();
  
  switch (c)
    {
    case endOfDataDelimiter:  
      {
      chksum();
      }
     break; 

    case startOfDataDelimiter: 
      {index = 0;} 
      break;
    case 1 ... 254:
      
       bufferRx[index] = c;
       index ++;
       
      break;
      
      
    } // end of switch  
  }  // end of processInput
  
void loop ()
  {
  
  while (Serial.available ())
    processInput ();
    
  // do other stuff here
  } // end of loop

[/code]