I am using barcode scanner with Arduino.
I need to count the number of the scanned items. The code correctly scan and identify the items, but count variable does not increment. I am not sure what's the problem, and will really appreciate any help.
///////////////// serial LCD
int x =1;
#include <SoftwareSerial.h>
SoftwareSerial mySerial(15,14); /////first one doesnt matter
//////////////
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);
mySerial.begin(9600);
while(!Serial){ ; }
while(!mySerial){ ; }
delay(500);
mySerial.write(254); mySerial.write(128);
mySerial.write(" "); // clear display;
mySerial.write(" ");
mySerial.write(254); // move cursor to beginning of first line
mySerial.write(128);
//mySerial.write("welcome");
}
void loop() {
dataValue = dataRead();
Serial.print(dataValue);
// 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){
//count=count+1; Serial.print("count=");Serial.println(char(count));delay(200);
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"); mySerial.write("Masafi");count=count+1; Serial.print("count=");Serial.println(count);delay(200);}
if (check[0] ==tag2[0] && check[1] == tag2[1] && check[2] ==tag2[2] && check[3] ==tag2[3] && check[4] ==tag2[4] )
{Serial.println("\tAlain");mySerial.write("Alain");count=count+1; Serial.print("count=");Serial.println(count);delay(200); }
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");mySerial.write("Mai Dubai");count=count+1; Serial.print("count=");Serial.println(count);}
if (check[0] ==tag4[0] && check[1] == tag4[1] && check[2] ==tag4[2] && check[3] ==tag4[3] && check[4] ==tag4[4] )
{Serial.println("\tCocacola");mySerial.write("Cocacola");count=count+1; Serial.print("count=");Serial.println(count);}
if (check[0] ==tag5[0] && check[1] == tag5[1] && check[2] ==tag5[2] && check[3] ==tag5[3] && check[4] ==tag5[4])
{Serial.println("\t7UP");mySerial.write("7UP");count=count+1; Serial.print("count=");Serial.println(count);}
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");mySerial.write("Al Rabie");count=count+1;Serial.print("count=");Serial.println(count);delay(200); }
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");mySerial.write("Cocunut Water");count=count+1;Serial.write("count="); Serial.write(count);delay(200); }
//Serial.print(" [count=]");
//count = count+1;
Serial.println(count);
mySerial.write(count);
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;
}