BASIC4MCU | 질문게시판 | 답변 : 스텝모터 제어하는 소스파일인데 질문있습니다.
페이지 정보
작성자 master 작성일2023-05-27 19:54 조회294회 댓글0건본문
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
//
const int bt_start=2,bt_countup=3,bt_countdown=4,bt_speedup=5,bt_speeddown=6;
const int DIR=8,STEP=9
//
bool flag_countup=0,flag_countdown=0;
int speed=0,pulse=0,run=0;
//
void setup(){
pinMode(bt_start,INPUT_PULLUP);
pinMode(bt_countup,INPUT_PULLUP);
pinMode(bt_countdown,INPUT_PULLUP);
pinMode(bt_speedup,INPUT_PULLUP);
pinMode(bt_speeddown,INPUT_PULLUP);
lcd.init(); lcd.backlight();
lcd.setCursor(0,0); lcd.print(" FILAMENT WINDER");
lcd.setCursor(0,1); lcd.print(" CAPSTONE DESIGN");
lcd.setCursor(0,2); lcd.print(" INJE UNIV ");
lcd.setCursor(0,3); lcd.print(" HELLO WORLD ");
delay(3000);
lcd.clear();
Serial.begin(9600);
}
//
void lcd_disp(){
lcd.setCursor(0,0); lcd.print(" StepMotor Speed ");
lcd.setCursor(0,1); lcd.print(" -200~200~"); lcd.print(speed); lcd.print(" ");
lcd.setCursor(0,2); lcd.print(" StepMotor Count");
lcd.setCursor(0,3); lcd.print(" 1spin/200pulse~"); lcd.print(pulse/200); lcd.print(" ");
}
//
void loop(){
// lcd_disp();
//-----------------------------------------------------------------------------------
if(digitalRead(bt_start)==0){ run=1; } // 스위치 1을 눌렀을 때 모터가 회전을 시작합니다.
//-----------------------------------------------------------------------------------
if(digitalRead(bt_countup)==0){ // 스위치 2는 누른 횟수만큼 스텝 모터가 회전을 합니다.
if(flag_countup==0){ flag_countup=1; pulse+=200; lcd_disp(); delay(50); }
}
else{
if(flag_countup){ flag_countup=0; delay(50); }
}
//-----------------------------------------------------------------------------------
if(digitalRead(bt_countdown)==0){ // 스위치 3은 누른 횟수만큼 스텝 모터 회전을 discount하는데 0보다는 작아지지 않게
if(flag_countdown==0){ flag_countdown=1; pulse-=200; if(pulse<0)pulse=0; lcd_disp(); delay(50); }
}
else{
if(flag_countdown){ flag_countdown=0; delay(50); }
}
//-----------------------------------------------------------------------------------
else if(digitalRead(bt_speedup)==0){ // 스위치 4는 모터의 회전 속도가 빨라집니다
speed-=10; if(speed<200)speed=200; lcd_disp();
}
//-----------------------------------------------------------------------------------
if(digitalRead(bt_speeddown)==0){ // 스위치 5는 회전 속도가 느려지게
speed+=10; if(speed>2000)speed=2000; lcd_disp();
}
//-----------------------------------------------------------------------------------
if(run){
if(pulse){ pulse--;
digitalWrite(STEP,1); delayMicroseconds(speed);
digitalWrite(STEP,0); delayMicroseconds(speed);
}
}
}
댓글 0
조회수 294등록된 댓글이 없습니다.