Counting

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;                                                            
}

Attached is scan of the serial monitor.

Run2.pdf (179 KB)

for (int i=0; i <= 10; i++){ Serial.print(check[i]);  delay(10); }

Why are you trying to print eleven characters from a ten character buffer?

Your code is waaaay too long - are you being paid by the LOC?

This is not the cause of the problem.
Here I wanted to check that I am saving the scanned code correctly.
I will improve the code, once I complete the main functions.

Why not improve it now?

Why do you use Serial.write in some places, and Serial print in others?
Why does your code wander over the page?

Serial.print() >>>>> to print the values in the serial monitor
Serial.Write() >>>> To print the data to the serial LCD. This is the command they used in the LCD manual.

Any luck in identifying why count variable is not incremented correctly ?

AWOL:
Why not improve it now?

Why do you use Serial.write in some places, and Serial print in others?
Why does your code wander over the page?

I am more concern with the counting function.

FutureEngineer:
Serial.print() >>>>> to print the values in the serial monitor
Serial.Write() >>>> To print the data to the serial LCD. This is the command they used in the LCD manual.

That don't make no sense.
Read your code again.

(Bugs like that can hide in plain view, when code is longer than it needs to be)

FutureEngineer:
I am more concern with the counting function.

I am more concerned with getting you to think and act like a future engineer.

Kindly refer to the LCD tutorial linked below.

https://www.sparkfun.com/tutorials/246

Even without mySerial.write command for the serial LCD, count is not incremented correctly.
I could not view the values on the serial monitor.

FutureEngineer:
Kindly refer to the LCD tutorial linked below.

https://www.sparkfun.com/tutorials/246

Kindly refer to your code, above.

I just run the code without mySerial.Write command. Results are attached.

///////////////// 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() {

  ////////////////////////////////// dataRead() function 
  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; 
dataValue = val; 
  //////////////////////////////
  
  
	//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: ");  
                 count = count+1; 
		// 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);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");count=count+1; Serial.print("count=");Serial.println(count);}
                 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");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");count=count+1; Serial.print("count=");Serial.println(count);delay(200);}
                 if (check[0] ==tag5[0]  && check[1] == tag5[1] && check[2] ==tag5[2]  && check[3] ==tag5[3] && check[4] ==tag5[4])
                    {Serial.println("\t7UP");count=count+1; Serial.print("count=");Serial.println(count);delay(200);}  
                 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");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");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;                                                         
}

Run3.pdf (205 KB)

AWOL:
Kindly refer to your code, above.

please be specific, which part in my code resulted in the problem ?

Serial.write("count=");"without Serial.write"?

Please, start paying attention.

I removed all mySerial.write commands but still same results. Kindly refer to the code below.

///////////////// serial LCD

//////////////
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() {

  ////////////////////////////////// dataRead() function 
  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; 
dataValue = val; 
  //////////////////////////////
  
  
	//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: ");  
                 count = count+1; 
		// 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);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");count=count+1; Serial.print("count=");Serial.println(count);}
                 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");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");count=count+1; Serial.print("count=");Serial.println(count);delay(200);}
                 if (check[0] ==tag5[0]  && check[1] == tag5[1] && check[2] ==tag5[2]  && check[3] ==tag5[3] && check[4] ==tag5[4])
                    {Serial.println("\t7UP");count=count+1; Serial.print("count=");Serial.println(count);delay(200);}  
                 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");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");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;                                                         
}

OK, it's clear now that you're simply not paying attention and have no interest in finding your problem, just wanting to be spoon-fed.

That's not my thing. I'm out.

AWOL:
OK, it's clear now that you're simply not paying attention and have no interest in finding your problem, just wanting to be spoon-fed.

That's not my thing. I'm out.

I honestly don't know what you are talking about.
Thanks any way.

FutureEngineer:
I honestly don't know what you are talking about.
Thanks any way.

When you tell me you've removed the Serial.writes from your code then post your code and I can see a Serial.write, then I know you're not paying attention.

Your code is too long.

AWOL:
OK, it's clear now that you're simply not paying attention and have no interest in finding your problem, just wanting to be spoon-fed.

That's not my thing. I'm out.

I just remove the one serial.write for the coconut water, but still counting problem is not solved.

///////////////// serial LCD

//////////////
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() {

  ////////////////////////////////// dataRead() function 
  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; 
dataValue = val; 
  //////////////////////////////
  
  
	//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: ");  
                 count = count+1; 
		// 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);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");count=count+1; Serial.print("count=");Serial.println(count);}
                 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");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");count=count+1; Serial.print("count=");Serial.println(count);delay(200);}
                 if (check[0] ==tag5[0]  && check[1] == tag5[1] && check[2] ==tag5[2]  && check[3] ==tag5[3] && check[4] ==tag5[4])
                    {Serial.println("\t7UP");count=count+1; Serial.print("count=");Serial.println(count);delay(200);}  
                 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");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");count=count+1;Serial.print("count="); Serial.print(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;                                                         
}