아두이노stepper motor 질문입니다
페이지 정보
작성자 박찬순 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 작성일15-11-12 22:20 조회1,753회 댓글2건본문
초음파센서를이용해서 자동쓰레기통을 구현하는중에 질문이있습니다. 초음파로 거리에따라 휴지통이 자동으로 열렸다가 닫혔다가 하는것을 구현하는중인데요,
모터가 다가가면 시계방향(open)으로가다가 멀어지면 다시반시계방향(close)으로 도는것까지는구현을 했는데
반시계방향으로 돌다가멈추게는할수 없을까요???? 멈추지않으면 쓰레기통에연결되어있는 낚시줄이 계속감기게되어 끊어 짐니다. 아래소스입니다.
int motorPin1 = 11; // INA
int motorPin2 = 4; // INB
int motorPin3 = 2; // INC
int motorPin4 = 3; // IND
const int trigPin = 13;
const int echoPin = 12;
const int ledPin = 10;
unsigned int gun_countStep = 0;
unsigned int gun_direction = 0;
int motorSpeed = 3;
void setup() {
//declare the motor pins as outputs
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
Serial.begin(9600);
}
void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration / 58;
if(distance > 100 || distance < 0)
Serial.println("Out of range");
else {
Serial.print(distance);
Serial.println(" cm");
}
if (distance <= 10)
digitalWrite(ledPin, HIGH);
else
digitalWrite(ledPin, LOW);
if(distance < 10) {
if(gun_direction == 0){
clockwise_2PhaseExcitation(); // 시계 방향
}
else {
counterclockwise_2PhaseExcitation();
}
}
}
void counterclockwise_2PhaseExcitation(){ // 반 시계 방향
// 1
digitalWrite(motorPin4, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin1, LOW);
delay (motorSpeed);
// 2
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin1, LOW);
delay(motorSpeed);
// 3
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin1, HIGH);
delay (motorSpeed);
// 4
digitalWrite(motorPin4, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin1, HIGH);
delay(motorSpeed);
}
void clockwise_2PhaseExcitation(){ // 시계 방향
// 1
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay (motorSpeed);
// 2
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(motorSpeed);
// 3
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, HIGH);
delay (motorSpeed);
// 4
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(motorSpeed);
}
댓글목록
세유니님의 댓글
세유니 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 작성일스텝모터니까 반 시계 방향으로 몇회 회전 한 후에 멈추게 for 문을 이용하면 어떨까요?
최고관리자님의 댓글

1. 윗 분 말씀대로 for 문을 이용해서 일정 스텝만큼 반복해서 돌도록 작성하거나
2. 뚜껑에 버튼(혹은 엔드스탑) 등을 달아서 완전히 열렸는지(혹은 닫혔는지) 체크할 수 있도록 구성해서 신호가 오긴 전까지 계속 돌도록 하는 방법이 있겠네요