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:
-
Upon uploading and successful signal connection to the Barcode scanner, the linear actuator will fully extend and fully retract.
-
I have the scanner in continuous scanning mode, and, upon scanning a random code, the linear actuator extends and retracts, as its supposed to!
-
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.
-
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 = "";
}
}
}