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