urobot2011 2022-07-19 21:03:36
#include
Servo servo;
char data;
// 핀 설정
int piezoPin = 11;
int echoPin = 3;
int trigPin = 2;
int servoPin = 9;
unsigned long currentTime = 0;
boolean flag = 0;
// 초음파 센서의 손 측정 시간 설정 변수
int checkTime = 2000;
// 손소독제 크기에 따른 모터의 각도 조절 변수
int pushAngle = 40;
int currentAngle = 90;
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
servo.attach(servoPin);
servo.write(currentAngle);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(piezoPin, OUTPUT);
}
void loop() {
if (Serial.available()) {
data = Serial.read();
digitalWrite(trigPin, LOW);
digitalWrite(echoPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
unsigned long duration = pulseIn(echoPin, HIGH);
float distance = duration / 29.0 / 2.0;
if (distance <= 10) {
digitalWrite(13, HIGH);
if (flag == 0) {
currentTime = millis();
flag = 1;
}
if (millis() >= currentTime + checkTime) {
if (data == '1') {
tone(piezoPin, 1046, 200);
delay(200);
tone(piezoPin, 1175, 200);
delay(200);
tone(piezoPin, 1319, 200);
delay(800);
servo.write(pushAngle);
delay(1000);
servo.write(currentAngle);
delay(1000);
}
if (data == '2') {
tone(piezoPin, 1319, 150);
delay(200);
tone(piezoPin, 1319, 150);
delay(200);
tone(piezoPin, 1319, 150);
delay(200);
}
//추가{
if (data == '3') {
tone(piezoPin, 1319, 150);
delay(200);
tone(piezoPin, 1319, 150);
delay(200);
tone(piezoPin, 1319, 150);
delay(200);
}
//}추가
/*
if (data == '몇번째인지') {
할일
}
*/
flag = 0;
}
}
else {
digitalWrite(13, LOW);
flag = 0;
noTone(piezoPin);
}
}
}
/*
아두이노 기초 공부 추천드려요.
*/
tpals8824 2022-04-14 19:24:57
저 혹시 질문 하나 드려도 될까요? 티처블 머신에서 3가지 이상의 경우를 학습시켜 보았을 때 잘 구별하는 모습을 볼 수 있었는데, 아두이노로 가지고 오니까 3가지 이상의 경우에 대해서는 동작을 안하더라구요.. 혹시 더 다양한 경우에 대해서도 동작이 가능한가요?