BASIC4MCU | 질문게시판 | lcd,바코드,서보모터, 아두이노 질문 드립니다. ㅠㅠ
페이지 정보
작성자 아두이노개발자 작성일2023-09-26 11:24 조회1,979회 댓글1건본문
바코드 인식을 LCD로 하는 과정에서 문제가 있어 질문 드립니다.
#include <usbhid.h>
#include <usbhub.h>
#include <hiduniversal.h>
#include <hidboot.h>
#include <SPI.h>
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);
}
void MyParser::OnScanFinished() {
}
USB Usb;
USBHub Hub(&Usb);
HIDUniversal Hid(&Usb);
MyParser Parser;
void setup() {
Serial.begin( 115200 );
if (Usb.Init() == -1) {
Serial.println("OSC did not start.");
}
delay( 200 );
Hid.SetReportParser(0, &Parser);
}
void loop() {
Usb.Task();
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo servo1;
Servo servo2;
int Trig1 = 10;
int Echo1 = 11;
int Trig2 = 8;
int Echo2 = 9;
int ultrasonic_1 = 2;
int ultrasonic_2 = 3;
int ultrasonic_num_1 = 0;
int ultrasonic_num_2 = 0;
unsigned long current_time;
unsigned long code1_time[5];
unsigned long code2_time[5];
int code1_count = 0;
int code2_count = 0;
int code1_count2 = 0;
int code2_count2 = 0;
int code1_num_1 = 0;
int code2_num_1 = 0;
int code1_num_2 = 0;
int code2_num_2 = 0;
char recieved_data;
int recieved_data_num;
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
pinMode(Trig1, OUTPUT);
pinMode(Echo1, INPUT);
pinMode(Trig2, OUTPUT);
pinMode(Echo2, INPUT);
pinMode(ultrasonic_1, OUTPUT);
pinMode(ultrasonic_2, OUTPUT);
servo1.attach(6);
servo2.attach(5);
servo1.write(0);
servo2.write(0);
}
void loop() {
current_time = millis();
if(Serial.available()) {
recieved_data = Serial.read();
Serial.println(recieved_data);
}
lcd.setCursor(0,0);
lcd.print("Black_ RED_ ");
if(recieved_data == '1') {
code1_time[code1_count] = current_time + 3400; //3초 후
code1_num_1 = 1;
code1_count++;
code1_count2++;
}
lcd.setCursor(0,1);
lcd.print("IN:");
lcd.setCursor(3,1);
lcd.print(code1_count2);
for(int i = 0; i < 5; i++) {
if((code1_num_1 == 1) && (code1_time[i] < current_time) && (current_time < code1_time[i] + 250)) {
code1_num_2 = 1;
}
}
if(code1_num_2 == 1) {
servo1.write(40);
code1_num_2 = 0;
}
else if(code1_num_2 == 0) {
servo1.write(0);
}
if(recieved_data == '2') {
code2_time[code2_count] = current_time + 5500; //5초 후
code2_num_1 = 1;
code2_count++;
code2_count2++;
}
lcd.setCursor(8,1);
lcd.print("IN:");
lcd.setCursor(11,1);
lcd.print(code2_count2);
for(int i = 0; i < 5; i++) {
if((code2_num_1 == 1) && (code2_time[i] < current_time) && (current_time < code2_time[i] + 250)) {
code2_num_2 = 1;
}
}
if(code2_num_2 == 1) {
servo2.write(40);
code2_num_2 = 0;
}
else if(code2_num_2 == 0) {
servo2.write(0);
}
digitalWrite(Trig1, LOW);
digitalWrite(Echo1, LOW);
delayMicroseconds(2);
digitalWrite(Trig1, HIGH);
delayMicroseconds(10);
digitalWrite(Trig1, LOW);
unsigned long duration1= pulseIn(Echo1, HIGH);
float distance1 = ((float)(340 * duration1) / 10000) / 2;
digitalWrite(Trig2, LOW);
digitalWrite(Echo2, LOW);
delayMicroseconds(2);
digitalWrite(Trig2, HIGH);
delayMicroseconds(10);
digitalWrite(Trig2, LOW);
unsigned long duration2= pulseIn(Echo2, HIGH);
float distance2 = ((float)(340 * duration2) / 10000) / 2;
for(int i = 0; i < 5; i++) {
if((code1_num_1 == 1) && (distance1 < 4) && (code1_time[i] - 1000 < current_time) && (current_time < code1_time[i] + 1000)) {
ultrasonic_num_1 = 1;
}
}
if(ultrasonic_num_1 == 1) {
digitalWrite(ultrasonic_1, HIGH);
} else {
digitalWrite(ultrasonic_1, LOW);
}
for(int i = 0; i < 5; i++) {
if((code2_num_1 == 1) && (distance2 < 4) && (code2_time[i] - 1000 < current_time) && (current_time < code2_time[i] + 1000)) {
ultrasonic_num_2 = 1;
}
}
if(ultrasonic_num_2 == 1) {
digitalWrite(ultrasonic_2, HIGH);
} else {
digitalWrite(ultrasonic_2, LOW);
}
recieved_data = 0;
if(code1_count == 4) {
code1_count = 0;
}
if(code2_count == 4) {
code2_count = 0;
}
ultrasonic_num_1 = 0;
ultrasonic_num_2 = 0;
delay(200);
lcd.clear();
}
댓글 1
조회수 1,979master님의 댓글
master 작성일
동일한 회로를 가지고 있고
회로 연결도 동일하게 했을 경우에만
동일한 증상을 겪을 수 있습니다.
불가능하겠죠?
설명을 많이 하셔야 할 것 같습니다.