Pages: [1] 2   Go Down
Author Topic: input from barcode  (Read 1356 times)
0 Members and 1 Guest are viewing this topic.
Barnsley / uk
Offline Offline
Full Member
***
Karma: 0
Posts: 116
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I should know how to do this but I have not played with my arduinos for some time and I seam to have forgot….. Well on with the question !!!!! …. I have a barcode scanner hooked up to my arduino and with this sketch it reads barcode’s and prints the value to the screen . How do I get it to recognise a barcode that  I have already put in the code
Eg   if barcode reads    1234567  Serial.println" barcode in database"
Code:
/*
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;                                                            
}
thanke
« Last Edit: November 26, 2012, 06:12:16 pm by maddave » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 178
Posts: 8064
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How do I get it to recognise a barcode that  I have already put in the code
Eg   if barcode reads    1234567  Serial.println" barcode in database"

You would put the barcodes to be recognized into a list and compare the barcode received to each of the ones in the list.  If you find a match, print "barcode in database".
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Barnsley / uk
Offline Offline
Full Member
***
Karma: 0
Posts: 116
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks john but how do i compaire them ... in other words how do i do it

somthing like 

if (dataRead () = 1234567){Serial.println(" [Barcode in database]")}:
« Last Edit: November 26, 2012, 06:13:58 pm by maddave » Logged

Barnsley / uk
Offline Offline
Full Member
***
Karma: 0
Posts: 116
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


if (dataRead () = 1234567){Serial.println(" [Barcode in database]")}:


the problem is what do i replace dataRead () with to make it work
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 547
Posts: 45993
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The dataRead() function gets one value, which is then used to determine what to store in buffer.

Once dataRead() returns a SCAN_ENTER, it is the value in buffer that needs to be compared to values in a list. Since buffer is a char array, and is NULL terminated, use strcmp() to compare buffer to known strings.
Logged

Barnsley / uk
Offline Offline
Full Member
***
Karma: 0
Posts: 116
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
use strcmp() to compare buffer

sorry but how do i do this....This is the first time i have seen " stcmp() " i have tried looking it up but have had little success


thanks
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 547
Posts: 45993
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
char *goodCards = {"12345", "Jones", "Smith", "Stolen" };

int cardCnt = sizeof(goodCards)/sizeof(goodCards[0]);

int goodMatch = -1;

for(int c=0; c<cardCnt; c++)
{
   if(strcmp(buffer, goodCards[c]) == 0)
   {
      goodMatch = c;
      break;
   }
}

Serial.print("goodMatch  = ");
Serial.println(goodMatch);     // Will print index of good card in array
Logged

Barnsley / uk
Offline Offline
Full Member
***
Karma: 0
Posts: 116
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

sorry i just dont get it .... at all !!!!!
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 547
Posts: 45993
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
sorry i just dont get it
OK. Not too worry.

Where have you defined the names/values of the barcodes that you want to check the scanned on against? That is the (global) array that I called goodCards. If you have another name, we can use that instead.

Logged

Barnsley / uk
Offline Offline
Full Member
***
Karma: 0
Posts: 116
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

if i understand you your asking for a list of coodcard bar code's

if so here they are

3455192327218 card 1
4005209057240 card 2
5060048310337 card 3

thanks for your time
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 547
Posts: 45993
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
if i understand you your asking for a list of coodcard bar code's
I was asking if you had created an array of such values in your code. If not, you can modify goodCards and use those values:
char *goodCards = { "3455192327218", "4005209057240", "5060048310337" };

After this:
Code:
if(dataValue == SCAN_ENTER){
Serial.print("\nbuffer: ");

Put this:
Code:
int goodMatch = -1;

for(int c=0; c<cardCnt; c++)
{
   if(strcmp(buffer, goodCards[c]) == 0)
   {
      goodMatch = c;
      break;
   }
}

Serial.print("goodMatch  = ");
Serial.println(goodMatch);     // Will print index of good card in array
Logged

Barnsley / uk
Offline Offline
Full Member
***
Karma: 0
Posts: 116
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thnaks for all you help but this is what i have and if is not working



Quote
de 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                                                                   
*/
char *goodCards = { "3455192327218", "4005209057240", "5060048310337" };
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;


After this:


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;                                           
      if(temp == dataValue){                                               
         if(!breakActive == 1){                                             
            buffer[bufferPos] = characters;                               
            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 goodMatch = -1;

for(int c=0; c<cardCnt; c++)
{
   if(strcmp(buffer, goodCards[c]) == 0)
   {
      goodMatch = c;
      break;
   }
}

Serial.print("goodMatch  = ");
Serial.println(goodMatch);     // Will print index of good card in array      

                int i=0;                                                                               
      if (buffer != 0) {                                               
         while(buffer != 0) {                                           
            Serial.print( buffer );                                       
            buffer = 0;                                                   
            i++;                                                             
         }                                                                 
      }                                                                   
      Serial.println(" [Enter]");                                         
      bufferPos = 0;                                                       
      // Blink the LED                                                      
      digitalWrite(ledPin, HIGH);                                         
      delay(300);                                                         
      digitalWrite(ledPin, LOW);                                           
   }                                                                     
   int goodMatch = -1;

for(int c=0; c<cardCnt; c++)
{
   if(strcmp(buffer, goodCards[c]) == 0)
   {
      goodMatch = c;
      break;
   }
}

Serial.print("goodMatch  = ");
Serial.println(goodMatch);     // Will print index of good card in array// 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;                                                           
}
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 547
Posts: 45993
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What does your serial output look like?

The code IS working. It just isn't producing the results you are expecting, but, right now, we don't know what results it produces, or what you are expecting.

Please use the # icon, and code tags, not the quote icon and quote tags, when posting code, so the forum software doesn't try to interpret stuff in your code as fonting information.
Logged

Barnsley / uk
Offline Offline
Full Member
***
Karma: 0
Posts: 116
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
/*
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                                                                   
*/
char *goodCards = { "3455192327218", "4005209057240", "5060048310337" };
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;


After this:


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 goodMatch = -1;

for(int c=0; c<cardCnt; c++)
{
   if(strcmp(buffer, goodCards[c]) == 0)
   {
      goodMatch = c;
      break;
   }
}

Serial.print("goodMatch  = ");
Serial.println(goodMatch);     // Will print index of good card in array

                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);                                           
}                                                                     
int goodMatch = -1;

for(int c=0; c<cardCnt; c++)
{
   if(strcmp(buffer, goodCards[c]) == 0)
   {
      goodMatch = c;
      break;
   }
}

Serial.print("goodMatch  = ");
Serial.println(goodMatch);     // Will print index of good card in array// 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;                                                           
}
Logged

Barnsley / uk
Offline Offline
Full Member
***
Karma: 0
Posts: 116
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

will not complile
Logged

Pages: [1] 2   Go Up
Jump to: