BASIC4MCU | 질문게시판 | 초음파 센서3개와 스텝 모터연결 , 스텝모터가 안 돌아요ㅜㅜ
페이지 정보
작성자 소소행 작성일2022-11-29 20:11 조회1,148회 댓글1건본문
초음파 센서 3개를 연결해서 3개 중 하나라도 장애물이랑 가까워지면 스텝모터가 거리에 반비례해서 빨리 돌아가게하는 코딩을 해봤는데 시리얼모니터에는 값들이 잘 나오는데 스텝 모터가 진동만 있고 안 돌아가네요 변수문제인가요ㅜㅜ 알려주시면 감사하겠습니다!
#include <NewPing.h>
//sonar(TrigPin, EchoPin, MaxDistance);
NewPing sonar[3] = {
NewPing(2, 3, 200),
NewPing(4, 5, 200),
NewPing(6, 7, 200),
};
// 스텝 모터 신호핀 설정
int motorPin1 = 8;
int motorPin2 = 9;
int motorPin3 = 10;
int motorPin4 = 11;
// 모터 속도 관련 변수 설정
int motorSpeed; // 스텝 사이의 지연시간으로서 4500~1000의 범위를 갖는다.
int motorSpeedPercent; // 속도를 0~100%로 나타낸다.
// 스텝 모터의 스텝 설정
// 0~7은 동작 신호, 8번째는 모터 정시지 신호
int steps[] = {B1000, B1100, B0100, B0110, B0010, B0011, B0001, B1001, B0000};
void setup() {
Serial.begin(9600);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
}
void clockwise(){
// 7~0 번째 신호를 순차적으로 출력시킨다.
for(int i = 7; i >= 0; i--)
{
motorSignalOutput(i);
delayMicroseconds(motorSpeed);
}
}
void motorStop(){
// 정지신호를 출력시킨다.
motorSignalOutput(8);
}
void motorSignalOutput(int out)
{
// out 변수에 해당하는 모터 시그날을 출력한다.
digitalWrite(motorPin1, bitRead(steps[out], 0));
digitalWrite(motorPin2, bitRead(steps[out], 1));
digitalWrite(motorPin3, bitRead(steps[out], 2));
digitalWrite(motorPin4, bitRead(steps[out], 3));
}
void loop() {
delay(50);
Serial.print("A Ping : ");
Serial.print(sonar[0].ping_cm());
Serial.println("cm");
delay(50);
Serial.print("B Ping : ");
Serial.print(sonar[1].ping_cm());
Serial.println("cm");
delay(50);
Serial.print("C Ping : ");
Serial.print(sonar[2].ping_cm());
Serial.println("cm");
delay(500);
int distance1 = sonar[0].ping_cm();
int distance2 = sonar[1].ping_cm();
int distance3 = sonar[2].ping_cm();
// CW로 회전시 모터를 CW방향으로 회전시킨다.
if( distance1 <= 90){
//모터의 속도를 계산한다.
motorSpeed = map(distance1,90,0,4500,1000);
// 모터의 속도를 백분율로 변환시킨다.
motorSpeedPercent = map(motorSpeed,4500,1000,1,100);
// 시리얼 통신 메세지를 출력한다.
Serial.print("CW Motor Speed: ");
Serial.print(motorSpeedPercent);
Serial.println("%");
// CW로 회전시킨다.
clockwise();
}
else if( distance2 <= 90){
//모터의 속도를 계산한다.
motorSpeed = map(distance2,90,0,4500,1000);
// 모터의 속도를 백분율로 변환시킨다.
motorSpeedPercent = map(motorSpeed,4500,1000,1,100);
// 시리얼 통신 메세지를 출력한다.
Serial.print("CW Motor Speed: ");
Serial.print(motorSpeedPercent);
Serial.println("%");
// CW로 회전시킨다.
clockwise();
}
else if( distance3 <= 90){
//모터의 속도를 계산한다.
motorSpeed = map(distance3,90,0,4500,1000);
// 모터의 속도를 백분율로 변환시킨다.
motorSpeedPercent = map(motorSpeed,4500,1000,1,100);
// 시리얼 통신 메세지를 출력한다.
Serial.print("CW Motor Speed: ");
Serial.print(motorSpeedPercent);
Serial.println("%");
// CW로 회전시킨다.
clockwise();
}
else{
Serial.println("Motor Stop");
motorStop();
}
}
댓글 1
조회수 1,148master님의 댓글
master 작성일
void motorStop(){ // 정지신호를 출력시킨다.
motorSignalOutput(8);
}
정지는 아무런 출력도 하지 않는 것이 정지입니다.
이 함수를 사용 할 필요가 없겠죠
//
//모터의 속도를 계산한다.
motorSpeed = map(distance1,90,0,4500,1000);
딜레이가 부족하면 진동만하고 움직이지 않을 수 있습니다.
motorSpeed = map(distance1,90,0,30000,30000);
충분히 딜레이를 줘서 동작하는 것을 확인하고 , 그 후에 값을 줄여서 정상동작하는 딜레이 값을 찾아보세요
//
모터 전압이나 전류가 부족한 경우에도 진동만하고 회전하지 못하기도 합니다.
//
모터 토크에 비해서 부하가 큰 경우에도 진동만하고 회전하지 못하기도 합니다.