BASIC4MCU | 질문게시판 | 아두이노 스텝모터 값 제어(헤더파일 사용X)
페이지 정보
작성자 산만 작성일2022-12-09 11:22 조회1,080회 댓글0건첨부파일
본문
float value, data;
float value1 = 0.072; // 스텝 값
const int button1 = 12;
const int button2 = 11;
int mode=0;
int count1, count2;
int i, j;
int a = 0;
int nState = 1;
int v1,v2;
//
#define CW 2 //정회전
#define CCW 3 //역회전
//
void CW_n(int n){
for(int i=0;i<n;i++){
digitalWrite(CW,HIGH); delayMicroseconds(500);
digitalWrite(CW,LOW); delayMicroseconds(500);
}
}
void CCW_n(int n){
for(int i=0;i<n;i++){
digitalWrite(CCW,HIGH); delayMicroseconds(500);
digitalWrite(CCW,LOW); delayMicroseconds(500);
}
}
void setup() {
Serial.begin(9600);
pinMode(CW,OUTPUT); pinMode(CCW,OUTPUT);
pinMode(button1,INPUT_PULLUP); pinMode(button2,INPUT_PULLUP);
}
void loop() {
if (nState) {
Serial.println("Enter 2 Integers to add");
nState = 0;
}
//
if(mode==0){
for(count1=0; count1<=4800; count1+=200){
if(digitalRead(button1) == LOW){
break;
}
else{
mode=1;
}
for(i=0; i<=200; i+=200){
Serial.print((String)"정회전 = ");
Serial.println(i);
CW_n(i);
}
}
Serial.print((String)"정회전 합 = ");
Serial.println(count1);
}
if(mode==1){
delay(1000);
i = count1;
CCW_n(i); mode=2;
}
if(mode==2){
delay(1500);
for(count2=0; count2<=4800; count2+=200){
if(digitalRead(button2) == LOW){
break;
}
else{
mode=3;
}
for(j=0; j<=200; j+=200){
Serial.print((String)"역회전 = ");
Serial.println(j);
CCW_n(j);
}
}
Serial.print((String)"역회전 합 = ");
Serial.println(count2);
}
if(mode==3){
delay(1000);
j = count2;
CW_n(j); mode=4;
}
if(mode==4){
delay(1000);
a = (count1 + -count2) / 2;
Serial.print((String)"원점 이동 = ");
Serial.println(a);
CW_n(a);
CCW_n(-a); mode=5;
}
//수정이 필요한 시점
if(mode==5){
while(Serial.available() == 0){}
int v1 = Serial.parseInt();
Serial.print((String)"입력 값 : ");
Serial.println(v1, DEC);
value = v1 / value1;
Serial.print((String)"스텝 수 : ");
Serial.println(value, 4);
CW_n(value);
delay(200);
CCW_n(-value);
delay(200);
//
while(Serial.available() == 0){}
int v2 = Serial.parseInt();
Serial.print((String)"입력값 : ");
Serial.println(v2);
int value = (v2-v1) / value1;
Serial.print("value = ");
Serial.println(value, 4);
while(value != 0){
value = v1;
Serial.print((String)"현재 값 : ");
Serial.println(value);
}
//
CW_n(value);
delay(200);
CCW_n(-value);
delay(200);
//nState = 1;
}
}
스텝모터 : A50K-M566-G10(오토닉스)스텝모터 드라이브 : MD5-HD14(오토닉스)Stepper 헤더파일 없이 짜집기로 스텝모터 구동하는 코드를 작성했습니다.이 코드는 시작하면 센서값(1) 이 HIGH 될 때까지 정회전, 센서값(1) HIGH되면 처음 시작 위치로 이동시작 위치 이동 후센서값(2) HIGH 될 때까지 역회전, 센서값(2) HIGH되면 시작 위치로 이동(센서값(1)+센서값(2)) / 2를 하여 원점으로 이동 합니다.여기까지는 구동이 됩니다.여기서 이제 시리얼통신에 10도라는 값을 입력하면 10도 만큼 이동합니다.그 후 20도로 보내고 싶으면 스텝모터 특성상 20-10 = 10 해서 20도 위치 로 가야하는데처음 10도 간 후 20을 입력하면 총 30도 위치만큼 갑니다.예를 들어 처음에 10도를 주고 다시 -10도를 주면 -10도 가야하는데 0으로 갑니다.어떻게 코딩을 해야 -10도로 갈수 있을까요?※코드는 master님의 코드를 참고했습니다.
댓글 0
조회수 1,080등록된 댓글이 없습니다.