#include <Stepper.h>
Stepper motor(2048, A5, A3, A4, A2);
bool state = false;
bool autoState = false;
long cnt = 0;
String protocol = "";
void setup(){
Serial.begin(9600);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
pinMode(A4, OUTPUT);
pinMode(A5, OUTPUT);
f_speed(16);
}
void loop(){
if(state){
if(cnt == 0){
state = false;
return;
}else if(cnt > 0){
motor.step(1);
if(!autoState){
cnt--;
}
}else if(cnt < 0){
motor.step(-1);
if(!autoState){
cnt++;
}
}
}
}
void serialEvent(){
if(Serial.available()>0){
char c = (char)Serial.read();
if(c != '\n'){
protocol += c;
}else{
int index = protocol.indexOf(0x2F);
if(index != -1){
String str = protocol.substring(0, index);
if(str.equals("MANUAL")){
f_step(protocol.substring(index+1).toInt(), true, false);
Serial.print("STATE/M\n");
}else if(str.equals("SPEED")){
f_speed(protocol.substring(index+1).toInt());
}
}else{
if(protocol.equals("CW")){
f_step(1, true, true);
Serial.print("STATE/CW\n");
}else if(protocol.equals("CCW")){
f_step(-1, true, true);
Serial.print("STATE/CCW\n");
}else if(protocol.equals("STOP")){
f_step(0, false, false);
Serial.print("STATE/STOP\n");
}
}
protocol = "";
}
}
}
void f_speed(int index){
if(index > 16){
index = 16;
}else if(index < 0){
index = 0;
}
motor.setSpeed(index);
Serial.print("SPEED/" + String(index) + "\n");
}
void f_step(int c, bool b1, bool b2){
cnt = c;
state = b1;
autoState = b2;
}
★ [ IOT코아 ]에서 실행방법은 아래 링크를 참고해주세요
https://koa-rea.tistory.com/67
[IOT코아]아두이노 Step 모터 실시간 제어하기
※아이디어를 주시면, 부품이 있을 경우, 예제로 만들어드리겠습니다~ ^ㅡ^ 답글 또는 방명록에 글올려주세요~ ☆ 본 예제는 다음 기능을 포함하는 스텝모터 제어입니다. ○ 실시간 "CW"(정방향)&"
koa-rea.tistory.com
★ 실제 동작 영상은 아래 링크를 참고해주세요
https://youtube.com/shorts/EAieijq_coE?feature=share

'아두이노 예제 코드' 카테고리의 다른 글
아두이노 LORA( RF-95) 화재( Server ) 코드 (0) | 2022.12.05 |
---|---|
아두이노 LORA( RF-95) 화재( Client ) 코드 (0) | 2022.12.05 |
아두이노 IOT 출입시스템 만들기( 코드 ) (0) | 2022.12.01 |
아두이노 MLX90614, DHT11 코드 (0) | 2022.11.30 |
아두이노 초음파거리센서 코드( 연동 ) (0) | 2022.11.30 |
댓글