Only 1 servo working when using Arduino + Barcode Scanner + USB Host

I’m fairly new to the Arduino. i want to make a barcode to scan and move the servo based on the data from barcode scanner, i have the barcode reader working with arduino from https://github.com/felis/USB_Host_Shield_2.0/issues/323 but when i add servo code, only 1 servo working, idk how to fix it
this is my code:

#include <usbhid.h>
#include <usbhub.h>
#include <hiduniversal.h>
#include <hidboot.h>
#include <SPI.h>
#include <Servo.h>

String hasil;

Servo servo1;
Servo servo2;
Servo servo3;

class MyParser : public HIDReportParser {
  public:
    MyParser();
    void Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
  protected:
    uint8_t KeyToAscii(bool upper, uint8_t mod, uint8_t key);
    virtual void OnKeyScanned(bool upper, uint8_t mod, uint8_t key);
    virtual void OnScanFinished();
};

MyParser::MyParser() {}

void MyParser::Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) {
  // If error or empty, return
  if (buf[2] == 1 || buf[2] == 0) return;

  for (uint8_t i = 7; i >= 2; i--) {
    // If empty, skip
    if (buf[i] == 0) continue;

    // If enter signal emitted, scan finished
    if (buf[i] == UHS_HID_BOOT_KEY_ENTER) {
      OnScanFinished();
    }

    // If not, continue normally
    else {
      // If bit position not in 2, it's uppercase words
      OnKeyScanned(i > 2, buf, buf[i]);
    }

    return;
  }
}

uint8_t MyParser::KeyToAscii(bool upper, uint8_t mod, uint8_t key) {
  // Letters
  if (VALUE_WITHIN(key, 0x04, 0x1d)) {
    if (upper) return (key - 4 + 'A');
    else return (key - 4 + 'a');
  }

  // Numbers
  else if (VALUE_WITHIN(key, 0x1e, 0x27)) {
    return ((key == UHS_HID_BOOT_KEY_ZERO) ? '0' : key - 0x1e + '1');
  }

  return 0;
}

void MyParser::OnKeyScanned(bool upper, uint8_t mod, uint8_t key) {
  uint8_t ascii = KeyToAscii(upper, mod, key);
  hasil = hasil + (char)ascii;
}

void MyParser::OnScanFinished() {
  Serial.println(hasil);
  //Cek variable 
  if(hasil.substring(0,1) == "1"){
    Serial.println("Hasil: 1");
    Serial.println("Reset hasil");
    hasil = "";
    //Mulai menggerakkan servo
    Serial.println("Membuka gate 1");
    servo1.write(180);
  }else if(hasil.substring(0,1) == "2"){
    Serial.println("Hasil: 2");
    Serial.println("Reset hasil");
    hasil = "";
    servo2.write(180);
  }else if(hasil.substring(0,1) == "3"){
    Serial.println("Hasil: 3");
    Serial.println("Reset hasil");
    hasil = "";
    servo3.write(180);
  }else{
    Serial.println("Hasil tidak ditemukan");
    hasil = "";
  }
}

USB          Usb;
//USBHub       Hub(&Usb);
HIDUniversal Hid(&Usb);
MyParser     Parser;

void setup() {
  Serial.begin(115200);
  Serial.println("Start");
  
  if (Usb.Init() == -1) {
    Serial.println("OSC did not start.");
  }

  delay(200);

  Hid.SetReportParser(0, &Parser);
  servo1.attach(8);
  servo2.attach(9);
  servo3.attach(10);
  servo1.write(0);
  servo2.write(0);
  servo3.write(0);
}

void loop() {
  Usb.Task();
}

Which servo works? What exactly do the other two do? Do your Serial.prints show that it is writing to all of the servos?

What type of servos are they? How are they powered. Almost all servo problems are caused by insufficient power e.g. trying to power them from the Arduino 5V pin or through a breadboard or other poor connections.

Steve

Use a 4xAA battery pack to power 1-2 small servos; never the 5V output. Don't forget to connect the grounds.

slipstick:
Which servo works? What exactly do the other two do? Do your Serial.prints show that it is writing to all of the servos?

What type of servos are they? How are they powered. Almost all servo problems are caused by insufficient power e.g. trying to power them from the Arduino 5V pin or through a breadboard or other poor connections.

Steve

Thanks for answering, Only servo connected in pin 8 work correctly, other servo have same working principle, when the result from barcode scanner have a first character in hasil (result) is 1 then servo1 write 180 and when result have the first charater 2 then servo2 write 180 etc. I use 12v external power supply and when i use sweep example project all three servo works fine

I use 12v external power supply

What servos take a 12V power supply?

jremington:
What servos take a 12V power supply?

for the arduino, not for servos

OK, what OTHER power supply are you using for the servos? (See replies #1 and #2).

jremington:
OK, what OTHER power supply are you using for the servos? (See replies #1 and #2).

I just connecting it to the 5v arduino pin and ground using breadboard, share the power between all servos, i will try using a battery if that is the problem

slipstick:
How are they powered. Almost all servo problems are caused by insufficient power e.g. trying to power them from the Arduino 5V pin or through a breadboard or other poor connections.

When I said that way back in reply #1 I wan't kidding. So use a battery (4 x AA, preferably NiMH recharegables) and solid connections NOT via a breadboard. The 5V pin can't provide enough current and the breadboard probably can't carry enough current.

Steve

if that is the problem

How many times will we have to tell you that yes, that is the problem, before it starts to sink in?

Attempting to power motors and servos from the 5V output is a popular way to destroy an Arduino.