제품에 사용되는 부품들이 여럿이다보니 한 곳에 모아 고정시켜 놓는 것도 쉽지 않았다. 인터넷을 찾아보니 만능기판이라는 것을 사용하는 거 같아 몇 가지 크기들로 구입했다. 우선 아두이노 Micro 보드를 만능기판에 납땜하고, 주변의 남는 공간에 부저와 로터리 딥 스위치, 터치 스위치 등을 납땜했다. 만능기판 덕분에 좀 더 수월하게 튼튼하고 효율적인 배선이 가능했다.
아두이노 IDE 에서 지금까지 테스트했던 코드들을 하나로 모아 업로드했다. 디지털 핀 번호를 순차적으로 사용하지 않고 뒤죽박죽으로 할당했는데, 만능기판에 납땜하면서 최대한 가까운 곳에 위치한 핀으로 연결하다보니 이렇게 됐다.
포토 인터럽터 센서 두 곳에 모두 물체가 감지되면 'PASS' 상태가 되고, 이 상태에서 터치 스위치를 누르면 엔터키 신호가 출력된다. 이 때 터치 스위치를 오래 누르고 있거나 반복해서 누르는 경우가 있을 수 있는데, 그러면 엔터키 신호가 무수히 많이 출력되는 문제가 있다. 그래서 keyboardSend 라는 변수를 이용해, 'PASS' 상태에서는 딱 한번만 엔터키 신호를 보내고 그 이후부터는 무시하도록 했다.
엔터키 신호를 출력하도록 하기 위해 KEY_RETURN 상수를 이용했다.
#include <Keyboard.h>
int buzzerPin = 2;
int sensor1Pin = 4;
int sensor2Pin = 6;
int rotary1Pin = 7;
int rotary2Pin = 8;
int touchButtonPin = 16;
int delaySeconds;
boolean keyboardSend = false;
void setup() {
Serial.begin(9600);
while(!Serial && millis()<5000) {
}
Keyboard.begin();
pinMode(buzzerPin, OUTPUT);
pinMode(sensor1Pin, INPUT_PULLUP);
pinMode(sensor2Pin, INPUT_PULLUP);
pinMode(rotary1Pin, INPUT_PULLUP);
pinMode(rotary2Pin, INPUT_PULLUP);
pinMode(touchButtonPin, INPUT);
}
void get_delay() {
// "1" 핀과 "2" 핀 모두 선택 됨
if ( (digitalRead(rotary1Pin) == LOW) && (digitalRead(rotary2Pin) == LOW) ) {
delaySeconds = 800;
}
// "1" 핀은 선택 안 되고, "2" 핀만 선택 됨
else if ( (digitalRead(rotary1Pin) == HIGH) && (digitalRead(rotary2Pin) == LOW) ) {
delaySeconds = 600;
}
// "1" 핀만 선택 되고, "2" 핀은 선택 안 됨
else if ( (digitalRead(rotary1Pin) == LOW) && (digitalRead(rotary2Pin) == HIGH) ) {
delaySeconds = 400;
}
// "1" 핀과 "2" 핀 모두 선택 안 됨
else if ( (digitalRead(rotary1Pin) == HIGH) && (digitalRead(rotary2Pin) == HIGH) ) {
delaySeconds = 200;
}
//Serial.println(delaySeconds);
delay(delaySeconds);
}
void loop() {
// 어느 한 센서에라도 물체가 들어오면 시작
if ( (digitalRead(sensor1Pin) == HIGH) || (digitalRead(sensor2Pin) == HIGH) ) {
// 나머지 센서에도 물체가 들어오는지 체크하기 전에 잠시 대기
get_delay();
// 나머지 센서 체크
while (1) {
// case 1 : 처음 센서와 나중 센서 모두 물체 감지
if ( (digitalRead(sensor1Pin) == HIGH) && (digitalRead(sensor2Pin) == HIGH) ) {
Serial.println("PASS");
// 센서에서 물체를 빼는 것 체크
while (1) {
// 터치 스위치를 누르면 엔터키 출력
if (digitalRead(touchButtonPin) == HIGH) {
// 일단 한번 엔터키 출력을 했으면, 터치 스위치를 계속 누르더라도 중복해서 출력하지 않음
if (keyboardSend == false) {
Serial.println("Touch");
keyboardSend = true;
digitalWrite(buzzerPin, HIGH);
Keyboard.press(KEY_RETURN);
delay(300);
Keyboard.release(KEY_RETURN);
digitalWrite(buzzerPin, LOW);
}
}
// 센서에서 물체를 빼는 것 체크
if ( (digitalRead(sensor1Pin) == LOW) || (digitalRead(sensor2Pin) == LOW) ) {
keyboardSend = false;
// 검사 종료됐으니 처음부터 다시 시작
goto gotoNextLoop;
}
}
}
// case 2 : 처음 센서는 물체 감지했으나 나중 센서는 미감지
else if ( (digitalRead(sensor1Pin) == HIGH) && (digitalRead(sensor2Pin) == LOW) ) {
Serial.println("Fail 2");
digitalWrite(buzzerPin, HIGH);
// 센서에서 물체를 빼는 것 또는 뒤늦게 넣는 것 체크
while (1) {
if ( (digitalRead(sensor1Pin) == LOW) || (digitalRead(sensor2Pin) == HIGH) ) {
digitalWrite(buzzerPin, LOW);
// 검사 종료됐으니 처음부터 다시 시작
goto gotoNextLoop;
}
}
}
// case 3 : 처음 센서는 물체 미감지했으나 나중 센서는 감지
else if ( (digitalRead(sensor1Pin) == LOW) && (digitalRead(sensor2Pin) == HIGH) ) {
Serial.println("Fail 1");
digitalWrite(buzzerPin, HIGH);
// 센서에서 물체를 빼는 것 또는 뒤늦게 넣는 것 체크
while (1) {
if ( (digitalRead(sensor1Pin) == HIGH) || (digitalRead(sensor2Pin) == LOW) ) {
digitalWrite(buzzerPin, LOW);
// 검사 종료됐으니 처음부터 다시 시작
goto gotoNextLoop;
}
}
}
// case 4 : 이상 발생시, 처음부터 다시 시작
else {
break;
}
}
}
gotoNextLoop:;
}
완성한 후, 최종 테스트 결과이다.
'아두이노 > 제품 검사 센서' 카테고리의 다른 글
#9 제작 - 기능 개선 (0) | 2023.07.18 |
---|---|
#7 기획 및 검토 - 로터리 딥 스위치 (0) | 2023.06.10 |
#6 기획 및 검토 - 터치 스위치 (0) | 2023.06.05 |
#5 기획 및 검토 - 아두이노 Micro (0) | 2023.06.03 |
#4 제작 (0) | 2023.05.30 |