I managed to modify the code to compare the scanned code with the saved ones.
When I run the code, it only identifies the first barcode correctly and then nothing appear at the serial monitor, it's like the serial monitor freezes.
Despite that the buzzer make the sound of detecting the barcode, any idea why I have this problem ?
int SCAN_ENTER = 0x5a; int SCAN_BREAK = 0xf0;
int breakActive = 0;
int clockPin = 3;
int dataPin = 2;
int ledPin = 13; // When a SCAN_ENTER scancode is received the LED blink
int clockValue = 0;
byte dataValue;
byte scanCodes[10] = {0x45,0x16,0x1e,0x26,0x25,0x2e,0x36,0x3d,0x3e,0x46};
char characters[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
int quantityCodes = 10;
char buffer[64] = {}; // This saves the characters (for now only numbers)
char check[10]={'0','0','0','0','0','0','0','0','0','0'};
int bufferPos = 0;
int bufferLength = 64;
char tag1[5] = {'6','2','9','1','0'}; //masafi
char tag2[5] = {'6','2','9','1','1'}; //alain
char tag3[5] = {'6','2','9','7','0'}; //maiDubi
char tag4[5] = {'5','4','4','9','0'}; ////cocacola
char tag5[5] = {'0','1','2','0','0'}; ////7up
char tag7[5] = {'6','2','8','1','0'}; ////Al Rabie
char tag8[5] = {'4','7','9','2','0'}; ////Coconut water
int count=0;
//
void setup() {
pinMode(dataPin, INPUT);
pinMode(clockPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
}
void loop() {
dataValue = dataRead();
// If there is a break code, skip the next byte
if (dataValue == SCAN_BREAK) {
breakActive = 1;
}
// Translate the scan codes to numbers
// If there is a match, store it to the buffer
for (int i = 0; i < quantityCodes; i++) {
byte temp = scanCodes[i];
if(temp == dataValue){
if(!breakActive == 1){
buffer[bufferPos] = characters[i];
bufferPos++;
}
}
}
//Serial.print('*'); // Output an asterix for every byte
// Print the buffer if SCAN_ENTER is pressed.
if(dataValue == SCAN_ENTER){
Serial.print("\nbuffer: ");
// Read the buffer
int i=0;
if (buffer[i] != 0) {
while(buffer[i] != 0) {
Serial.print( buffer[i]);
check[i]=buffer[i];
buffer[i] = 0;
i++;
}
}
if (check[0] ==tag1[0] && check[1] == tag1[1] && check[2] ==tag1[2] && check[3] ==tag1[3] && check[4] ==tag1[4] )
{Serial.println("\tMasafi"); count=count+1; Serial.print("count=");Serial.println(count);}
if (check[0] ==tag2[0] && check[1] == tag2[1] && check[2] ==tag2[2] && check[3] ==tag2[3] && check[4] ==tag2[4] ) {Serial.println("\tAlain");}
if (check[0] ==tag3[0] && check[1] == tag3[1] && check[2] ==tag3[2] && check[3] ==tag3[3] && check[4] ==tag3[4]){Serial.println("\tMai Dubai");}
if (check[0] ==tag4[0] && check[1] == tag4[1] && check[2] ==tag4[2] && check[3] ==tag4[3] && check[4] ==tag4[4] ) {Serial.println("\tCocacola");}
if (check[0] ==tag5[0] && check[1] == tag5[1] && check[2] ==tag5[2] && check[3] ==tag5[3] && check[4] ==tag5[4]){Serial.println("\t7UP");}
if (check[0] ==tag7[0] && check[1] == tag7[1] && check[2] ==tag7[2] && check[3] ==tag7[3] && check[4] ==tag7[4]){Serial.println("\tAl Rabie");}
if (check[0] ==tag8[0] && check[1] == tag8[1] && check[2] ==tag8[2] && check[3] ==tag8[3] && check[4] ==tag8[4]){Serial.println("\tCocunut Water");}
Serial.println(" [Enter]");
Serial.print("Check=");
for (int i=0; i <= 10; i++){ Serial.print(check[i]); delay(10); }
bufferPos = 0;
// Blink the LED
digitalWrite(ledPin, HIGH);
delay(300);
digitalWrite(ledPin, LOW);
}
// Reset the SCAN_BREAK state if the byte was a normal one
if(dataValue != SCAN_BREAK){
breakActive = 0;
}
dataValue = 0;
}
int dataRead() {
byte val = 0;
// Skip start state and start bit
while (digitalRead(clockPin)); // Wait for LOW.
// clock is high when idle
while (!digitalRead(clockPin)); // Wait for HIGH.
while (digitalRead(clockPin)); // Wait for LOW.
for (int offset = 0; offset < 8; offset++) {
while (digitalRead(clockPin)); // Wait for LOW
val |= digitalRead(dataPin) << offset; // Add to byte
while (!digitalRead(clockPin)); // Wait for HIGH
}
// Skipping parity and stop bits down here.
while (digitalRead(clockPin)); // Wait for LOW.
while (!digitalRead(clockPin)); // Wait for HIGH.
while (digitalRead(clockPin)); // Wait for LOW.
while (!digitalRead(clockPin)); // Wait for HIGH.
return val;
}