HELP [How can I link barcode scanner to Arduino UNO to activate servo and LED?]

Hi All

I am relatively new to Arduino and I am using this for my final year project on a Visitor Control Access System.

I manage to make 2 separate coding works:

  1. Barcode Scanner
  2. Control Access System with Servo, push buttons and Red/Green LEDs (When green LED is ON, servo opens, ultrasonic sensor will sense for any obstruction before Green LED goes off and servo closes)

I am trying to combine both of the 2 codes to build a visitor control access system when a visitor will scan its ID to open the servo, pass through and servo closes.

I uploaded the above 2 codes and the combined version which I am currently stuck at for weeks.

Code 1: Control Access System

#include <Servo.h>

Servo s1;
int ledPinGreen = 10;                  // Green LED connected to digital pin 10
int ledPinRed = 11;                    // Red LED connected to digital pin 11
int ledPinBlue = 12;   // blueled entry US sensor
int inputPin1 = 3;     // pushbutton pin for green
int pos = 0;
int ledPinBlue2 = 5;   // blueled2 exit US sensor
int inputPin2 = 9;
 
int UStrigPin = 13;    //entry US sensor output 
int USechoPin = 8;     //entry US sensor input
long duration;
int distance;

int UStrigPin2 = 7;    // exit US sensor output
int USechoPin2 = 6;     // exit US sensor input
long duration2;
int distance2;

void setup (){
  s1.attach(4);
  pinMode(ledPinGreen, OUTPUT);       // sets the pin as output
  pinMode(ledPinRed, OUTPUT);         // sets the pin as output
  pinMode(inputPin1, INPUT);   // entry set 
  pinMode(ledPinBlue, OUTPUT);
  pinMode(inputPin2, INPUT);   // exit set 
  
  Serial.begin(9600);
  pinMode(UStrigPin, OUTPUT);
  pinMode(USechoPin, INPUT);
  pinMode(UStrigPin2, OUTPUT);
  pinMode(USechoPin2, INPUT);
}

void loop(){
    long dist;  
   digitalWrite(ledPinRed, HIGH);   // turn the Red LED on (LOW is the voltage level)
   if (digitalRead(inputPin1) == LOW) {  // read the state of the pushbutton1 value
      for (pos = 0; pos < 90; pos +=1){
      s1.write(pos);
      delay(1);
   digitalWrite(ledPinRed, LOW);   // turn the Red LED off (LOW is the voltage level)
   digitalWrite(ledPinGreen, HIGH);   // turn the Green LED on (HIGH is the voltage level)
    delay(3000);
    digitalWrite(UStrigPin,HIGH);   //output
    delay(1000);
    digitalWrite(UStrigPin,LOW);   //output
    duration=pulseIn(USechoPin,HIGH);
    distance = duration*0.034/2;
    dist = distance;
    Serial.println(distance);
    
    if (dist <= 15) {
    digitalWrite(ledPinBlue,HIGH);
    }
    else {
      digitalWrite(ledPinBlue,LOW);
      for (pos = 0; pos < 90; pos +=1){
      s1.write(pos);
      delay(1);
      digitalWrite(ledPinRed,HIGH);
      digitalWrite(ledPinGreen,LOW);
      }
    }
}
   }
   if (digitalRead(inputPin2) == LOW) {  // read the state of the pushbutton1 value
      for (pos = 0; pos < 90; pos +=1) {
      s1.write(pos);
      delay(1);
   digitalWrite(ledPinRed, LOW);   // turn the Red LED off (LOW is the voltage level)
   digitalWrite(ledPinGreen, HIGH);   // turn the Green LED on (HIGH is the voltage level)
    delay(3000);
    digitalWrite(UStrigPin2,HIGH);   //output
    delay(1000);
    digitalWrite(UStrigPin2,LOW);   //output
    duration2 = pulseIn(USechoPin2,HIGH);
    distance2 = (duration2*0.034)/2;
    dist = distance2;
    Serial.println(distance2);
    
    if (dist <= 15) {
    digitalWrite(ledPinBlue2,HIGH);
    }
    else {
      digitalWrite(ledPinBlue2,LOW);
      for (pos = 0; pos < 90; pos +=1){
      s1.write(pos);
      delay(1);
      digitalWrite(ledPinRed,HIGH);
      digitalWrite(ledPinGreen,LOW);
      }

    }
}
   digitalWrite(ledPinGreen, LOW);
   }
}

Code 2: Combined Sketch

/*
The original version was developed by Oleg Mazurov@circuitsathome.com and the second version which was edited to get it full working was developed by @electroingenio.com
*/

//#include <hid.h> //Add to Oleg Mazurov code to Bar Code Scanner
#include <hiduniversal.h> //Add to Oleg Mazurov code to Bar Code Scanner
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <Usb.h>
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <hidboot.h>
#include <SPI.h>
#define HID_PROTOCOL_KEYBOARD 1
USB Usb;
HIDUniversal Hid(&Usb); //Add this line so that the barcode scanner will be recognized, I use "Hid" below
HIDBoot<HID_PROTOCOL_KEYBOARD> Keyboard(&Usb);

#include <Servo.h>
Servo s1;
int ledPinGreen = 10;                  // Green LED connected to digital pin 10
int ledPinRed = 11;                    // Red LED connected to digital pin 11
int ledPinBlue = 12;   // blueled entry US sensor
int inputPin1 = 3;     // pushbutton pin for green
int pos = 0;
int ledPinBlue2 = 5;   // blueled2 exit US sensor
int inputPin2 = 9;
 
int UStrigPin = 13;    //entry US sensor output 
int USechoPin = 8;     //entry US sensor input
long duration;
int distance;

int UStrigPin2 = 7;    // exit US sensor output
int USechoPin2 = 6;     // exit US sensor input
long duration2;
int distance2;
long dist;
 
class KbdRptParser : public KeyboardReportParser
{
void PrintKey(uint8_t mod, uint8_t key); // Add this line to print character in ASCII

protected:
virtual void OnKeyDown (uint8_t mod, uint8_t key);
virtual void OnKeyPressed(uint8_t key);
};

void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)
{
  uint8_t c = OemToAscii(mod, key);
  if (c)
  OnKeyPressed(c);
}

/* what to do when symbol arrives */
void KbdRptParser::OnKeyPressed(uint8_t key)
{
  
  if(key!=19) Serial.print((char)key);
  else 
  {Serial.print((char)0x0D);
  Serial.println();
  }
};

KbdRptParser Prs;

void setup()
{
  Serial.begin(9600); 
  if(Usb.Init()==-1) Serial.println("OSC did not start.");
  else Serial.println("Barcode Ready");
  Hid.SetReportParser(0,(HIDReportParser*)&Prs); //Change "Keyboard" for "Hid"

   s1.attach(4);
  pinMode(ledPinGreen, OUTPUT);       // sets the pin as output
  pinMode(ledPinRed, OUTPUT);         // sets the pin as output
  pinMode(inputPin1, INPUT);   // entry set 
  pinMode(ledPinBlue, OUTPUT);
  pinMode(inputPin2, INPUT);   // exit set 
  
  pinMode(UStrigPin, OUTPUT);
  pinMode(USechoPin, INPUT);
  pinMode(UStrigPin2, OUTPUT);
  pinMode(USechoPin2, INPUT);
}

void loop()
{
    digitalWrite(ledPinRed, HIGH);
    (Usb.Task());
}

How can I code so that when the barcode scanner scanned an ID, it will turn off RED Led, trigger the Green LED to turn on?

Any help will be greatly appreciated! Thank you in advance and Cheers!

Take a look at the advice for contacting Forum, #7, how to attach sketches. Many of us work from smartphones and then reading Your codes calls for too much, unneccesary work, and You get no answer.
To integrate two working sketches should not be difficult.

Railroader:
Take a look at the advice for contacting Forum, #7, how to attach sketches. Many of us work from smartphones and then reading Your codes calls for too much, unneccesary work, and You get no answer.
To integrate two working sketches should not be difficult.

Thanks Railroader for the quick prompt. I am new to this Forum thus is still unfamiliar with it. :slight_smile: