barcode

This is a special forum devoted to educators using Adafruit and Arduino products for teaching.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
FutureEngineer
 
Posts: 54
Joined: Sat Dec 26, 2015 5:11 pm

barcode

Post by FutureEngineer »

Good morning

I need to interface the barcode reader linked below to Arduino.
I am using PS2 to breadboard connector also linked below.

https://www.adafruit.com/product/1202
https://www.adafruit.com/product/804

There can some issues that need clarifications:
Why there are two connectors used with the barcode reader (male and female)
Can you please refer to a startup tutorial with step by step guide on how to interface the barcode reader to Arduino.

Appreciate your early reply.
Thank you

User avatar
FutureEngineer
 
Posts: 54
Joined: Sat Dec 26, 2015 5:11 pm

Re: barcode

Post by FutureEngineer »

I connected the barcode reader as below.

The barcode terminals are listed below.
Brown is data
Green is VCC
Black is ground
Yellow is clock

I used this connection diagram
https://playground.arduino.cc/Component ... odeScanner

Attached is the connection.

Please reference an Arduino code for the scanner.
when I press on the button at the scanner, the flash light turns ON and OFF but there is no sound.

Appreciate your early reply
Thanks
Attachments
barcode_S.jpg
barcode_S.jpg (190.27 KiB) Viewed 659 times

User avatar
FutureEngineer
 
Posts: 54
Joined: Sat Dec 26, 2015 5:11 pm

Re: barcode

Post by FutureEngineer »

Good morning.

Any help is appreciated.

Thanks

User avatar
FutureEngineer
 
Posts: 54
Joined: Sat Dec 26, 2015 5:11 pm

Re: barcode

Post by FutureEngineer »

Dears,

I spent a lot of money on this barcode scanner, I actually purchased two of them.

And I need to make them work, please your technical support is urgently needed here.

Thanks.

User avatar
Franklin97355
 
Posts: 23939
Joined: Mon Apr 21, 2008 2:33 pm

Re: barcode

Post by Franklin97355 »

When you respond to your own post it takes it off the list of topics without responses and sometimes that slows down a response. I'll see if there is an answer to your question since I don't know that product.

User avatar
FutureEngineer
 
Posts: 54
Joined: Sat Dec 26, 2015 5:11 pm

Re: barcode

Post by FutureEngineer »

Kindly check the connector image to check each colored wire is correctly identified.
These are from the male connector of the barcode scanner.
They are connected as below:

Brown wire (pin 1 in the male connector) is data and it's connected to pin 2 in Arduino (through a 2.2k resistor).
Black wire (pin 3 in the male connector) is connected to ground.
Yellow wire (pin 5 in the male connector) is clock and it's connected to pin 3 in Arduino (through a 2.2k resistor)
Green wire (pin 4 in the male connector) is connected to VCC
The white and the red (pin 2 and 6 respectively) are not connected

Kindly check image 2 that shows the connection to Arduino.

I am using three resistors of 1k, 1k and 220 to get the 2.2kohms resistor.
Attachments
barcode1.pdf
(1.16 MiB) Downloaded 41 times
Last edited by FutureEngineer on Sun Dec 17, 2017 2:57 am, edited 1 time in total.

User avatar
FutureEngineer
 
Posts: 54
Joined: Sat Dec 26, 2015 5:11 pm

Re: barcode

Post by FutureEngineer »

Kindly check the code and the connection to Arduino mega attached.

Code: Select all

/*
Barcode Scanner                                                        
	This code reads the input from a ps/2 keyboard or keyboard-like        
	device (e.g. a barcode scanner), translates the scan-codes into        
	numbers (only numbers from 0 to 9 can be used at the moment)           
	It is nowhere near a complete implementation of the ps/2 protocol,     
	but it should give you a starting point.                               
	mys .// Benjamin Maus	( benjamin.maus <at> allesblinkt.com )          
	2007                                                                   
*/

int SCAN_ENTER = 0x5a; int SCAN_BREAK = 0xf0;
int breakActive = 0;
int clockPin = 3; // Clock is only output. 
int dataPin = 2; // The data pin is bi-directional
				// But at the moment we are only interested in receiving   
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) 
int bufferPos = 0; 
int bufferLength = 64;


void setup() {
	pinMode(dataPin, INPUT);                                               
	pinMode(clockPin, INPUT);                                              
	pinMode(ledPin, OUTPUT);                                               
	Serial.begin(9600);                                                    
}

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] );                                       
				buffer[i] = 0;                                                   
				i++;                                                             
			}                                                                  
		}                                                                    
		Serial.println(" [Enter]");                                          
		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;                                                            
}
Attachments
barcode2.pdf
(950.44 KiB) Downloaded 39 times

User avatar
FutureEngineer
 
Posts: 54
Joined: Sat Dec 26, 2015 5:11 pm

Re: barcode

Post by FutureEngineer »

Now it's working. I was not careful of the distance between the reader and the barcode.

: )

User avatar
FutureEngineer
 
Posts: 54
Joined: Sat Dec 26, 2015 5:11 pm

Re: barcode

Post by FutureEngineer »

Another technical question I have,

how can I activate the barcode reader using an external push button rather than the built-in of the reader ?

User avatar
adafruit2
 
Posts: 22193
Joined: Fri Mar 11, 2005 7:36 pm

Re: barcode

Post by adafruit2 »

you'd have to carefully solder two wires to the built-in switch, then you can attach your own button!

User avatar
FutureEngineer
 
Posts: 54
Joined: Sat Dec 26, 2015 5:11 pm

Re: barcode

Post by FutureEngineer »

Can I activate it using a command from Arduino ?

User avatar
FutureEngineer
 
Posts: 54
Joined: Sat Dec 26, 2015 5:11 pm

Re: barcode

Post by FutureEngineer »

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 ?

Code: Select all

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]");   

		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;                                                            
}
Attachments
run.pdf
(237.06 KiB) Downloaded 32 times

User avatar
FutureEngineer
 
Posts: 54
Joined: Sat Dec 26, 2015 5:11 pm

Re: barcode

Post by FutureEngineer »

Why sometimes the scanner sound the beep but I don't get any thing at the serial monitor?

Please I need the reply.

User avatar
adafruit2
 
Posts: 22193
Joined: Fri Mar 11, 2005 7:36 pm

Re: barcode

Post by adafruit2 »

sounds like something with your code crashed, go back to the working code you had before!

User avatar
FutureEngineer
 
Posts: 54
Joined: Sat Dec 26, 2015 5:11 pm

Re: barcode

Post by FutureEngineer »

Now it's detecting correctly, but I need to do a simple count, but the program is not counting.

I am not sure why?? I spent three hours trying to fix the count problem without any success.

Code: Select all

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

Locked
Please be positive and constructive with your questions and comments.

Return to “For Educators”