Go Down

Topic: ps2 barcode scanner using instantly (Read 1 time) previous topic - next topic

feveran

Hi there, i am running this playground well http://playground.arduino.cc/ComponentLib/BarcodeScanner but i want make one change.
In the code as you see,  dataRead();  command always running in loop() .  I want to run it when i want. But it doesn't work.

PaulS

Quote
I want to run it when i want.

And how is the Arduino supposed to know when that is?

Quote
But it doesn't work.

We've hired a psychic who will be able to determine what the code actually does without seeing it, and how that differs from what you want, without you having to describe that.

His start date is the 12th of never. If you need help before then, there are some things you need to do.

feveran


Quote
I want to run it when i want.

And how is the Arduino supposed to know when that is?

Quote
But it doesn't work.



We've hired a psychic who will be able to determine what the code actually does without seeing it, and how that differs from what you want, without you having to describe that.

His start date is the 12th of never. If you need help before then, there are some things you need to do.


Here this point is not important maybe? Why not when i click a button wired to one of digital pins?
:)
The code is this http://playground.arduino.cc/uploads/ComponentLib/arduino-barcode-code.txt
I just want to make run what have written in loop function  (i.e. when i click a button) in the code above.
I tried but it didn't work.

Here the code in the loop
Code: [Select]

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

and dataRead() function
Code: [Select]

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


Now as you see, program always reading scanner data. But i want to check it like this way:
Code: [Select]
void loop() {
.
.
.
.
if(number==5)
{
checkBarcode();
}
.
.
.
.
}
void checkBarcode(){

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

tack

So check it that way then. What's the problem exactly? It looks to me like you've just posted the solution to your own original question.

feveran


So check it that way then. What's the problem exactly? It looks to me like you've just posted the solution to your own original question.

no, i don't know where is the problem but code not working in this way.

UKHeliBob

If you have written the code that reads a barcode when a button is pressed but it does not work please post it here with a description of what it does.  In your skeleton code explaining what you want to do we do not know where the value of number comes from.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

feveran

#6
Feb 11, 2013, 11:00 am Last Edit: Feb 11, 2013, 11:02 am by feveran Reason: 1
I am using u8glib libraly with one 128x64 graphic lcd. This libraly working like this code in the loop()
Code: [Select]

void loop(void) {
 // picture loop
 u8g.firstPage();  
 do {
   draw();
 } while( u8g.nextPage() );
 
 // rebuild the picture after some delay
 delay(100);

//run barcode scanner
if(number==5)
{
checkBarcode();
checked=true;
}

}

void draw(void) {
 // graphic commands to redraw the complete screen should be placed here  
 u8g.setFont(u8g_font_unifont);
 u8g.drawStr( 0, 20, "Hello World!");
//if barcode scanner had checked write ok.
 if(checked)
 u8g.drawStr( 0, 40, "ok checked");
}
.
.
.


In my setup, screen freezing when number=5

AWOL

Try here first, paying attention to section 6.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

UKHeliBob


I am using u8glib libraly with one 128x64 graphic lcd. This libraly working like this code in the loop()
Code: [Select]

void loop(void) {
 // picture loop
 u8g.firstPage();  
 do {
   draw();
 } while( u8g.nextPage() );
 
 // rebuild the picture after some delay
 delay(100);

//run barcode scanner
if(number==5)
{
checkBarcode();
checked=true;
}

}

void draw(void) {
 // graphic commands to redraw the complete screen should be placed here  
 u8g.setFont(u8g_font_unifont);
 u8g.drawStr( 0, 20, "Hello World!");
//if barcode scanner had checked write ok.
 if(checked)
 u8g.drawStr( 0, 40, "ok checked");
}
.
.
.


In my setup, screen freezing when number=5

I try to be polite here, but which part of
Quote

If you have written the code that reads a barcode when a button is pressed but it does not work please post it here with a description of what it does.  In your skeleton code explaining what you want to do we do not know where the value of number comes from.
did you not understand ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

feveran

Ok very thanks and many sorry to all. Here's coming the full code.
Code: [Select]

#include "U8glib.h"
U8GLIB_ST7920_128X64 u8g(9, 8, 3, U8G_PIN_NONE);       
int barkod[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int SCAN_ENTER = 0x5a; int SCAN_BREAK = 0xf0;
int breakActive = 0;
int clockPin = 22; //
int dataPin = 19; //


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;
bool barbool=false;

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete
int number=0;
void setup(void) {
    // initialize serial:
  Serial.begin(9600);
  // reserve 200 bytes for the inputString:
  inputString.reserve(200);
 
             pinMode(dataPin, INPUT);                                               
pinMode(clockPin, INPUT);       
   
}
  void loop(void) {
  // picture loop
  u8g.firstPage(); 
  do {
    draw();
  } while( u8g.nextPage() );
 
  // rebuild the picture after some delay
 

//run barcode scanner
if(number==5)
{
checkBarcode();
//checked=true;
}

}

void checkBarcode(){
  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++;                                                             

barbool=true;
}                                                                   
Serial.println(" [Enter]");                                         
bufferPos = 0;                                                       
// Blink the LED                                                    
;                                           
}                                                                     
// Reset the SCAN_BREAK state if the byte was a normal one             
if(dataValue != SCAN_BREAK){                                           
breakActive = 0;                                                     
}                                                                     
dataValue = 0;                     
}
void draw(void) {
  // graphic commands to redraw the complete screen should be placed here 
  u8g.setFont(u8g_font_unifont);
  u8g.drawStr( 0, 20, "Hello World!");
//if barcode scanner had checked write ok.
  if(barbool)
  u8g.drawStr( 0, 40, "ok checked");
}

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;                                                           
}
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == 'a') {
      number = 5;
    }
        if (inChar == 'b') {
      number = 1;
    }
    Serial.println(number);
  }
}



And the problem is this, when i send 'a' to the arduino (so number being set to 5, i can see it via serial port viewer)  screen freeze. When screen frozen if i use barcode scanner and make it scan a valid barcode code, screen blinks for 0.5 second (not exactly) then be frozen again. 

PaulS

Code: [Select]
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'};

If you worked for me, this would get you some time off. ONE statement per line!

Use the space bar to make the code readable. Also feel free to break that array initialization code onto more than one line. 2 groups of 5 is much easier to count than one group of 10.

Code: [Select]
String inputString = "";         // a string to hold incoming data
This would too. Strings have major problems on the Arduino, and are a crutch, anyway.

Code: [Select]
  // reserve 200 bytes for the inputString:
  inputString.reserve(200);

This part says that you have an idea how big the static array should be. Go with that!

Code: [Select]
  // rebuild the picture after some delay
Comments after the code look pretty stupid. Comments BEFORE the code are a good idea.

Code: [Select]
//run barcode scanner
if(number==5)
{
checkBarcode();
//checked=true;
}

Why
only
when
number
is
five?

What's
magic
about
five?

How
does
number
get
to
be
five?

I give up on the rest of that mess. The indentation is horrid. Put each { on a new line, and use Tools + Auto Format AND some comments if you really want help.

UKHeliBob

Quote
How
does
number
get
to
be
five?

In the serialEvent() function maybe ?
Does the capitalisation of the function name matter ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

feveran

#12
Feb 12, 2013, 01:11 pm Last Edit: Feb 12, 2013, 01:13 pm by feveran Reason: 1
Code: [Select]

//libraly for lcd screen
#include "U8glib.h"
U8GLIB_ST7920_128X64 u8g(9, 8, 3, U8G_PIN_NONE);  
//variables for barcod scanner  
int SCAN_ENTER = 0x5a;
int SCAN_BREAK = 0xf0;
int breakActive = 0;
int clockPin = 22; //
int dataPin = 19; //
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;
// This saves the characters (for now only numbers)
char buffer[64] = {
};
int bufferPos = 0;
int bufferLength = 64;
//a state shows some job has done by barcode scanner
bool barbool=false;
// a string to hold incoming data from serial
String inputString = "";      
// whether the string is complete
boolean stringComplete = false;  
// a stupid number
int number=0;

void setup(void) {
 // initialize serial:
 Serial.begin(9600);
 // reserve some bytes for the inputString:
 inputString.reserve(50);
 //barcode scanning pins
 pinMode(dataPin, INPUT);                                              
 pinMode(clockPin, INPUT);      

}
void loop(void) {
 // picture loop for U8glib lcd libraly
 u8g.firstPage();  
 do {
//shows draw2() function on the screen if barcode scanner running
   if(barbool)
   {
     draw2();
   }
   else{
   //shows draw() function on the screen
     draw();
   }
 }
 while( u8g.nextPage() );
 //run barcode scanner if number set to 5
 if(number==5)
 {
   checkBarcode();
 }

}

void checkBarcode(){
 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++;                                                    
     }                                                                  
   }                                                                    
 }                                                                                        
 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++;                                                            
     }  

     barbool=true;
   }                                                                    
   Serial.println(" [Enter]");                                          
   bufferPos = 0;                                                      
   // Blink the LED                                                  
   ;                                          
 }                                                                      
 // Reset the SCAN_BREAK state if the byte was a normal one            
 if(dataValue != SCAN_BREAK){                                          
   breakActive = 0;                                                    
 }                                                                      
 dataValue = 0;                      
}
void draw(void) {
 // graphic commands to redraw the complete screen should be placed here  
 u8g.setFont(u8g_font_unifont);
 u8g.drawStr( 0, 20, "Hello World!");
 //if barcode scanner had checked write ok.
 if(barbool){
   u8g.drawStr( 0, 40, "ok checked");
 }
}

void draw2() {
 u8g.setFont(u8g_font_unifont);
 u8g.drawStr( 0, 20, "ok checked");
}

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;                                                            
}
void serialEvent() {
 while (Serial.available()) {
   // get the new byte:
   char inChar = (char)Serial.read();
   // add it to the inputString:
   inputString += inChar;
   // if the incoming character is a newline, set a flag
   // so the main loop can do something about it:
   //if user send 'a' command, set my lovely number 5
   if (inChar == 'a') {
     number = 5;
     Serial.println(number);
   }
   if (inChar == 'b') {
     number = 1;
     Serial.println(number);
   }

 }
}

five is my lovely number. now its auto-formatted.
now what i realised, draw2() never run. when number set 5, program get freeze. while screen frozen, if i scan a valid barcode with barcode scanner (it beep when read) Serial.println() works (prints 5) but draw2() doesn't work.

PaulS

Quote
In the serialEvent() function maybe ?

The point was that there should be a comment in the code explaining the magic number.

Quote
Does the capitalisation of the function name matter ?

Of course it does.

OP: What happens if you get rid of all the code to draw to the LCD, and just print to the serial port?

feveran

Without lcd and u8g libraly, everything goes well. i can read valid barcode number from serial. i think problem can depend on   "picture loop".

Go Up