ps2 barcode scanner using instantly

I want to run it when i want.

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

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.

PaulS:

I want to run it when i want.

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

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?
:slight_smile:
The code is this Arduino Playground - HomePage
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

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

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:

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

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.

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.

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

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.

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

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

Try here first, paying attention to section 6.

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

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

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 ?

Ok very thanks and many sorry to all. Here's coming the full code.

#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.

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.

String inputString = "";         // a string to hold incoming data

This would too. Strings have major problems on the Arduino, and are a crutch, anyway.

  // 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!

  // rebuild the picture after some delay

Comments after the code look pretty stupid. Comments BEFORE the code are a good idea.

//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.

How
does
number
get
to
be
five?

In the serialEvent() function maybe ?
Does the capitalisation of the function name matter ?

//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.

In the serialEvent() function maybe ?

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

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?

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

Without lcd and u8g libraly, everything goes well.

You then need to include the libraries, and create the instances, but don't use them. Does the code still work?

Add parts back in until the code quits working correctly. Then, let us know what specific line of code/function call causes the program to quit.

Have you checked how much free memory you have?

ok i will work on it.

PaulS:

Does the capitalisation of the function name matter ?

Of course it does.

In my heart of hearts I knew that, but it would be less confusing if the tutorial on the function http://arduino.cc/en/Tutorial/SerialEvent was consistent as to what it called the function. Admittedly it is called serialEvent in the example programs but it is referred to as SerialEvent in several places in the text and comments, not least at the very start of the tutorial.

SerialEvent
Demonstrates use of the SerialEvent() function. SerialEvent() is called after a loop(), if there is serial data in the buffer.

Post this issue in the Website and Forum section. Of late, madbob has been very good at fixing issues in the reference documentation.

Will do