2017-06-17 핀번호 빼고 끝 |
좋아요:0 | ||||
---|---|---|---|---|---|
작성자 | 이지성 | 등록일 | 17.06.17 | 조회수 | 122 |
첨부파일 | |||||
아두이노 uno ->아두이노 mega 팬모터 1 릴레이 1 dc모터 2 l298n 1 서보모터 2 그린라이트 스위치 1 led 2 초음파센서 2 ----------------------------------------------------------------------------------------------------------- #include Servo left_brush; //빗자루 Servo right_brush; int left_trigPin = ; //trig 핀 설정 디지털핀 int left_echoPin = ; //echo 핀 설정 int right_trigPin = ; //trig 핀 설정 디지털핀 int right_echoPin = ; //echo 핀 설정 int left_brush_pin = ; //브러쉬핀 설정 int right_brush_pin = ; int left_led = ; int right_led = ; int Fan_Motor = : int range = 10; //cm int ENA = ; //모터A속도 ~핀으로 analogWrite 0~255 int IN1 = ; //모터A int IN2 = ; // int IN3 = ; //모터B int IN4 = ; // int ENB = ; //모터B속도 ~핀으로 analogWrite int Motor_speed = ; //min 0~255 max void left_measure() { float left_duration, left_distance; digitalWrite(left_trigPin, HIGH); //trigPin HIGH delay(10); //0.01초 딜레이 digitalWrite(left_trigPin, LOW); //trigPin LOW left_duration = pulseIn(left_echoPin, HIGH); //echoPin 이 HIGH를 유지한 시간을 저장한다. left_distance = ((float)(340 * left_duration) / 10000) / 2; //거리계산 cm } void right_measure() { float right_duration, right_distance; digitalWrite(right_trigPin, HIGH); delay(10); digitalWrite(right_trigPin, LOW); right_duration = pulseIn(right_echoPin, HIGH); right_distance = ((float)(340 * right_duration) / 10000) / 2; } void Brushing() { left_brush.write(0); right_brush.write(90); delay(250); left_brush.write(90); right_brush.write(0); delay(250); } void GO() { digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW); } void BACK() { digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH); } void turn_left() { digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH); } void turn_right() { digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW); } void setup() { pinMode(left_trigPin, OUTPUT); //trigPin모드 OUTPUT pinMode(left_echoPin, INPUT); //echoPin모드 INPUT pinMode(right_trigPin, OUTPUT); pinMode(right_echoPin, INPUT); left_brush.attach(left_brush_pin); right_brush.attach(right_brush_pin); pinMode(left_led, OUTPUT); pinMode(right_led, OUTPUT); pinMode(Fan_Motor, OUTPUT); pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT); pinMode(ENA, OUTPUT); pinMode(ENB, OUTPUT); analogWrite(ENA, Motor_speed); analogWrite(ENB, Motor_speed); } void loop() { digitalWrite(Fan_Motor,HIGH); Brushing() left_measure(); right_measure(); if(left_distance < range || right_distance < range) { if(left_distance < right_distance) { turn_right(); digitalWrite(right_led,LOW); delay(100); digitalWrite(right_led,HIGH); delay(100); } else if(left_distance > right_distance) { turn_left(); digitalWrite(left_led,LOW); delay(100); digitalWrite(left_led,HIGH); delay(100); } } else if(left_distance > range && right_distance > range) { GO(); digitalWrite(left_led,HIGH); digitalWrite(right_led,HIGH); } } |
이전글 | 6월 17일 |
---|---|
다음글 | 초음사센서이용(17/6/17) |