2017년 6월 15일 목요일
아두이노로 Step-Motor 구동하기
//////////////Step-Motor구동
int A = 8;
int B = 9;
int A_NOT = 10;
int B_NOT = 11;
int myDelay = 100;
void setup()
{
pinMode(A,OUTPUT);
pinMode(B,OUTPUT);
pinMode(A_NOT,OUTPUT);
pinMode(B_NOT,OUTPUT);
}
void loop()
{
digitalWrite(A,1);
digitalWrite(B,0);
digitalWrite(A_NOT,0);
digitalWrite(B_NOT,0);
delay(myDelay);
digitalWrite(A,0);
digitalWrite(B,1);
digitalWrite(A_NOT,0);
digitalWrite(B_NOT,0);
delay(myDelay);
digitalWrite(A,0);
digitalWrite(B,0);
digitalWrite(A_NOT,1);
digitalWrite(B_NOT,0);
delay(myDelay);
digitalWrite(A,0);
digitalWrite(B,0);
digitalWrite(A_NOT,0);
digitalWrite(B_NOT,1);
delay(myDelay);
}
//////////////1상--1회전후 5초 Sleep
int A = 8;
int B = 9;
int A_NOT = 10;
int B_NOT = 11;
int myDelay = 10;
void setup()
{
pinMode(A,OUTPUT);
pinMode(B,OUTPUT);
pinMode(A_NOT,OUTPUT);
pinMode(B_NOT,OUTPUT);
}
void loop()
{
int i;
for(i=0; i<50; i++){
digitalWrite(A,1);
digitalWrite(B,0);
digitalWrite(A_NOT,0);
digitalWrite(B_NOT,0);
delay(myDelay);
digitalWrite(A,0);
digitalWrite(B,1);
digitalWrite(A_NOT,0);
digitalWrite(B_NOT,0);
delay(myDelay);
digitalWrite(A,0);
digitalWrite(B,0);
digitalWrite(A_NOT,1);
digitalWrite(B_NOT,0);
delay(myDelay);
digitalWrite(A,0);
digitalWrite(B,0);
digitalWrite(A_NOT,0);
digitalWrite(B_NOT,1);
delay(myDelay);
}
delay(5000);
}
//////////////2상--1회전(CW)후 5초 Sleep
int A = 8;
int B = 9;
int A_NOT = 10;
int B_NOT = 11;
int myDelay = 50;
void setup()
{
pinMode(A,OUTPUT);
pinMode(B,OUTPUT);
pinMode(A_NOT,OUTPUT);
pinMode(B_NOT,OUTPUT);
}
void loop()
{
int i;
for(i=0; i<50; i++){
digitalWrite(A,1);
digitalWrite(B,0);
digitalWrite(A_NOT,0);
digitalWrite(B_NOT,1);
delay(myDelay);
digitalWrite(A,1);
digitalWrite(B,1);
digitalWrite(A_NOT,0);
digitalWrite(B_NOT,0);
delay(myDelay);
digitalWrite(A,0);
digitalWrite(B,1);
digitalWrite(A_NOT,1);
digitalWrite(B_NOT,0);
delay(myDelay);
digitalWrite(A,0);
digitalWrite(B,0);
digitalWrite(A_NOT,1);
digitalWrite(B_NOT,1);
delay(myDelay);
}
delay(5000);
}
//////////////2상--CW,CCW 각1회전후 5초 Sleep
int A = 8;
int B = 9;
int A_NOT = 10;
int B_NOT = 11;
int myDelay = 20;
void setup()
{
pinMode(A,OUTPUT);
pinMode(B,OUTPUT);
pinMode(A_NOT,OUTPUT);
pinMode(B_NOT,OUTPUT);
}
void loop()
{
int i;
for(i=0; i<50; i++){
digitalWrite(A,1);
digitalWrite(B,0);
digitalWrite(A_NOT,0);
digitalWrite(B_NOT,1);
delay(myDelay);
digitalWrite(A,1);
digitalWrite(B,1);
digitalWrite(A_NOT,0);
digitalWrite(B_NOT,0);
delay(myDelay);
digitalWrite(A,0);
digitalWrite(B,1);
digitalWrite(A_NOT,1);
digitalWrite(B_NOT,0);
delay(myDelay);
digitalWrite(A,0);
digitalWrite(B,0);
digitalWrite(A_NOT,1);
digitalWrite(B_NOT,1);
delay(myDelay);
}
delay(5000);
for(i=0; i<50; i++){
digitalWrite(A,0);
digitalWrite(B,0);
digitalWrite(A_NOT,1);
digitalWrite(B_NOT,1);
delay(myDelay);
digitalWrite(A,0);
digitalWrite(B,1);
digitalWrite(A_NOT,1);
digitalWrite(B_NOT,0);
delay(myDelay);
digitalWrite(A,1);
digitalWrite(B,1);
digitalWrite(A_NOT,0);
digitalWrite(B_NOT,0);
delay(myDelay);
digitalWrite(A,1);
digitalWrite(B,0);
digitalWrite(A_NOT,0);
digitalWrite(B_NOT,1);
delay(myDelay);
}
delay(5000);
}
//////////////2상--CW 3회전 3초 Sleep, CCW 2회전 5초 Sleep
int A = 8;
int B = 9;
int A_NOT = 10;
int B_NOT = 11;
int myDelay = 10; //100Hz
int myDelay1 = 20; //50Hz
void setup()
{
pinMode(A,OUTPUT);
pinMode(B,OUTPUT);
pinMode(A_NOT,OUTPUT);
pinMode(B_NOT,OUTPUT);
}
void loop()
{
int i;
for(i=0; i<150; i++){
digitalWrite(A,1);
digitalWrite(B,0);
digitalWrite(A_NOT,0);
digitalWrite(B_NOT,1);
delay(myDelay);
digitalWrite(A,1);
digitalWrite(B,1);
digitalWrite(A_NOT,0);
digitalWrite(B_NOT,0);
delay(myDelay);
digitalWrite(A,0);
digitalWrite(B,1);
digitalWrite(A_NOT,1);
digitalWrite(B_NOT,0);
delay(myDelay);
digitalWrite(A,0);
digitalWrite(B,0);
digitalWrite(A_NOT,1);
digitalWrite(B_NOT,1);
delay(myDelay);
}
delay(3000);
for(i=0; i<100; i++){
digitalWrite(A,0);
digitalWrite(B,0);
digitalWrite(A_NOT,1);
digitalWrite(B_NOT,1);
delay(myDelay1);
digitalWrite(A,0);
digitalWrite(B,1);
digitalWrite(A_NOT,1);
digitalWrite(B_NOT,0);
delay(myDelay1);
digitalWrite(A,1);
digitalWrite(B,1);
digitalWrite(A_NOT,0);
digitalWrite(B_NOT,0);
delay(myDelay1);
digitalWrite(A,1);
digitalWrite(B,0);
digitalWrite(A_NOT,0);
digitalWrite(B_NOT,1);
delay(myDelay1);
}
delay(5000);
}
/////////////////////////CW,CCW 제어
int A = 8;
int B = 9;
int A_NOT = 10;
int B_NOT = 11;
void setup()
{
pinMode(A,OUTPUT);
pinMode(B,OUTPUT);
pinMode(A_NOT,OUTPUT);
pinMode(B_NOT,OUTPUT);
}
void CW(int rota, int myDelay)
{
int i;
for(i=0; i<50*rota; i++)
{
digitalWrite(A,1);
digitalWrite(B,0);
digitalWrite(A_NOT,0);
digitalWrite(B_NOT,1);
delay(myDelay);
digitalWrite(A,1);
digitalWrite(B,1);
digitalWrite(A_NOT,0);
digitalWrite(B_NOT,0);
delay(myDelay);
digitalWrite(A,0);
digitalWrite(B,1);
digitalWrite(A_NOT,1);
digitalWrite(B_NOT,0);
delay(myDelay);
digitalWrite(A,0);
digitalWrite(B,0);
digitalWrite(A_NOT,1);
digitalWrite(B_NOT,1);
delay(myDelay);
}
}
void CCW(int rota1, int myDelay1)
{
int j;
for(j=0; j<50*rota1; j++)
{
digitalWrite(A,0);
digitalWrite(B,0);
digitalWrite(A_NOT,1);
digitalWrite(B_NOT,1);
delay(myDelay1);
digitalWrite(A,0);
digitalWrite(B,1);
digitalWrite(A_NOT,1);
digitalWrite(B_NOT,0);
delay(myDelay1);
digitalWrite(A,1);
digitalWrite(B,1);
digitalWrite(A_NOT,0);
digitalWrite(B_NOT,0);
delay(myDelay1);
digitalWrite(A,1);
digitalWrite(B,0);
digitalWrite(A_NOT,0);
digitalWrite(B_NOT,1);
delay(myDelay1);
}
}
void loop()
{
CW(5,10);
delay(3000);
CCW(3,20);
delay(5000);
}
////////////////정역스위치로 CW,CCW 제어
int A = 8;
int B = 9;
int A_NOT = 10;
int B_NOT = 11;
void setup()
{
pinMode(A,OUTPUT);
pinMode(B,OUTPUT);
pinMode(A_NOT,OUTPUT);
pinMode(B_NOT,OUTPUT);
pinMode(6,INPUT);
pinMode(7,INPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
}
void CW(int rota, int myDelay) //정회전함수
{
int i;
for(i=0; i<50*rota; i++)
{
digitalWrite(A,1);
digitalWrite(B,0);
digitalWrite(A_NOT,0);
digitalWrite(B_NOT,1);
delay(myDelay);
digitalWrite(A,1);
digitalWrite(B,1);
digitalWrite(A_NOT,0);
digitalWrite(B_NOT,0);
delay(myDelay);
digitalWrite(A,0);
digitalWrite(B,1);
digitalWrite(A_NOT,1);
digitalWrite(B_NOT,0);
delay(myDelay);
digitalWrite(A,0);
digitalWrite(B,0);
digitalWrite(A_NOT,1);
digitalWrite(B_NOT,1);
delay(myDelay);
}
}
void CCW(int rota1, int myDelay1) //역회전함수
{
int j;
for(j=0; j<50*rota1; j++)
{
digitalWrite(A,0);
digitalWrite(B,0);
digitalWrite(A_NOT,1);
digitalWrite(B_NOT,1);
delay(myDelay1);
digitalWrite(A,0);
digitalWrite(B,1);
digitalWrite(A_NOT,1);
digitalWrite(B_NOT,0);
delay(myDelay1);
digitalWrite(A,1);
digitalWrite(B,1);
digitalWrite(A_NOT,0);
digitalWrite(B_NOT,0);
delay(myDelay1);
digitalWrite(A,1);
digitalWrite(B,0);
digitalWrite(A_NOT,0);
digitalWrite(B_NOT,1);
delay(myDelay1);
}
}
void loop()
{
int CWsw = digitalRead(6);
int CCWsw = digitalRead(7);
if (CWsw == 1)
{
digitalWrite(12,1);
CW(4,10); //정방향 4회전,100Hz
}else{
digitalWrite(12,0);
}
if (CCWsw == 1)
{
digitalWrite(13,1);
CCW(3,20); //역방향 3회전,50Hz
}else{
digitalWrite(13,0);
}
}
//////////////스위치 하나로 CW,CCW 제어
int A = 8;
int B = 9;
int A_NOT = 10;
int B_NOT = 11;
int preSW = 0;
int STATUS = 0;
void setup()
{
pinMode(A,OUTPUT);
pinMode(B,OUTPUT);
pinMode(A_NOT,OUTPUT);
pinMode(B_NOT,OUTPUT);
pinMode(6,INPUT);
pinMode(7,INPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
}
void CW(int rota, int myDelay)
{
int i;
for(i=0; i<50*rota; i++)
{
digitalWrite(A,1);
digitalWrite(B,0);
digitalWrite(A_NOT,0);
digitalWrite(B_NOT,1);
delay(myDelay);
digitalWrite(A,1);
digitalWrite(B,1);
digitalWrite(A_NOT,0);
digitalWrite(B_NOT,0);
delay(myDelay);
digitalWrite(A,0);
digitalWrite(B,1);
digitalWrite(A_NOT,1);
digitalWrite(B_NOT,0);
delay(myDelay);
digitalWrite(A,0);
digitalWrite(B,0);
digitalWrite(A_NOT,1);
digitalWrite(B_NOT,1);
delay(myDelay);
}
}
void CCW(int rota1, int myDelay1)
{
int j;
for(j=0; j<50*rota1; j++)
{
digitalWrite(A,0);
digitalWrite(B,0);
digitalWrite(A_NOT,1);
digitalWrite(B_NOT,1);
delay(myDelay1);
digitalWrite(A,0);
digitalWrite(B,1);
digitalWrite(A_NOT,1);
digitalWrite(B_NOT,0);
delay(myDelay1);
digitalWrite(A,1);
digitalWrite(B,1);
digitalWrite(A_NOT,0);
digitalWrite(B_NOT,0);
delay(myDelay1);
digitalWrite(A,1);
digitalWrite(B,0);
digitalWrite(A_NOT,0);
digitalWrite(B_NOT,1);
delay(myDelay1);
}
}
void loop()
{
int SW1 = digitalRead(6);
if (SW1 != preSW)
{
if (SW1 == 1)
{
STATUS++;
STATUS = STATUS % 2;
if (STATUS ==1)
{
digitalWrite(12,1);
CW(3,10);
digitalWrite(12,0);
}
else if (STATUS == 0)
{
digitalWrite(13,1);
CCW(2,20);
digitalWrite(13,0);
}
}
preSW = SW1;
}
}
피드 구독하기:
댓글 (Atom)
댓글 없음:
댓글 쓰기