소스코드
아래는 소스코드입니다. 아두이노 IDE에 붙여넣기 하여 컴파일하고 업로드합니다.
//스텝모터 구동실험 int IN1 = 7; //IN1 int IN2 = 6; //IN2 int IN3 = 5; // IN3 int IN4 = 4; // IN4 int beep= 3; // 액티브부저 -극에 연결 char time = 0, i = 0, count = 0; void setup() { //출력 모드로 모터 드라이브 IO 초기화 pinMode(IN1, OUTPUT); // PIN 7 pinMode(IN2, OUTPUT); // PIN 6 pinMode(IN3, OUTPUT); // PIN 5 pinMode(IN4, OUTPUT); // PIN 4 pinMode(beep, OUTPUT); //PIN 3 digitalWrite(beep, HIGH); Serial.begin(9600);//시리얼 전송속도 9600bps } /*******************************4상8스텝***********************************/ void Coil_A() { digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); digitalWrite(IN3, HIGH); digitalWrite(IN4, HIGH); } void Coil_AB() { digitalWrite(IN1, LOW); digitalWrite(IN2, LOW); digitalWrite(IN3, HIGH); digitalWrite(IN4, HIGH); } void Coil_B() { digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); digitalWrite(IN3, HIGH); digitalWrite(IN4, HIGH); } void Coil_BC() { digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH); } void Coil_C() { digitalWrite(IN1, HIGH); digitalWrite(IN2, HIGH); digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH); } void Coil_CD() { digitalWrite(IN1, HIGH); digitalWrite(IN2, HIGH); digitalWrite(IN3, LOW); digitalWrite(IN4, LOW); } void Coil_D() { digitalWrite(IN1, HIGH); digitalWrite(IN2, HIGH); digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW); } void Coil_DA() { digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW); } /*******************************************************************/ void beep_once()//부저를 한 번 울린다. { digitalWrite(beep, LOW); delay(100); digitalWrite(beep, HIGH); } void loop() { unsigned long now_time = micros(); //현재 시스템 런타임을 불러옵니다. while ((micros() - now_time) < 14648); // 60 000 000us/4096 switch (i) { case 0: Coil_A(); i++; break; case 1: Coil_AB(); i++; break; case 2: Coil_B(); i++; break; case 3: Coil_BC(); i++; break; case 4: Coil_C(); i++; break; case 5: Coil_CD(); i++; break; case 6: Coil_D(); i++; break; case 7: Coil_DA(); i = 0; break; default: break; } count++; if (count == 68)//1초이면 시간을 표시하고 부저를 한번 울립니다. { count = 0; time++; if (time == 60) time = 0; Serial.print("second: " ) ; Serial.print(time, DEC) ; Serial.println("s" ) ; beep_once(); } }