#include <Servo.h>
// 핀 설정
const int trigPin = 8;
const int echoPin = 9;
const int moisturePin = A0;
const int servoPin = 7;
// 거리 및 수분 기준값 설정
const int distanceThreshold = 15; // cm, 초음파 감지 거리
const int moistureThreshold = 400; // 값이 작을수록 더 젖음
Servo myServo;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
myServo.attach(servoPin);
myServo.write(90); // 중간 위치 (대기 상태)
}
void loop() {
// 초음파 센서로 거리 측정
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
// 감지되면 분류 시작
if (distance > 0 && distance < distanceThreshold) {
int moistureValue = analogRead(moisturePin);
Serial.print("Moisture: ");
Serial.println(moistureValue);
if (moistureValue < moistureThreshold) {
Serial.println("젖은 쓰레기 → 왼쪽으로 분류");
myServo.write(45); // 왼쪽
} else {
Serial.println("마른 쓰레기 → 오른쪽으로 분류");
myServo.write(135); // 오른쪽
}
delay(1500); // 분류 시간 대기
myServo.write(90); // 원위치
delay(1000); // 대기
}
delay(200); // 센서 반복 주기
}
모터가 반응할 습도 기준과
초음파 센서가 반응할 거리 기준을 정한다
습도값이 반응 기준보다 낮으면 젖은 쓰레기로 분류
습도값이 반응 기준보다 높으면 마른 쓰레기로 분류
다음 물체의 분류를 위해 원위치로 돌아오기
2025순천고 1학년 6반