I am currently working on a little project to build system who can scan barcodes when an item is placed close to the barcode reader. For this I am using a basic presence sensor (HC-SR04) and a Barcode Scanner module from Waveshare (https://www.waveshare.com/barcode-scanner-module.htm).
What I am trying to get is that:
When no item is close to the sensor: led if off and lcd screen says "Hello, put pouch under scanner" --> so far this works well
When an item is close to the sensor (<5cm): (i) led is ON, (ii) scanner starts scanning, and (iii) lcd shows "Scanning..." --> so far this also works
When scanner has scanned the barcode of the item close to the sensor and scanner: (i) led remains ON (this works), (ii) lcd shows "product code is ". This last part is where I have a problem. Either the LCD remains on "Scanning..." or it shows some very unreadable strange signs (only readable signs is number 31, don't ask me why 31...).
Can someone please help me out?
Here is my source code:
// Barcode Reader
#include <SoftwareSerial.h>
SoftwareSerial ScannerSerial(0, 1); // RX, TX //Serial Communication for the barcode scanner
int strglength = 0;
int oldstrglength = 0;
long productcode;
String scannerString = "";
bool scannerComplete = false;
// Lcd screen
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Presence Sensor Led
const int presenceLedpin = 8;
// Presence sensor
#define echoPin 7 // attach pin D7 Arduino to pin Echo of HC-SR04
#define trigPin 6 //attach pin D6 Arduino to pin Trig of HC-SR04
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement
int presenceState = 0;
int oldpresenceState = 0;
void setup() {
// Barcode Reader
Serial.begin(9600);
ScannerSerial.begin(9600);
// Presence Led
pinMode(presenceLedpin, OUTPUT);
// Presence Sensor
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
// Lcd screen
lcd.begin(16,2);
lcd.print ("Hello, put pouch ");
lcd.setCursor(0,1);
lcd.print("under scanner ");
}
void loop() {
// Presence sensor
digitalWrite(trigPin, LOW); //Clear the trigPin condition of the presence sensor
delayMicroseconds(2);
digitalWrite(trigPin, HIGH); //Sets the trigPin HIGH (presence sensor ACTIVE)
delayMicroseconds(10); //Wait for 10 seconds
digitalWrite(trigPin, LOW); //Turns presence sensor off again to mesure distance
duration = pulseIn(echoPin, HIGH); //Reads the echoPin, returns the sound wave travel time in microseconds
distance = duration * 0.034 / 2; //Calculating the distance
if (distance < 5) { //if there is an object close to the sensor
// Presence Led
digitalWrite(presenceLedpin, HIGH); //turn the presence sensor led ON
// Presence Sensor
presenceState = 1;
if (oldpresenceState != presenceState){ //if a pouch was detected but last loop there was no pouch detected
// Lcd screen
lcd.begin(16,2); // Write message on lcd screen
lcd.print ("Scanning... ");
lcd.setCursor(0,1);
lcd.print(" ");
oldpresenceState = presenceState;
}
// Barcode Reader
Serial.write(0X7E); // Command scanner to start scanning
Serial.write((byte)0X00);
Serial.write(0X08);
Serial.write(0X01);
Serial.write((byte)0X00);
Serial.write(0X02);
Serial.write(0X01);
Serial.write(0XAB);
Serial.write(0XCD);
delay (5);
oldstrglength = scannerString.length();
if (ScannerSerial.available()) {
char c = (char)ScannerSerial.read();
scannerString += c;
}
strglength = scannerString.length();
if (strglength == oldstrglength && strglength != 0) {
// Barcode Reader
scannerComplete = true;
}
if (scannerComplete) {
// Barcode Reader
productcode = scannerString.toInt();
// lcd screen
lcd.begin(16,2); // Write message on lcd screen
lcd.print ("Product code is: ");
lcd.setCursor(0,1);
lcd.print(scannerString);
// Barcode Reader
scannerString = ""; // clear the string:
scannerComplete = false;
}
delay(5);
}
else {
// Presence Led
digitalWrite(presenceLedpin, LOW);
presenceState = 0;
if (oldpresenceState != presenceState){ //if a pouch was detected during last loop but not in this one
// Lcd screen
lcd.begin(16,2);
lcd.print ("Hello, put pouch ");
lcd.setCursor(0,1);
lcd.print("under scanner ");
oldpresenceState = presenceState;
}
}
}
Well, to simplify things I created a new code in which I used AltSoftSerial i.o. ScannerSerial and replaced the presence sensor by a button. I also took away the lcd screen and just use the Serial Monitor for displaying results.
To activate the sensor and light on the LED, the button needs to be pushed in.
The communication seems indeed much more stable but I don't get any data back from my barcode scanner now. In my serial monitor there is just "Scanning..." showing up once I am pressing the button.
Here is the simplified code
// Barcode Reader
#include <AltSoftSerial.h>
AltSoftSerial ScannerSerial; //Serial Communication for the barcode scanner
int strglength = 0;
int oldstrglength = 0;
long productcode;
String scannerString = "";
bool scannerComplete = false;
// Scanner Button
int switchState = 0;
int oldswitchState = 0;
const int switchPin = 2;
// Switch Led
const int switchLedpin = 7;
void setup() {
// Serial Monitor
Serial.begin(9600);
// Barcode Reader
ScannerSerial.begin(9600);
// Button Led
pinMode(switchLedpin, OUTPUT);
// Scanner Button
pinMode(switchPin, INPUT);
}
void loop() {
// Scanner Button
switchState = digitalRead(switchPin);
if (switchState == HIGH) { //if button is pushed
// Switch Led
digitalWrite(switchLedpin, HIGH); //turn the presence sensor led ON
if (oldswitchState != switchState) {
// Serial monitor
Serial.println("Scanning...");
oldswitchState = switchState;
}
// Barcode Reader
ScannerSerial.write(0X7E); // Command scanner to start scanning
ScannerSerial.write((byte)0X00);
ScannerSerial.write(0X08);
ScannerSerial.write(0X01);
ScannerSerial.write((byte)0X00);
ScannerSerial.write(0X02);
ScannerSerial.write(0X01);
ScannerSerial.write(0XAB);
ScannerSerial.write(0XCD);
delay (5);
oldstrglength = scannerString.length();
if (ScannerSerial.available()) {
char c = (char)ScannerSerial.read();
scannerString += c;
}
strglength = scannerString.length();
if (strglength == oldstrglength && strglength != 0) {
scannerComplete = true;
}
if (scannerComplete) {
productcode = scannerString.toInt();
Serial.print("Product Code: ");
Serial.println(scannerString);
scannerString = ""; // clear the string:
scannerComplete = false;
}
delay(5);
}
else {
// Switch Led
digitalWrite(switchLedpin, LOW); //turn the presence sensor led OFF
oldswitchState = switchState;
// Serial monitor
// Serial.print("Scanner off");
}
}
Your Scanner input logic if not correct. The first time through oldstrglength is 0. If you receive a char, then strglength will never equal oldstrglength so scannerComplete will not be set to true. If you do not receive a char, the string lengths will match but will be 0 so you still will not set scannerComplete to true.
Why would strglength never be equal oldstrglength if I receive a char? (sorry I am still quite new to Arduino).
What I am trying to do here is that the barcode reader only starts when the button is pushed.
Once it is on, it reads and once it has read a barcode, the serial monitor displays the code.
If I release the pouch button just after I have scanned the barcode, nothing is showing on the serial monitor. However, if I keep it pushed on for ~6-7 seconds after having scanned the barcode, I get the following printed in the serial monitor:
Product Code: 13131333131313131313131313131313131113313133111313131311333131133111313131313131313113313131331131313131131313131333131313131313111333313131133318058333490090
3133311313131313133111331313311333313131113331131313311313131313311313133118058333490090
31333313131313311133131311331313131331133131113331313131313131313331113131113131313133331'''%$& "$(2>>BHKH6OKZTS^prqivhdiRE&#+>.M0ISaGi⸮iZi⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮))#$!"(&08>B;PECNTRCRZf{hej⸮ee=I1$6?<BNJ3NKN[\V\m⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮%&%!%%*1?OBH[2gVUXVSW⸮9nkqelM+?9156>IH6<IM;E=Uag⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮')#$'(3CHG@311311331313131
I don't understand why I am getting so much information while I am only expecting the barcode number, which is 8058333490090. Where is all this noise information coming from???
becuase oldstrglength is equal to 0 and after receiving 1 char, strglength will be equal to 1 and 0 != 1.
Do you always receive a fixed number of chars with your barcode? Or does it always end with a carriage return '\r' or linefeed '\n' or both? You need to have some sort of logic in your code to determine when the "string" is complete so you know when to process it and reset the input back to empty. You could also do it based on time if you record the time of the last char input and timeout after several seconds. I'm guessing that may be a more advanced approach given your newness to all this. Not that you can't learn...