Delay at end of loop - Barcode scanner

Hi,

I have a barcode scanner set up with the PS2 library and the scanner I have does not appear to send any special characters (such as the enter button) at the end of it's messages. I want to capture the code and then transmit it serially over a ESP8266 which I can imagine won't be an issue.

The issue I am having is determining the end of the code (as I want to send the whole code with a line feed) due to the lack of termination character, the codes will be of variable length so I can't check whether or not the number of characters has hit a particular amount.

I figured that I need to look at the time elapsed since the last character read and if it hit a modest number then it would likely be the end of the code (I thought 500ms/1s) and to send the code. I've got the bottom code but it does not work and instead prints a very nice ASCII triangular shaped output. This is the code I have so far;

#include <PS2Keyboard.h>

const int DataPin = 8;
const int IRQpin =  3;
String scannedBarcode;
long previousMillis = 0;

PS2Keyboard keyboard;

void setup() {
  delay(1000);
  keyboard.begin(DataPin, IRQpin);
  Serial.begin(9600);
  while (!Serial) {
  }
  digitalWrite(13, HIGH);
  delay(500);
  digitalWrite(13, LOW);
}

void loop() {
  
  if (keyboard.available()) {
    char c = keyboard.read();
    scannedBarcode.concat(c);
    previousMillis = millis();
  }

  if ((previousMillis + 1000) > millis()) {
  Serial.println(scannedBarcode);
  previousMillis = 0;
  }
  
}

The output looks like;

9
97
978
9780
97807
978076
9780764
97807645
978076455
9780764556
97807645565
978076455658
9780764556586

So I know that it is going into the bottom loop which it should not be - is there any chance someone could put me out of my misery, I know I am probably not far off but it is driving me up the wall!

Thanks

First, quote-tags are NOT code-tags. Can you please edit that?

And of course, millis() will keep on ticking. So instead of setting previousMillis to 0, set it actually to millis!

And while you're at it, change the check to
previousMillis - millis > interval
That why you don't get into trouble when millis() rolls over. Now you do :wink:

Thank you - I managed to get it working and have edited my post. I think looking at VB for 15 years has brainwashed me.