SparkFun Barcode Scanner & Linear Actuator

Hello everyone! I am new to this forum and find myself in need of some assistance for a project I am attempting at completing.

Project Background: I am attempting at creating an error detection system using Barcode inputs where the output is the motion of a linear actuator either extending or not. For this project I am using an Arduino Uno, a SparkFun Barcode Scanner, a standard motor driver, power source, and 12V linear actuator.

Although I am not the best at coding, I have managed to piece together a code compiled of both the supplied codes in the SparkFun library, as well as various forum discussions on this site. So far, this code does the following:

  1. Upon uploading and successful signal connection to the Barcode scanner, the linear actuator will fully extend and fully retract.

  2. I have the scanner in continuous scanning mode, and, upon scanning a random code, the linear actuator extends and retracts, as its supposed to!

  3. The main issue is that after the first barcode is scanned and I scan a different random barcode, the Arduino does not seem to recognize the new barcode value but instead, displays the same value as the first barcode.

  4. The second issue I have ran into is that, when scanning the control code (the barcode that is supposed to be recognized as correct, in this case "11212022") the actuator also extends, when it should in fact stay still as there is no error in that value.

The control value in this case will be a production date, so I have assigned it as "11212022" and have used a Barcode generator to generate a barcode of that exact value. The scanner will read the value, but will proceed with the wrong output.

I've attached my code to this topic and really hope someone can point me in the right direction. I feel as though I am on the right track but could really use some assistance.

#include "SoftwareSerial.h"
SoftwareSerial softSerial(2, 3); //RX, TX: Connect Arduino pin 2 to scanner TX pin. Connect Arduino pin 3 to scanner RX pin.

#include "SparkFun_DE2120_Arduino_Library.h" //Click here to get the library: http://librarymanager/All#SparkFun_DE2120
DE2120 scanner;

#define BUFFER_LEN 40
char scanBuffer[BUFFER_LEN];

byte Speed = 0; //Initialize Variable for the speed of the motor (0-255); 
int RPWM = 10; //connect Arduino pin 10 to IBT-2 pin RPWM
int LPWM = 11; //connect Arduino pin 11 to IBT-2 pin LPWM

String code1 = "11212022";
String curCode = "";

void setup()
{
  pinMode(10, OUTPUT); //Configure pin 10 as an Output 
  pinMode(11, OUTPUT); //Configure pin 11 as an Output 
  Serial.begin(9600);
  Serial.println("DE2120 Scanner Example");

  if (scanner.begin(softSerial) == false)
  {
    Serial.println("Scanner did not respond. Please check wiring. Did you scan the POR232 barcode? Freezing...");
    while (1)
      ;
  }
  Serial.println("Scanner online!");
}

void loop() 
{
  if (scanner.readBarcode(scanBuffer, BUFFER_LEN))
  {
    Serial.print("Code found: ");
    for (int i = 0; i < strlen(scanBuffer); i++)
    {
      Serial.print(scanBuffer[i]);
      Serial.println();//prints a blank line under value scanned
      curCode = curCode+scanBuffer[i];
    }
 
    if(curCode == code1)
    {
      analogWrite(10 ,0); //no output to either pin
      analogWrite(11, 0);
      curCode = "";
    }

    if(curCode != code1)
    { 
      Speed = 255;
      analogWrite(10, 0);
      analogWrite(11, Speed);//extend actuator at full speed
      delay(8000);//pause code from proceeding to retraction prior to full extension
      analogWrite(10, Speed);//retract actuator at full speed
      analogWrite(11,0);
      delay(8000);//pause code from proceeding prior to full retraction
      curCode = "";

    } 
  }
  

}

You shouldn't use the String class on AVR Arduinos.

The two ifs checking for code1 should be an if/else.

Are you sure the read barcode string is identical to the saved code1?

What class do you suggest I use in this case?

I had my second if loop as an else if and had the same outcome before.

I’ll be running more tests this afternoon, but I’m almost certain the codes were the same as I had a print command after reading the value of the barcode and it seemed to match the value on the barcode.

Show that complete experiment. The code as written is flawed exactly there at least, so.

      curCode = "";
    }

    if(curCode != code1)

If curCode matches, the if part gets done and curCode is reset.

So it certainly does not equal curCode and the second if block is also done.

I think. Looking through a very tiny window in transit.

a7

Use the standard <string.h> library to manipulate and compare C-strings (zero terminated character arrays). Strings cause program malfunctions on AVR-based Arduinos.

C type strings aka character arrays. There are C library helper functions to support this (e.g. strcmp). The library seems to return a C string already, otherwise the strlen() function wouldn't work.

A print doesn't show non-printable characters but the compare will check for those too.

I'll record my setup and testing for everyone to see. I'm a bit preoccupied at the moment but as soon as I can I will be uploading a video or posting a link so that everyone is clear. Thank you everyone for your inputs so far, I'll note all suggestions!

Okay everyone. I've gotten a video showing the setup as well as what the linear actuator does. Please use the link below to access the video:

Also, I forgot to record the output on the Serial Monitor after the second scan and have attached the screenshot below.

Notice that it does not print a barcode value the second time around. I will now try the various suggestions you all made and hopefully the adjustments help!

Any input is helpful! Thank you all!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.