Offline
Newbie
Karma: 0
Posts: 26
|
 |
« on: February 10, 2013, 09:26:09 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #1 on: February 10, 2013, 09:34:02 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 26
|
 |
« Reply #2 on: February 11, 2013, 02:16:14 am » |
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?  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 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; } }
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Sr. Member
Karma: 9
Posts: 351
|
 |
« Reply #3 on: February 11, 2013, 02:23:34 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 26
|
 |
« Reply #4 on: February 11, 2013, 02:54:20 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 46
Posts: 1380
May all of your blinks be without delay
|
 |
« Reply #5 on: February 11, 2013, 04:03:18 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 26
|
 |
« Reply #6 on: February 11, 2013, 05:00:09 am » |
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
|
|
|
|
« Last Edit: February 11, 2013, 05:02:07 am by feveran »
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 136
Posts: 18991
I don't think you connected the grounds, Dave.
|
 |
« Reply #7 on: February 11, 2013, 05:03:50 am » |
Try here first, paying attention to section 6.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 46
Posts: 1380
May all of your blinks be without delay
|
 |
« Reply #8 on: February 11, 2013, 05:15:04 am » |
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 ?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 26
|
 |
« Reply #9 on: February 12, 2013, 04:39:52 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #10 on: February 12, 2013, 06:31:29 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 46
Posts: 1380
May all of your blinks be without delay
|
 |
« Reply #11 on: February 12, 2013, 06:40:07 am » |
How does number get to be five?
In the serialEvent() function maybe ? Does the capitalisation of the function name matter ?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 26
|
 |
« Reply #12 on: February 12, 2013, 07:11:23 am » |
//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.
|
|
|
|
« Last Edit: February 12, 2013, 07:13:41 am by feveran »
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #13 on: February 12, 2013, 07:37:55 am » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 26
|
 |
« Reply #14 on: February 12, 2013, 07:47:23 am » |
Without lcd and u8g libraly, everything goes well. i can read valid barcode number from serial. i think problem can depend on "picture loop".
|
|
|
|
|
Logged
|
|
|
|
|
|