1215조경아1207신해인1203백우준이준상박다솜장광재
Published

SSHS_CS_2반_2019_어지러진_실험실을_깔끔하게_정리하는_아두이노_LabCleaning Arduino

바코드를 통해 병의 종류를 구분하여, 알맞은 위치로 이동시킨다. By barcode recognition, the types of bottles are identified and moved to the set location. (Arduino)

BeginnerShowcase (no instructions)2,129
SSHS_CS_2반_2019_어지러진_실험실을_깔끔하게_정리하는_아두이노_LabCleaning Arduino

Things used in this project

Hardware components

과학상자
×1
photo interrupter
arduino photo interrupter
×1
Arduino MotorShield Rev3
Arduino MotorShield Rev3
×1
barcode scanner
arduino barcode scanner
×1
Arduino USB Host Shield
Arduino USB Host Shield
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Code

Arduino
Used some Barcode Censor open source code.
#include <usbhid.h>
#include <usbhub.h>
#include <hiduniversal.h>
#include <hidboot.h>
#include <SPI.h>

#define FORWARD   1
#define BACKWARD  0

#define RED_LED_PIN   31  //아래, 위로 움직일 때 표시
#define GREEN_LED_PIN 32  //대기 상태 표시
#define BLUE_LED_PIN  33  //슬라이드가 병을 밀고, 원위치할 때 표시

#define DIRA_PIN    12    //모터 A : 병을 미는 모터
#define DIRB_PIN    13    //모터 B : 엘리베이터를 위, 아래로 움직이는 모터
#define BRAKEA_PIN  9
#define BRAKEB_PIN  8
#define PWMA_PIN    3
#define PWMB_PIN    11

int PhotoInterrupter1_pin = 42; //위쪽 광센서
int PhotoInterrupter2_pin = 43; //아래쪽 광센서

int barcode_input[50];
int barcode_i = 0;

int action = 0; //진행 상태 표시.. 0이면 대기 상태, 1x이면 2층 처리 중, 2x이면 1층 처리 중


/**
 * Motor
 */
void StartMotorA(bool is_forward) {
  digitalWrite(DIRA_PIN, is_forward);   //HIGH = forward direction of motor A
  digitalWrite(BRAKEA_PIN, LOW);  //Start motor A
}

void StopMotorA() {
  digitalWrite(DIRA_PIN, LOW);     //backword direction of motor A
  digitalWrite(BRAKEA_PIN, HIGH);  //Stop motor A
}

void StartMotorB(bool is_forward) {
  digitalWrite(DIRB_PIN, is_forward);   //HIGH = forward direction of motor B
  digitalWrite(BRAKEB_PIN, LOW);  //Start motor B
}

void StopMotorB() {
  digitalWrite(DIRB_PIN, LOW);     //backword direction of motor B
  digitalWrite(BRAKEB_PIN, HIGH);  //Stop motor B
}



/**
 * USB & barcode
 */
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);
  //Serial.print((char)ascii);
  barcode_input[barcode_i] = (char)ascii;
  barcode_i++;
}

void MyParser::OnScanFinished() {
  for (int i = 0; i < barcode_i; i++) {
    Serial.print((char)barcode_input[i]);
  }
  Serial.println(" - Finished");
  barcode_i = 0;

  digitalWrite(RED_LED_PIN, HIGH);
  digitalWrite(GREEN_LED_PIN, HIGH);
  digitalWrite(BLUE_LED_PIN, HIGH);
  
  if (barcode_input[0] == '8') {
    if (action == 0) {
      action = 1;
      digitalWrite(RED_LED_PIN, LOW);
    } else {
      Serial.println("under processing - ");
      Serial.println(action);
    }
  } else if (barcode_input[0] == '7') {
    if (action == 0) {
      action = 2;
      digitalWrite(BLUE_LED_PIN, LOW);
    } else {
      Serial.println("under processing - ");
      Serial.println(action);
    }
  }
}


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);

  Serial.println("Started..");

  //LED
  pinMode(RED_LED_PIN, OUTPUT);
  pinMode(GREEN_LED_PIN, OUTPUT);
  pinMode(BLUE_LED_PIN, OUTPUT);
  digitalWrite(RED_LED_PIN, HIGH);
  digitalWrite(GREEN_LED_PIN, HIGH);
  digitalWrite(BLUE_LED_PIN, HIGH);

  //LED test
  digitalWrite(RED_LED_PIN, LOW);
  delay(500);
  digitalWrite(RED_LED_PIN, HIGH);
  digitalWrite(GREEN_LED_PIN, LOW);
  delay(500);
  digitalWrite(GREEN_LED_PIN, HIGH);
  digitalWrite(BLUE_LED_PIN, LOW);
  delay(500);
  digitalWrite(BLUE_LED_PIN, HIGH);

  //
  digitalWrite(RED_LED_PIN, HIGH);
  digitalWrite(GREEN_LED_PIN, LOW);
  digitalWrite(BLUE_LED_PIN, HIGH);

  
  //Setup Channel A
  pinMode(DIRA_PIN, OUTPUT);    //Initiates Motor Channel A pin
  pinMode(BRAKEA_PIN, OUTPUT);  //Initiates Brake Channel A pin
  analogWrite(PWMA_PIN, 150);   //Spins the motor on Channel A at full speed
  digitalWrite(BRAKEA_PIN, HIGH);  //Disengage the Brake for Channel A

  //Setup Channel B
  pinMode(DIRB_PIN, OUTPUT);    //Initiates Motor Channel B pin
  pinMode(BRAKEB_PIN, OUTPUT);  //Initiates Brake Channel B pin
  analogWrite(PWMB_PIN, 253);   //Spins the motor on Channel B at half speed
  digitalWrite(BRAKEB_PIN, HIGH);  //Disengage the Brake for Channel B

  //
  pinMode(PhotoInterrupter1_pin, INPUT);
  pinMode(PhotoInterrupter2_pin, INPUT);

  //
  delay(500);
  if (digitalRead(PhotoInterrupter1_pin) == HIGH) {
    
  }
  StartMotorA(BACKWARD);
  delay(1500);
  StopMotorA();
}



void loop() {

  if (action == 1) {
    Serial.println("Motor up start");
    StartMotorB(FORWARD);
    action = 11;
  } else if (action == 11) {
    if (digitalRead(PhotoInterrupter1_pin) == HIGH) {
      Serial.println("Motor up end");
      StopMotorB();
      delay(500);
      action = 12;
    }
  } else if (action == 12) {
    //Motor slide
    Serial.println("Motor slide start");
    digitalWrite(RED_LED_PIN, HIGH);
    digitalWrite(GREEN_LED_PIN, HIGH);
    digitalWrite(BLUE_LED_PIN, LOW);
    StartMotorA(FORWARD);
    delay(1500);
    action = 13;
  } else if (action == 13) {
    StopMotorA();
    delay(500);
    action = 14;
  } else if (action == 14) {
    StartMotorA(BACKWARD);
    delay(1500);
    action = 15;
  } else if (action == 15) {
    StopMotorA();
    Serial.println("Motor slide end");
    delay(500);
    action = 16;
  } else if (action == 16) {
    //Motor down
    Serial.println("Motor down");
    digitalWrite(RED_LED_PIN, LOW);
    digitalWrite(GREEN_LED_PIN, HIGH);
    digitalWrite(BLUE_LED_PIN, HIGH);
    StartMotorB(BACKWARD);
    action = 17;
    /*
    delay(1450);
    Serial.println("Motor down end");
    StopMotorB();
    action = 0;
    */
  } else if (action == 17) {
    if (digitalRead(PhotoInterrupter2_pin) == HIGH) {
      Serial.println("Motor down end");
      StopMotorB();
      delay(500);
      action = 0;
    }
    
  } else if (action == 2) {
    Serial.println("Motor slide start");
    StartMotorA(FORWARD);
    delay(1500);
    action = 21;
  } else if (action == 21) {
    StopMotorA();
    delay(500);
    action = 22;
  } else if (action == 22) {
    StartMotorA(BACKWARD);
    delay(1500);
    action = 23;
  } else if (action == 23) {
    StopMotorA();
    Serial.println("Motor slide end");
    action = 0;
  }
  if (action == 0) {
    digitalWrite(RED_LED_PIN, HIGH);
    digitalWrite(GREEN_LED_PIN, LOW);
    digitalWrite(BLUE_LED_PIN, HIGH);    
  }

  Usb.Task();
}

Credits

1215조경아

1215조경아

1 project • 0 followers
1207신해인

1207신해인

1 project • 0 followers
1203백우준

1203백우준

1 project • 0 followers
이준상

이준상

1 project • 0 followers
박다솜

박다솜

14 projects • 5 followers
장광재

장광재

29 projects • 16 followers

Comments