아두이노/제품 검사 센서
#4 제작
songbum
2023. 5. 30. 23:28
지금까지 테스트한 것들을 하나로 모아 최종 테스트를 진행해본다.
아두이노 IDE 에 연결하고 지금까지 테스트 했던 코드들을 하나로 합쳐 업로드한다
대기시간 관련 부분은 코드가 중복될뿐만 아니라 개발/유지보수 단계에서 계속 변경될 확률이 높아서, 별도의 사용자정의 함수 get_delay() 를 만들어 분리했다. 지금은 테스트 단계라서 기본값 1초에 스위치 선택에 따라 0.5 초 또는 1.5초를 선택할 수 있게 해놨는데, 나중에 납품할 때는 시간을 줄일 생각이다.
한가지 주의할 점은 아두이노에서의 사용자정의 함수는 호출되기 전에 먼저 정의가 돼 있어야 하므로 loop() 함수보다 윗 줄에 코딩돼 있어야 한다는 것이다.
int buzzerPin = 2;
int switch1Pin = 3;
int switch2Pin = 4;
int sensor1Pin = 5;
int sensor2Pin = 6;
int switch1Value;
int switch2Value;
int delaySeconds;
void setup() {
Serial.begin(9600);
pinMode(buzzerPin, OUTPUT);
pinMode(switch1Pin, INPUT_PULLUP);
pinMode(switch2Pin, INPUT_PULLUP);
pinMode(sensor1Pin, INPUT_PULLUP);
pinMode(sensor2Pin, INPUT_PULLUP);
}
void get_delay() {
switch1Value = digitalRead(switch1Pin);
switch2Value = digitalRead(switch2Pin);
if ( (switch1Value == 1) && (switch2Value == 1) ) {
Serial.println("Select none");
delaySeconds = 1000;
}
else if ( (switch1Value == 1) && (switch2Value == 0) ) {
Serial.println("Select 1");
delaySeconds = 1500;
}
else if ( (switch1Value == 0) && (switch2Value == 1) ) {
Serial.println("Select 2");
delaySeconds = 500;
}
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(sensor1Pin) == LOW) || (digitalRead(sensor2Pin) == LOW) ) {
// 검사 종료됐으니 처음부터 다시 시작
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:;
}
두 센서 모두 정상작동 하는 것을 확인했고, 토글 스위치를 변경함에 따라 인식속도가 변하는 것도 확인할 수 있었다.